Skip to content

fix(worker): inline NEXT_DEPLOYMENT_ID into worker bundles - #3241

Open
AhmedElBanna80 wants to merge 1 commit into
cloudflare:mainfrom
AhmedElBanna80:fix/worker-deployment-id-define
Open

fix(worker): inline NEXT_DEPLOYMENT_ID into worker bundles#3241
AhmedElBanna80 wants to merge 1 commit into
cloudflare:mainfrom
AhmedElBanna80:fix/worker-deployment-id-define

Conversation

@AhmedElBanna80

Copy link
Copy Markdown

Problem

process.env.NEXT_DEPLOYMENT_ID and process.env.__VINEXT_DEPLOYMENT_ID are set as top-level Vite defines, and the comment there explicitly intends worker code to read process.env.NEXT_DEPLOYMENT_ID. But worker builds run in their own plugin container that the top-level defines don't reach — as the sibling createWorkerImageImportsPlugin already notes ("Worker builds use their own plugin container, so the top-level … plugin cannot perform this transform"). So a worker that reads process.env.NEXT_DEPLOYMENT_ID sees an un-replaced member access → null/undefined.

Surfaced by the Next.js e2e fixture test/e2e/app-dir/worker (NEXT_DEPLOYMENT_ID null inside the worker).

Fix

Add a worker-scoped define plugin (createWorkerDeploymentIdDefinePlugin) registered in worker.plugins alongside the existing worker-image plugin. It inlines both identifiers into worker scripts, mirroring the top-level define values for parity (NEXT_DEPLOYMENT_ID → the string or false; __VINEXT_DEPLOYMENT_ID → the string or ""). It is a no-op where the identifiers are absent, and harmless if the top-level define already reaches workers (the filter simply won't match a value that's already replaced).

packages/vinext/src/plugins/worker-image-imports.ts (the plugin, beside its sibling) + registration in packages/vinext/src/index.ts.

Test

tests/worker-deployment-id-define.test.ts exercises the transform directly: inlines a configured id, inlines false when unset (Next.js parity), handles the internal identifier, and is a no-op when the id is never read. Mutation-proved (neutering the replacement reds the positive cases).

Scope

This fixes the NEXT_DEPLOYMENT_ID-is-null half of the fixture. The ?dpl= skew query on the worker script URL is a separate asset-URL concern (renderBuiltUrl/assetPrefix) and is not addressed here.

`process.env.NEXT_DEPLOYMENT_ID` and `process.env.__VINEXT_DEPLOYMENT_ID` are set
as top-level Vite `define`s, and the comment there explicitly intends worker code
to read `process.env.NEXT_DEPLOYMENT_ID`. But worker builds run in their own plugin
container that the top-level defines don't reach (the sibling
`createWorkerImageImportsPlugin` exists for exactly this reason), so a worker that
reads `process.env.NEXT_DEPLOYMENT_ID` sees an un-replaced member access → null.

Add a worker-scoped define plugin that inlines both identifiers into worker scripts,
mirroring the top-level define values for parity (`NEXT_DEPLOYMENT_ID` → the string
or `false`; `__VINEXT_DEPLOYMENT_ID` → the string or `""`). It's a no-op where the
identifiers are absent, and safe if the top-level define already reaches workers.

Fixes the `NEXT_DEPLOYMENT_ID`-is-null half of the `app-dir/worker` e2e fixture.
(The `?dpl=` skew query on the worker *script* URL is a separate asset-URL concern.)

Test: tests/worker-deployment-id-define.test.ts exercises the transform directly —
inlines the configured id, inlines `false` when unset, handles the internal ident,
and is a no-op when the id is never read.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants