Skip to content

Commit 750b4db

Browse files
authored
Pre-bundle renderer server entrypoints and the default logger to stop Cloudflare dev crashes (#17945)
* fix(cloudflare): pre-bundle renderer server entrypoints and default logger in optimizeDeps Renderer server entrypoints (e.g. @astrojs/svelte/server.js) are imported lazily through virtual:astro:renderers, which the dep optimizer scan cannot reach. On Cloudflare (workerd), discovering them mid-request triggers a re-optimization that rewrites the optimized dep cache while workerd still references the old chunks, crashing the dev server with a 'file does not exist' error. Pre-include them for all server environments in vite-plugin-environment (mirroring how client entrypoints are handled in container.ts), and add astro/logger/console to the Cloudflare adapter's include list, since the assets runtime logger setup always imports it. Fixes #17921 * fix(cloudflare): type updateConfig param in optimizeDeps include test Annotate the updateConfig callback parameter with Parameters<HookParameters<'astro:config:setup'>['updateConfig']>[0] to fix TS7006 (implicit any) that failed the cloudflare package build in CI. * fix(astro): scope renderer server entrypoint pre-include to the Cloudflare adapter Pre-including every renderer server entrypoint in optimizeDeps for all server environments regressed Node dev: pre-bundling renderers that are served through the transform pipeline duplicates framework modules (duplicate React 'invalid hook call' in view-transitions, @astrojs/vue SSR/dev failures). The Cloudflare adapter is the only runtime that pre-bundles the entire server graph and cannot tolerate a mid-request renderer discovery, so gate the pre-include on settings.adapter.name === '@astrojs/cloudflare'. The adapter cannot read settings.renderers from its hooks, hence the gate lives in core. * fix(cloudflare): pre-include renderer server entrypoints from the adapter, not core Reverts the renderer pre-include in vite-plugin-environment, which special-cased the Cloudflare adapter by name in core. The adapter now adds every renderer's serverEntrypoint to its own server-environment optimizeDeps.include, captured from the renderers exposed by a new generic astro:config:done hook param (additive, adapter-agnostic). Renderers are only imported lazily through virtual:astro:renderers, so the optimizer scan cannot reach them; pre-bundling them up front keeps the optimizer from re-running mid-request, when workerd still references the previous bundle. Fixes #17921 * fix(cloudflare): drop the core adapter-name gate; scope renderer pre-include to workerd envs Commit the vite-plugin-environment revert that removes the Cloudflare adapter-name gate from core (the previous commit left it in HEAD, so the PR diff still special-cased an adapter in core). The adapter now owns the whole renderer pre-include: it adds each renderer's serverEntrypoint to its server-environment optimizeDeps.include, captured from the renderers exposed by astro:config:done (passed as a readonly snapshot so integrations cannot mutate Astro's internal list). The entries are skipped for the prerender environment when prerenderEnvironment is 'node', where pre-bundling renderers would duplicate framework modules; only workerd environments get them. Fixes #17921 * fix(cloudflare): use a platform-agnostic file URL in the optimizeDeps test The URL-form renderer used a Unix-only file:///tmp/... URL, which made the adapter's fileURLToPath throw ERR_INVALID_FILE_URL_PATH on Windows and fail the integrations Windows CI job. Build the URL from import.meta.url so the path is absolute on every platform. * fix(cloudflare): pre-bundle the default console logger in optimizeDeps Per maintainer review: drop the renderer server entrypoint pre-include and the astro:config:done renderers param it depended on (a public hook change), and drop the adapter unit tests. The adapter now only pre-bundles astro/logger/console, which the assets runtime logger setup always imports on the server but which was missing from the include list; a late discovery would re-run the optimizer mid-request and crash workerd. Fixes #17921 * fix(cloudflare): pre-bundle renderer server entrypoints via an internal plugin channel Restores the renderer server entrypoint pre-include that was removed when the astro:config:done renderers param was dropped. Instead of the public hook change, the adapter reads the registered renderers through an internal, undocumented channel: vite-plugin-renderers now exposes them on its plugin object, and the adapter's @astrojs/cloudflare:environment plugin reads them in its Vite config hook (which runs before configEnvironment, so the entries are available when the server environments' optimizeDeps.include is assembled). Renderers are pre-bundled alongside astro/logger/console, skipped for the Node prerender environment. Fixes #17921 * fix(cloudflare): rename vite config hook param to avoid shadowing The config hook's parameter shadowed the astro:config:setup hook's config parameter, failing @typescript-eslint/no-shadow in the Lint job. Rename it to viteConfig; the internal plugin-channel mechanism is unchanged.
1 parent dbbf10e commit 750b4db

3 files changed

Lines changed: 37 additions & 1 deletion

File tree

‎.changeset/wide-sheep-kick.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'astro': patch
3+
'@astrojs/cloudflare': patch
4+
---
5+
6+
Pre-bundles renderer server entrypoints and the default console logger during dev so they are included in the initial optimization pass, preventing a mid-request re-optimization that could crash the dev server on Cloudflare (workerd).

‎packages/astro/src/vite-plugin-renderers/index.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ export default function vitePluginRenderers(options: PluginOptions): VitePlugin
2020
return {
2121
name: 'astro:plugin-renderers',
2222
enforce: 'pre',
23+
// Internal, undocumented: lets adapters (e.g. @astrojs/cloudflare) read the
24+
// registered renderers from the plugin chain to pre-bundle their server
25+
// entrypoints (see https://github.com/withastro/astro/issues/17921).
26+
renderers,
2327

2428
resolveId: {
2529
filter: {
@@ -68,5 +72,5 @@ export default function vitePluginRenderers(options: PluginOptions): VitePlugin
6872
return { code: `export const renderers = [];` };
6973
},
7074
},
71-
};
75+
} as VitePlugin & { renderers: typeof renderers };
7276
}

