Skip to content

Add experimental prerenderWorker option#11697

Merged
jamesopstad merged 9 commits intomainfrom
james/prerender-worker
Jan 30, 2026
Merged

Add experimental prerenderWorker option#11697
jamesopstad merged 9 commits intomainfrom
james/prerender-worker

Conversation

@jamesopstad
Copy link
Copy Markdown
Contributor

@jamesopstad jamesopstad commented Dec 18, 2025

Adds an experimental prerenderWorker option for defining a dedicated Worker for pre-rendering.

Example:

cloudflare({
  experimental: {
    prerenderWorker: {
      config(_, { entryWorkerConfig }) {
        return {
          ...entryWorkerConfig,
          name: "prerender",
          main: "./src/prerender.ts",
        };
      },
    },
  },
}),

Internally, the plugin now sets an environment variable during the build that indicates to the preview server that the prerender Worker should be used as the entry. This means that the Vite preview server can be used during the build to send requests to the prerender Worker.


  • Tests
    • Tests included/updated
    • Tests not necessary because:
  • Public documentation
    • Cloudflare docs PR(s):
    • Documentation not necessary because: experimental and should be undocumented for now
  • Wrangler V3 Backport
    • Wrangler PR:
    • Not necessary because: not a Wrangler change

A picture of a cute animal (not mandatory, but encouraged)


Open with Devin

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Dec 18, 2025

🦋 Changeset detected

Latest commit: a740392

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Dec 18, 2025

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@11697

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@11697

miniflare

npm i https://pkg.pr.new/miniflare@11697

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@11697

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@11697

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@11697

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@11697

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@11697

@cloudflare/workers-utils

npm i https://pkg.pr.new/@cloudflare/workers-utils@11697

wrangler

npm i https://pkg.pr.new/wrangler@11697

commit: a740392

@matthewp
Copy link
Copy Markdown

We have this working in withastro/astro#15077

This change works for us.

@jamesopstad jamesopstad force-pushed the james/prerender-worker branch from 8033489 to 8e303f7 Compare January 30, 2026 11:50
@claude
Copy link
Copy Markdown
Contributor

claude Bot commented Jan 30, 2026

Claude finished @jamesopstad's task —— View job


Changeset Review

✅ All changesets look good

The changeset in .changeset/metal-planes-hammer.md:

  • Version Type: Correctly marked as minor for a new configuration option
  • Changelog Quality: Clear description with explicit note about experimental status
  • Markdown Headers: No h1/h2/h3 headers used
  • Experimental Features: Includes note that this is experimental and may change/be removed
  • Package: Correctly targets @cloudflare/vite-plugin

@jamesopstad jamesopstad force-pushed the james/prerender-worker branch from 8e303f7 to a6a6539 Compare January 30, 2026 14:03
@jamesopstad jamesopstad marked this pull request as ready for review January 30, 2026 14:03
@jamesopstad jamesopstad requested a review from a team as a code owner January 30, 2026 14:03
@@ -76,6 +79,8 @@ export function createBuildApp(
return;
}

// TODO: move static assets from the prerender environment to the client environment
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started working on this but it is quite complex and is not currently needed so I decided to leave it for now.

@@ -166,6 +166,7 @@ function readWorkerConfig(
};
}

// TODO: separate prerender Worker warnings from auxiliary Worker warnings
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can also be left for now I think as in Astro the concept of a pre-render Worker will be invisible to the user.

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

View issue and 5 additional flags in Devin Review.

Open in Devin Review

Comment thread packages/vite-plugin-cloudflare/src/context.ts
Comment on lines +312 to +315
// This environment variable is used to indicate to the preview server that it is being run during a build
// We need to delete it here as, during testing, preview also runs in the same process after the build completes
// eslint-disable-next-line turbo/no-undeclared-env-vars
delete process.env.CLOUDFLARE_VITE_BUILD;
Copy link
Copy Markdown
Member

@edmundhung edmundhung Jan 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just thinking out loud, would it be safer to clean up the env var through the vite plugin? Just in case our users run into the same problem?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to do that but I'm not sure how. Any suggestions?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't figure out a reliable way to clean it up in the right timing as well :(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A brittle way to handle this would be setting the env var only if prerenderWorker is configured, then monkey-patching previewServer.close() to clean that up. 😅

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that is an option. The downside is that it relies on the preview server only being run once for pre-rendering. That's probably a safe assumption to make but I'm not sure. Let's leave for now and revisit.

@github-project-automation github-project-automation Bot moved this from Untriaged to Approved in workers-sdk Jan 30, 2026
Copy link
Copy Markdown
Member

@edmundhung edmundhung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good. Just some nits.

Comment thread packages/vite-plugin-cloudflare/src/plugin-config.ts Outdated
Comment thread packages/vite-plugin-cloudflare/src/plugin-config.ts Outdated
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 new potential issue.

View issue and 9 additional flags in Devin Review.

Open in Devin Review

Comment thread packages/vite-plugin-cloudflare/src/build.ts
@jamesopstad jamesopstad force-pushed the james/prerender-worker branch from a12560b to a740392 Compare January 30, 2026 15:58
@jamesopstad jamesopstad merged commit 67a4ab1 into main Jan 30, 2026
55 of 59 checks passed
@jamesopstad jamesopstad deleted the james/prerender-worker branch January 30, 2026 17:34
@github-project-automation github-project-automation Bot moved this from Approved to Done in workers-sdk Jan 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants