Skip to content

import { env } from "cloudflare:workers" fails with Node prerendering and any dynamic prerendered routes #16553

@thomas-callahan-collibra

Description

Astro Info

Astro                    v6.2.1
Node                     v24.11.1
System                   macOS (arm64)
Package Manager          pnpm
Output                   static
Adapter                  @astrojs/cloudflare
Integrations             none

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

No response

Describe the Bug

import { env } from "cloudflare:workers" fails under the following conditions:

  1. output: "static"
  2. Cloudflare prerenderEnvironment: "node"
  3. Any route exists that uses getStaticPaths to generate routes at build time with a route file using the [path].astro pattern.

If all routes are dynamic (server mode for the entire project, or all routes use export const prerender = false) OR fully static including the path (route files like filename.astro with no dynamic route parameter), then it works.

My test case (and where I encountered this in my production projects) is a file endpoint, but I believe the same is true when using this import in regular Astro files.

I cannot use workerd for prerendering -- I have several projects running into this issue and all of them fail in different ways when doing that, in ways that would be impossible or very difficult to change (i.e. our CMS's JS SDK has issues with workerd).

With prerender = true (default) in [page].astro, [test].txt.ts fails with this error:

Image

With prerender = false in [page].astro, [test].txt.ts renders the expected content:

Image

To trigger this behavior in the MRE, start the dev server and visit http://localhost:4567/foo.txt, then uncomment line 2 in /pages/[page].astro and HMR will reload foo.txt showing the error. This happens in a deployed Worker, too.

What's the expected result?

Non-prerendered routes should still work with cloudflare:workers if there are dynamic prerendered routes in the project.

I've tried many (many) things, with no luck, including:

  1. Using dynamic imports inside the file endpoint's GET method (i.e. await import("cloudflare:workers").
  2. Flipping the logic and running in server mode with specific routes opted out using export const prerender = true.
  3. Moving this import to various places and passing env to functions as arguments.

During my troubleshooting I found, but cannot find again now to include here, a bug report with a very similar description, that suggested the two workarounds above, neither of which works in this case. That report said something about having any prerendered dynamic routes changed how the entire project's dependencies were handled, meaning that virtual modules like cloudflare:workers cannot be used, which seems to match this problem. But I'm still able to use other virtual modules like astro:env.

Link to Minimal Reproducible Example

https://github.com/thomas-callahan-collibra/astro-blog-starter-template

Participation

  • I am willing to submit a pull request for this issue.

Metadata

Metadata

Assignees

Labels

- P4: importantViolate documented behavior or significantly impacts performance (priority)pkg: astroRelated to the core `astro` package (scope)pkg: cloudflareRelated to the Cloudflare adapter

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions