Code Monkey home page Code Monkey logo

Comments (5)

Striffly avatar Striffly commented on July 24, 2024 1

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.

Striffly avatar Striffly commented on July 24, 2024

@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.

danielroe avatar danielroe commented on July 24, 2024

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.

danielroe avatar danielroe commented on July 24, 2024

Exactly. Remote images can also be optimised but not if they are paired with a runtime provider.

from image.

lukszy avatar lukszy commented on July 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.