image icon indicating copy to clipboard operation
image copied to clipboard

No image processing occurs after deployment (Cloudflare Pages)

Open AaronBeaudoin opened this issue 2 years ago • 29 comments

Info

  • Operating System: Darwin
  • Node Version: v18.18.2
  • Nuxt Version: 3.8.0
  • CLI Version: 3.9.1
  • Nitro Version: 2.7.0
  • Package Manager: [email protected]
  • Builder: -
  • User Config: modules, nitro, experimental
  • Runtime Modules: @nuxt/[email protected]
  • Build Modules: -

Reproduction

https://github.com/AaronBeaudoin/nuxt-image-issue-1

Description of Issue

In the reproduction above there is a minimal app.vue like this:

<template>
  <div>
    <h1>Nuxt Image Test</h1>
    <NuxtImg src="/cat.jpg" width="1024" quality="1"/>
  </div>
</template>

As you can see, the quality is set to 1. This is so that you can see very obviously that image processing is occurring. If you run npm run dev you'll see that the image looks like crap, so you know that IPX is working great.

aryse local_3000_

But now, deploy the reproduction to Cloudflare Pages. After doing so, go to the deployed URL and you'll see this:

nuxt-image-issue-1 pages dev_

Obviously image processing is not working.

AaronBeaudoin avatar Oct 19 '23 20:10 AaronBeaudoin

What's the url of the images in production? Is it something like /_ipx/... ?

DavidDeSloovere avatar Oct 20 '23 14:10 DavidDeSloovere

No, the URL is still just /cat.jpg.

The reproduction is right there. Deploy it and see for yourself.

AaronBeaudoin avatar Oct 20 '23 18:10 AaronBeaudoin

Indeed.

Deployed to Cloudflare: not working, are rather the src was not changed <img src="/cat.jpg" onerror="this.setAttribute('data-error', 1)" width="1024" data-nuxt-img="" srcset="/cat.jpg 1x, /cat.jpg 2x">

Deployed to Vercel: working <img src="/_vercel/image?url=/cat.jpg&amp;w=1024&amp;q=1" onerror="this.setAttribute('data-error', 1)" width="1024" data-nuxt-img="" srcset="/_vercel/image?url=/cat.jpg&amp;w=1024&amp;q=1 1x, /_vercel/image?url=/cat.jpg&amp;w=1536&amp;q=1 2x">

Maybe IPX doesn't run on CF runtime? Maybe something here with the baseUrl? https://github.com/nuxt/image/blob/58c5139106ed1552a98d318b5ca281e298c36180/src/runtime/providers/ipx.ts#L28C1-L30C4

DavidDeSloovere avatar Oct 20 '23 19:10 DavidDeSloovere

We're having the same issue with Deno Deploy and Cloudflare Pages combined with the IPX provider. Works fine with self hosted PM2 & Vercel for example.

Have managed to solve this somehow using a different configuration or setting?

adamskyle avatar Oct 30 '23 17:10 adamskyle

@adamskyle My current alternative until this is fixed is the following Custom Provider for Cloudflare Images:

import { encodeQueryItem, joinURL } from "ufo";
import { createOperationsGenerator } from "#image";
import type { ProviderGetImage } from "@nuxt/image";

const operationsGenerator = createOperationsGenerator({
  keyMap: {
    width: "w",
    height: "h"
  },
  joinWith: ",",
  formatter: (key: string, value: string) => {
    return encodeQueryItem(key, value);
  }
});

export const getImage: ProviderGetImage = (src, options) => {
  const base = "https://imagedelivery.net/<YOUR_ACCOUNT_HASH>";
  const operations = operationsGenerator(options.modifiers);
  return { url: joinURL(base, src, operations) };
};

Replace <YOUR_ACCOUNT_HASH> with your value from the Cloudflare Images dashboard page. Also, you should be able to add options in addition to width and height to the keyMap config by following the docs here.

Managing the images this way is a lot more tedious than just storing them in the repository though.

AaronBeaudoin avatar Oct 30 '23 21:10 AaronBeaudoin

I am not using cloudflare, but I had a similar problem with SSR and PM2, where images were not loading. I installed sharp as a dev dependency (e.g. yarn add -D sharp) and after build it worked.

shayr1 avatar Oct 31 '23 04:10 shayr1

Same problem

rahulkumarsingh73690 avatar Nov 18 '23 15:11 rahulkumarsingh73690

same here with netlify !

cfab avatar Dec 08 '23 09:12 cfab

Any updates about this bug?

Bjornftw avatar Dec 08 '23 17:12 Bjornftw

