Comments (5)
Ok, so if I understand correctly, currently the image URLs necessarily point to the provider, and the optimization (media sizes, webp / avif) must be done by the provider.
Only images hosted locally (thus directly in the Nuxt project) can be optimized throught IPX.
Is that correct ?
from image.
@danielroe Isn't it more of a bug than an enhancement? Generating and optimizing images for a static build from the provider is one of the key features of nuxt-image, if I'm not mistaken?
from image.
Nuxt Image supports static image optimisation using ipx; it doesn't currently do static optimisation of images from a runtime provider, though I think it would be a nice enhancement.
from image.
Exactly. Remote images can also be optimised but not if they are paired with a runtime provider.
from image.
Have the same issue with Strapi. Got a small workaround creating a new provider that runs only on 'prerender' (nuxt generate
) phase.
Its not perfect but so far it's the best i could come up with
import { type ProviderGetImage } from "@nuxt/image";
import axios from "axios";
import fs from "node:fs";
import * as ufo from "ufo";
const OUTPUT_DIR = '.output';
const PUBLIC_DIR = 'public';
function pullImageAndSave(remoteURL: string, distDir: string) {
return axios({
method: "GET",
url: remoteURL,
responseType: "stream",
})
.then((response) => response.data.pipe(fs.createWriteStream(distDir)))
.catch((error) => {
console.error(`Could not load image ${remoteURL}`);
});
}
export const getImage: ProviderGetImage = (
src,
{ baseURL, localDir } = {},
) => {
const dir = ufo.joinURL(OUTPUT_DIR, PUBLIC_DIR, localDir);
const remoteURL = ufo.joinURL(baseURL, src);
// return remote path for development
if (process.env.NODE_ENV === 'development') {
return {
url: remoteURL,
};
}
const filename = src.split("/").pop();
const distUrl = ufo.joinURL(localDir, filename!);
if (process.env.NODE_ENV === 'prerender') {
if (!fs.existsSync(dir)) {
try {
fs.mkdirSync(dir, { recursive: true });
} catch (error) {
console.log(error);
}
}
if (filename) {
const distDir = ufo.joinURL(dir, filename);
pullImageAndSave(remoteURL, distDir);
}
}
return {
url: distUrl,
};
};
But it has one downside, pullImageAndSave
is async, used in sync code which causes the nuxt generate
to be freeze at the end - i guess its because the script is still pulling the images after the prerender was done.
@danielroe maybe you have an idea how to fix that?
from image.
Related Issues (20)
- Disable auto-detection of a specific provider HOT 3
- Missing fit modes on cloudinary provider, and existing ones are wrongly mapped HOT 4
- Add provider for h2non/imaginary HOT 1
- Nitro publicAssets 404
- Provider Vercel is ignoring format
- 打包文件出席错误 HOT 3
- IPX modifier background color does not work
- IPX Internal Server Error HOT 8
- Bunny bugs HOT 2
- IPX_ERROR
- IPX error 500 with `nuxt generate`
- Absolute Cloudinary URLs do not work
- IPX_FILE_NOT_FOUND when using npm run build
- New provider for Filerobot from Scaleflex
- nuxt-image + glob v9.0 break build HOT 4
- NuxtImg with Nuxt 3.12.1 has error about "#imports"
- Uploadcare proxy feature HOT 1
- Uploadcare problem with xxl/2xl screens
- IPX Static Render Skipped with Custom BaseURL
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from image.