How to load more images in fallbacks image, next-pwa - next-pwa

Versions
next-pwa:
next:
Question
How to load more images in fallbacks.image
for example i neeed two images into fallbacks
file - next.config.js
const withPWA = require('next-pwa');
module.exports = withPWA({
pwa: {
dest: 'public',
fallbacks: {
image: 'static/fallback.svg'
},
}
})

Related

EJS can't find image in static public folder

I'm trying render a html template as a PDF using EJS, however the images are not appearing.
My project structure:
- public
- images
image.jpg
- routes
documentsRoute.js
- services
documentsService.js
- views
document-template.ejs
server.js
I am setting the public folder as a static folder in my server.js like so:
...
const app = express()
app.use(express.static('public'))
...
In my template file I'm using this as my src for the image
<img src="images/image.jpg" alt="my_image">
I'm rendering the file from my document service like so:
ejs.renderFile('views/document-template.ejs', inputs, (err, data) => {
if (err){
throw err;
}
let options = {
"format": "A4",
"header": {
"height": "20mm"
},
"footer": {
"height": "20mm"
}
};
pdf.create(data, options).toFile('test.pdf', (err, res) => {
if(err){
throw err;
}
});
})
The rest of the template is rendered correctly, just not the image, instead it shows the alt text. Does anyone know how I could fix this?
You need to set the view directory correctly in your server file.
So instead of app.use(express.static('public'))
try
app.use(express.static( path.join(__dirname, './public')))
it's rather pdf having trouble with the path. try using absolute path
add full path in the base property of pdf options:
let options = {
"format": "A4",
//...
base: `${req.protocol}://${req.headers.host}`
};

How to disable gulp encoding images with base64?