Just using the workaround suggested by @AaronBeaudoin at the moment.

Would love to contribute but I'm afraid this is a bit ambitious for a first PR here for me. Could be a bug in nuxt-image, but I can't rule out ipx or even nitro.

Some insights / attention from the nuxt team would be really great, the reproduction speaks for itself I think.

adamskyle avatar Dec 09 '23 14:12 adamskyle

Same here. If I have a <NuxtImg provider="cloudflare">, it resizes using Cloudflare fine, but if I have a <NuxtImg provider="ipx">, it does not work. The HTML renders out a path to /_ipx/..., but the image is 404. It works perfectly fine locally - it just breaks when deployed on Cloudflare pages.

nathanchase avatar Dec 29 '23 16:12 nathanchase

FWIW, I believe this is a Sharp limitation: https://github.com/lovell/sharp/issues/2863

nathanchase avatar Dec 29 '23 16:12 nathanchase

Looks like it is indeed sharp (or unjs/ipx which uses sharp) See also https://github.com/unjs/ipx/pull/190 where it is also noted as following: It is still Node.js only. Workers without thread support do not work!

CF Workers and Pages seems to be single threaded.

DavidDeSloovere avatar Dec 30 '23 14:12 DavidDeSloovere

I unexpectedly ran into the same issue when I tried to deploy a page on GitHub pages

youkei-zzz avatar Feb 01 '24 13:02 youkei-zzz

@nathanchase What's your workaround like with the provider="cloudflare" ? Or is it the same as https://github.com/nuxt/image/issues/1061#issuecomment-1786093586 where you need to upload the images to cloudflare separately?

DavidDeSloovere avatar Feb 02 '24 15:02 DavidDeSloovere

@nathanchase What's your workaround like with the provider="cloudflare" ? Or is it the same as #1061 (comment) where you need to upload the images to cloudflare separately?

My workaround at the moment is to not use ipx in production if deployed to Cloudflare Pages, but use some other image provider (like Cloudflare themselves, or weserv, or imagekit, or any others that do the processing elsewhere).

nathanchase avatar Feb 02 '24 17:02 nathanchase

Doesn't the following comment solve an issue for you?

Fifciu avatar Mar 17 '24 19:03 Fifciu

I'm also experiencing this issue, I would like to not build an entirely static website and want to benefits of ipx pre-rendered images. I think I found a temporary solution for it. Following the documentation of this page https://image.nuxt.com/advanced/static-images, it seems that if the route of the images are declared as pre-rendered it will work. I found a way to autogenerate them by using this configuration in nuxt.config.ts

{
    image: {
        provider: 'ipxStatic',
    },
    modules: [
        "@nuxt/image",
        "nitro-cloudflare-dev",
    ],
    nitro: {
        preset: "cloudflare-pages",
        prerender: {
            routes: ['/'],
            crawlLinks: true,
        },
    },
}

By defining ipxStatic as a provider, it forces @nuxt/image to create the pages for the images and with crawlLinks: true and a sensible routes configuration, it will crawl all the images and pre-render them.

It would be great that we could have a config that just automatically tags the ipx images as pre-rendered but that's all I found for now.

jbgosselin avatar Apr 10 '24 04:04 jbgosselin

has anyone come up with a solution for this?

mattgrah-am avatar May 05 '24 11:05 mattgrah-am

Any update? Even Cloudflare workers paid plan doesn't help.

rahulkumarsingh73690 avatar May 25 '24 13:05 rahulkumarsingh73690

I'm having the same problem. Already tried all the suggested fixes but nothing worked.

zonistefano avatar May 26 '24 11:05 zonistefano

Looks like it is indeed sharp (or unjs/ipx which uses sharp) See also unjs/ipx#190 where it is also noted as following: It is still Node.js only. Workers without thread support do not work!

CF Workers and Pages seems to be single threaded.

@cf-wasm/photon might work as replacement for IPX on workers

DavidDeSloovere avatar Jun 06 '24 06:06 DavidDeSloovere

Same issue here. Hopefully someone finds a solution.

duncandejong avatar Jul 12 '24 23:07 duncandejong

I managed to solve the problem by changing the build command from npm run build to npm run generate.

If you are building a static site using nuxt generate, Nuxt Image will optimize and save your images locally when your site is generated - and deploy them alongside your generated pages. Nuxt Images

nuxt ^3.12.4 @nuxt/image ^1.7.0

My project is a simple blog using @nuxt/content.

andreyquerino avatar Aug 09 '24 19:08 andreyquerino