‎packages/integrations/cloudflare/src/index.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ export default function createIntegration({
142142
let _buildOutput: 'server' | 'static';
143143
let _originalClientDir: URL;
144144

145+
// Renderer server entrypoints (e.g. `@astrojs/svelte/server.js`), read from
146+
// the `astro:plugin-renderers` plugin in the adapter's Vite `config` hook.
147+
// Pre-bundling them up front keeps the dep optimizer from re-running
148+
// mid-request, when workerd still references the previous bundle (#17921).
149+
let rendererServerEntries: string[] = [];
150+
145151
let _routes: IntegrationResolvedRoute[];
146152
let cfPluginConfig: PluginConfig;
147153
let hasUserBuildImageService = false;
@@ -326,6 +332,17 @@ export default function createIntegration({
326332
},
327333
{
328334
name: '@astrojs/cloudflare:environment',
335+
config(viteConfig) {
336+
const renderersPlugin = (viteConfig.plugins as any[])?.find(
337+
(plugin) => plugin?.name === 'astro:plugin-renderers',
338+
);
339+
rendererServerEntries = (renderersPlugin?.renderers ?? []).map(
340+
(renderer: { serverEntrypoint: string | URL }) =>
341+
typeof renderer.serverEntrypoint === 'string'
342+
? renderer.serverEntrypoint
343+
: fileURLToPath(renderer.serverEntrypoint),
344+
);
345+
},
329346
configEnvironment(environmentName, _options) {
330347
// Skip dependency pre-bundling during type generation (see `isTypeGenPhase` above).
331348
if (isTypeGenPhase) {
@@ -335,6 +352,11 @@ export default function createIntegration({
335352
environmentName,
336353
);
337354
if (isServerEnvironment && !_options.optimizeDeps?.noDiscovery) {
355+
// The prerender environment runs on Node when `prerenderEnvironment:
356+
// 'node'`, where pre-bundling renderers would duplicate framework
357+
// modules; only the workerd environments get the renderer entries.
358+
const isNodePrerender =
359+
prerenderEnvironment === 'node' && environmentName === 'prerender';
338360
return {
339361
optimizeDeps: {
340362
include: [
@@ -371,6 +393,10 @@ export default function createIntegration({
371393
...(prebundleContentRuntime ? (['astro/content/runtime'] as const) : []),
372394
'astro/compiler-runtime',
373395
'astro/jsx-runtime',
396+
...(isNodePrerender ? [] : rendererServerEntries),
397+
// Pre-bundled so a late discovery can't trigger a mid-request
398+
// re-optimization (https://github.com/withastro/astro/issues/17921).
399+
'astro/logger/console',
374400
...(config.logger?.entrypoint === 'astro/logger/json'
375401
? ['astro/logger/json']
376402
: []),

0 commit comments

Comments
 (0)