So after gulp work, i have encoded images in base64 in css file which size is 2.8mb(((
Here is my gulpfile:
const path = {
stylus: {
src: './src/stylus/**/*.styl',
dest: './build/styles',
},
build: {
dest: 'build/**'
}
}
function stylusTask() {
return src(path.stylus.src)
.pipe(plumber())
.pipe(stylus({
use: nib(),
import: ['nib'],
compress: true
}))
.pipe(dest(path.stylus.dest))
}
You can configure stylus to only encode images smaller than a specified limit. The urls for images which exceed that limit will not be modified.
In this example, only images smaller than 2000 bytes are encoded:
function stylusTask() {
return src(path.stylus.src)
.pipe(plumber())
.pipe(stylus({
use: nib(),
import: ['nib'],
compress: true,
define: {
url: require('stylus').url({
limit:2000
})
}
}))
.pipe(dest(path.stylus.dest))
}
For more information on the url function, see the following documentation:
https://stylus-lang.com/docs/functions.url.html

Why webpack converts require('path to img.png') in base64?

I have a chat, and a JSON file where all the history is stored. All images/video/audio link to require('path to media').
However, for some reason, when using vue-cli-service serve or vue-cli-service build, webpack skips my image which is used for Emoji, not including it in dist folder, but converting it to base64 string.
JSON looks like this:
{ type: 'emoji', author: `me`, data: { src: require('../../Media/img/smiling-face.png') } },
{ type: 'text', author: `me`, data: { text: `Do you read me...`, meta: '✓✓ Read' } },
{ type: 'image', author: `support`, data: { src: require('../../Media/img/2.gif'), meta: '✓✓ Read' } },
{ type: 'image', author: `me`, data: { src: require('../../Media/img/1.jpg'), meta: '✓✓ Read' } },
My Emoji type message is converted to base64 for some reason, and the png image is not included in the final build.
In the browser I get the following entry: {author: "support", type: "emoji", data: {src: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEgAAABICAYAAABV7bNHAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAB3RJTUUH4wMGFAELqykF5QAAAAZiS0dEAP8A/wD/oL2nkwAACghJREFUeNrtXAtsFNcVHTAJUAkXEQRORFTRqKkdoCUoIjRULd712t5dW26SpopqpWoLSdQUYytJS0sFbVpEU0D9qEmIaEraRPm0VK7URkUUu01IkAz+QFVQPyoQTD5g49A1Msbez+09b+/Mjve/M7M7S4Wlox3NvHfvuWfuvM+d9Wqaw3+nbvMA0xg3MJYyVjFWMBad/JjnOqIvVNCJ5nnU31TD8DJaGR2MzYwnBJvlXKu0qVF9uC9swJbYXCU+bhCfWtn+iTAzGCsZOxmHGe8yLjDOM06cXup59YNNDS9E3wz20NGmQQ48xJhgRBmUhKhcC6Et+qDvKbYBW2Lzgvg4LD5XCoeyEwaYz9jGGGJQJpy+3UPnHvDRRGeAaCBFlFRwG7RFH/TNZlt8bxMuWlkIJUSqGHsZ0RwBKJys9tBZr5fGX/ITZ0dmcfga2qAt+uRjWz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j/Ji7nnNy5yJYvgE77BwUb29IsGqf/MYsqiRyxnES/Ozv+5ln7wQGVJRYIv+IRvGwvEiMSupRXIJNJ8xkE7M9q/9n6Kvv352SURCT7gCz5tzlwHJfbM/xJlyqLPWZ7RJJOOv3InbbrvQ0UVCbbhA75sbi1GJWYtq0Amka5n7LK7P/tP51305FfmGlsIJ6dz4IdsGz4c2Hftkpjz+69DEWmxDFpk53Eb6faqDSc2mGovZXOmgg3Ygk3YtvlY6QPz4rzFSXrU/Iwhu5kU4Y3jiVdW0lNtC6lNhFpXQEbpm1f0hQ3Ygk0HMmdIYtQKEsgk0jTGBsa4/TevzWoB94/frFJ3/3utc6jNX2GMJWuToI9daIO26IO+ahF41JHK4bjENq1gcZJEmsXYzgg7Usrk4GL8GXrDR//mmef1p5bS777/UdrDG8xnO6oUcIxzuIY2aBuTvg6VU8MS0yzL4iSJVCkDWcTROrKpFBuTuhIQ608qrTpb345ILJW2xUkSaa4YDl/FbyrCEsNcx8RJk0nbHRmTSo9x4V7puDhpxqQNtme30mJIOM8qmjhJIqHQ38gYuArEGRCu04suTpp10mJ5pi+VoTCXhNtiS+scB4WayWhhvFkmA3hYuLQIN3d/IseUTfOlXNDvklBheZwe0XflrouTQaiFjLWMblsVgcJ24t3ic2HZCZNFqDlS/N4hVcpRh0XpE9tr9BpyWQuTYz83T17E4ZftXpD33vn/yFu8bY/07RBb82zto8o4syokOPPPBLbn/JnAeJ+KUmfK/wBU57Pt2uYbkgAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAxOS0wMy0wNlQyMDowMTowMSswMDowMIwOVmUAAAAldEVYdGRhdGU6bW9kaWZ5ADIwMTktMDMtMDZUMjA6MDE6MDErMDA6MDD9U+7ZAAAAAElFTkSuQmCC"}}
But it should look like this: {author: "support", type: "emoji", data: {src: "img/smiling-face.png"}}
And this happens only with smiling-face.png, any other image, including other png is displayed correctly and included in the build, so can anyone say what's wrong?
Link to image "smiling-face.png": https://mega.nz/#!Ze5UzK7A!MdV23KlEou4ByfOlN0aCBc8N7KAGkvNnwPH1YFiltBE
Vue will encode any images below 10kb into base64 and inline into your JS bundle.
You can amend vue.config.js to remove this by setting the limit to -1:
module.exports = {
chainWebpack: config => {
config.module
.rule('images')
.use('url-loader')
.loader('url-loader')
.tap(options => {
options.limit = -1
return options
})
}
}

Nuxt Set src URL for application without effecting routing

I'm trying to set a base URL to the src URLs for my application to be '/static/' without affecting the routing or folder structure
Tried setting the router to '/static' but that changes the routing. Also tried to set the publicPath to '/static/_nuxt' but that generates a new folder so the structure ends up being '/static/static/_nuxt'.
//this changes the routing
module.exports = {
mode: "spa",
generate: {
dir: "dist/static"
},
router: {
base: '/static/'
}
}`
//this changes the file path:
module.exports = {
mode: "spa",
generate: {
dir: "dist/static"
},
build: {
publicPath: '/static/_nuxt'
}
}
Haven't found a configuration option that sets the src links without affecting the routing or folder structure.

Webpack replace asset for cache busting

I have question, might be really silly as I am a beginner with Webpack but so far impressed.
So, I have a really small personal project with Flask(Python) on the backend and React on the frontend and I'm fighting with cache busting (I mean, not now, while I'm developing no problem whatsoever with cache, but I'm worrying already for when I deploy).
I am using Webpack to bundle the js and css (right now just the js though). So I was wondering if it is possible with Webpack for me to write, say in the css, something like:
some-selector {
background: #00ff00 url("my-background.png") no-repeat fixed center;
}
or in the HTML
<script src="bundle.js"></script>
and have Webpack to replace those strings with the resource with a cache busting hash for when building for production?
like
some-selector {
background: #00ff00 url("my-background.987asdh23193jf13.png") no-repeat fixed center;
}
and
<script src="bundle.23kjbi24f92do20f.js"></script>
I saw some sutff about html-webpack-plugin or string-replace-loader but not quite what I was looking for.
So, the questions:
is it possible with Webpack?
is it possible at all?
is there a better way to do it?
Yes it is possible to do cache busting using webpack and you can use this code for that or reference is https://medium.com/#okonetchnikov/long-term-caching-of-static-assets-with-webpack-1ecb139adb95#.nctflpxl2
I never tried for images but it is also possible using webpack.
var webpack = require('webpack');
const path = require("path");
var ChunkHashReplacePlugin = require('chunkhash-replace-webpack-plugin');
var WebpackMd5Hash = require('webpack-md5-hash');
var ManifestPlugin = require('webpack-manifest-plugin');
var node_dir = __dirname + '/node_modules';
var HtmlWebpackPlugin = require('html-webpack-plugin');
var InlineManifestWebpackPlugin=require('inline-manifest-webpack-plugin');
module.exports = {
context: __dirname + '/app',
entry: {
app: './app.js',
vendor: ['angular', 'underscore', 'restangular', 'angular-ui-router', 'bootstrap', 'angular-ui-bootstrap', 'angular-animate', 'angular-sanitize']
},
output: {
path: path.join(__dirname, "js"),
filename: "[name].bundle.js"
// filename: "[name].[chunkhash].bundle.js"
},
plugins: [
function() {
this.plugin("done", function(stats) {
require("fs").writeFileSync(
path.join(__dirname, "js", "stats.json"),
JSON.stringify(stats.toJson()));
});
},
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
}),
new webpack.ProvidePlugin({
_: "underscore",
underscore: "underscore"
}),
new webpack.optimize.CommonsChunkPlugin({
name: ["vendor"], // vendor libs + extracted manifest
minChunks: Infinity
}),
new ManifestPlugin({
filename: "chunk-manifest.json",
manifestVariable: "webpackManifest"
}),
new WebpackMd5Hash(),
new InlineManifestWebpackPlugin({
name: 'webpackManifest'
}),
new HtmlWebpackPlugin({
title: ' Portal',
template: 'index.ejs',
filename:'../index.html'
})
],
devServer: {
inline: true,
headers: { "Access-Control-Allow-Origin": "*" }
},
resolve: {
alias: {
"underscore": node_dir + "/underscore/underscore-min.js"
}
}
};
};