For a project, I'm trying to import images from a remote source and copy them in my static
folder (I'm not sure which one is better between static
or assets
) as I generate my routes, so I can use them locally as part of the nuxt build process.
So in my nuxt.config.js
file, I'm using the writeFileSync
from the Node fs
library:
import fs from 'fs'
import path from 'path'
export default {
// ...
generate: {
async routes() {
const url = `https://via.placeholder.com/300/09f/fff.png`
const dest = `${path.resolve(
__dirname
)}/static/images/fff.png`
fs.writeFileSync(dest, url, 'ascii') // I also tried 'binary'
}
}
I'm getting no error when I run npm run generate
but the image is unreadable and weight only a few octets.