Skip to content

<Image> in SSR page response 404 when deploy Cloudflare Works #13825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
sgalcheung opened this issue May 19, 2025 · 9 comments
Open
1 task done

<Image> in SSR page response 404 when deploy Cloudflare Works #13825

sgalcheung opened this issue May 19, 2025 · 9 comments
Labels
needs triage Issue needs to be triaged pkg: cloudflare Related to the Cloudflare adapter

Comments

@sgalcheung
Copy link

sgalcheung commented May 19, 2025

Astro Info

Astro                    v5.6.1
Node                     v18.20.3
System                   macOS (x64)
Package Manager          pnpm
Output                   static
Adapter                  @astrojs/cloudflare
Integrations             astro-icon
                         set-prerender

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

The image configuration is here

image: {
    service: passthroughImageService(),
  },
  adapter: cloudflare({
    imageService: "passthrough",
  }),

The image display is right in the dev.
Deploy page also works well: https://develop.balanced-chef.pages.dev/
But have trouble with CF works: https://balanced-chef.sgalwork.workers.dev/

for example fetch image: howm.Dr0NQRDu.jpg
The image URL is here: https://balanced-chef.sgalwork.workers.dev/_astro/howm.Dr0NQRDu.jpg
But _image endpoint return Not Found 404

What's the expected result?

Resolved the image.

Link to Minimal Reproducible Example

https://github.com/sgalcheung/balanced-chef

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label May 19, 2025
@ascorbic
Copy link
Contributor

Hi. Can you provide a minimal repro. Using a full site like the one you provided makes it hard for us to isolate the source of a problem.

@ascorbic ascorbic added the needs repro Issue needs a reproduction label May 20, 2025
Copy link
Contributor

Hello @sgalcheung. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with needs repro will be closed if they have no activity within 3 days.

@github-actions github-actions bot removed the needs triage Issue needs to be triaged label May 20, 2025
@sgalcheung
Copy link
Author

I tried this React demo, still have this problem.

I also created an issue to Cloudflare Support: https://community.cloudflare.com/t/cant-fetch-self-assert-in-workers/800037

This fetch method is here:

const res = await fetch(src, {

We try to fetch the image(https://balanced-chef.sgalwork.workers.dev/_astro/howm.Dr0NQRDu.jpg), but this is not successful. I guess this is a CF gateway problem; they can't fetch the self-hosted app assert.

@sgalcheung
Copy link
Author

sgalcheung commented May 22, 2025

@ascorbic But this seems to be a Cloudflare Workers design feature; they need to use the env.ASSET.fetch() method instead. @alexanderniebuhr

export default {
  async fetch(request, env) {
    // Wrong approach: Direct access via URL
    // const response = await fetch('https://your-worker.workers.dev/asset.png');
    
    // Correct approach: Use env.ASSET.fetch()
    const response = await env.ASSET.fetch(new Request('https://your-worker.workers.dev/asset.png'));
    
    return response;
  }
}

Therefore, we shouldn't use passthroughImageService; we should use a custom handler with env.ASSET.fetch().

@alexanderniebuhr
Copy link
Member

I need to look at the example to fully understand the issue, but using internal bindings is always the better approach anyways.

@sgalcheung
Copy link
Author

Hello, I added a mini-repo: astro-cloudflare-workers-demo.

@ascorbic ascorbic added pkg: cloudflare Related to the Cloudflare adapter and removed needs repro Issue needs a reproduction labels May 22, 2025
@sgalcheung
Copy link
Author

sgalcheung commented May 25, 2025

I have just created a Cloudflare Astro framework demo using npm create cloudflare@latest, and I noticed the wrangler.jsonc file has a flag global_fetch_strictly_public in compatibility_flags, I reference this doc:

When the global_fetch_strictly_public compatibility flag is enabled, the global fetch() function will strictly route requests as if they were made on the public Internet.

This means requests to a Worker's own zone will loop back to the "front door" of Cloudflare and will be treated like a request from the Internet, possibly even looping back to the same Worker again.

When the global_fetch_strictly_public is not enabled, such requests are routed to the zone's origin server, ignoring any Workers mapped to the URL and also bypassing Cloudflare security settings.

reference Global fetch() strictly public

Then, I added the flag to astro-cloudflare-workers-demo, now it works well at /hello.

Enable this feature, and the app will force all fetch() calls to go through the public network, which ensures security but may introduce loops or delays.

Or should we write code Astro.locals.runtime.env.ASSETS.fetch(request.url); for passthrough?

@alexanderniebuhr
Copy link
Member

Interesting. That flag seems to be new. I still haven't had time to look into your code, sorry.

@Baraban511
Copy link

I had the same problem before adding the flag global_fetch_strictly_public. It looks like prerendered images /_astro/ works correctly, but on-demand rendered images /_images doesn't. Even for cloudflare own image resizing tool it wasn't woking (404).
I've seen the 1042 error code referring to same worker fetching on on-demand rendered images. Any progress?

@florian-lefebvre florian-lefebvre added the needs triage Issue needs to be triaged label May 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Issue needs to be triaged pkg: cloudflare Related to the Cloudflare adapter
Projects
None yet
Development

No branches or pull requests

5 participants