diff --git a/.changeset/modern-rooms-cry.md b/.changeset/modern-rooms-cry.md new file mode 100644 index 000000000000..e7c983fcd9b6 --- /dev/null +++ b/.changeset/modern-rooms-cry.md @@ -0,0 +1,5 @@ +--- +'@astrojs/cloudflare': patch +--- + +Fixed a bug where a cascade of reloads would cause the page to crash during the first visit when building or developing with Cloudflare SSR in Astro v6 due to dependency loading issues. diff --git a/packages/integrations/cloudflare/src/esbuild-plugin-astro-frontmatter.ts b/packages/integrations/cloudflare/src/esbuild-plugin-astro-frontmatter.ts index 4a3e4039d67a..f9057dec69ad 100644 --- a/packages/integrations/cloudflare/src/esbuild-plugin-astro-frontmatter.ts +++ b/packages/integrations/cloudflare/src/esbuild-plugin-astro-frontmatter.ts @@ -1,4 +1,5 @@ import { readFile } from 'node:fs/promises'; +import { dirname, isAbsolute, resolve } from 'node:path'; import type { DepOptimizationConfig } from 'vite'; const FRONTMATTER_RE = /^---\r?\n([\s\S]*?)\r?\n---/; @@ -16,6 +17,8 @@ function replaceTopLevelReturns(code: string): string { }); } +const ASTRO_FRONTMATTER_NAMESPACE = 'astro-frontmatter'; + // Not exposed as a type from Vite, so need to grab this way. type ESBuildPlugin = NonNullable< NonNullable['plugins'] @@ -25,47 +28,61 @@ type ESBuildPlugin = NonNullable< * An esbuild plugin that extracts frontmatter from .astro files during * dependency optimization scanning. This allows Vite to discover imports * in the server-side frontmatter code. + * + * This plugin uses an `onResolve` handler to intercept `.astro` files before + * Vite's built-in `vite:dep-scan` plugin routes them to the `html` namespace. + * Without this, Vite's scanner only extracts ` + + diff --git a/packages/integrations/cloudflare/test/fixtures/dev-ssr-optimization/src/pages/index.astro b/packages/integrations/cloudflare/test/fixtures/dev-ssr-optimization/src/pages/index.astro new file mode 100644 index 000000000000..396d2d384b6d --- /dev/null +++ b/packages/integrations/cloudflare/test/fixtures/dev-ssr-optimization/src/pages/index.astro @@ -0,0 +1,13 @@ +--- +import { ClientRouter } from 'astro:transitions'; +--- + + + + Dev SSR Optimization Test + + + +
OK
+ + diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5f017934c0d5..a6624090c0cb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4591,6 +4591,19 @@ importers: specifier: workspace:* version: link:../../../../../astro + packages/integrations/cloudflare/test/fixtures/dev-ssr-optimization: + dependencies: + '@astrojs/cloudflare': + specifier: workspace:* + version: link:../../.. + astro: + specifier: workspace:* + version: link:../../../../../astro + devDependencies: + wrangler: + specifier: ^4.83.0 + version: 4.83.0(@cloudflare/workers-types@4.20260228.0) + packages/integrations/cloudflare/test/fixtures/external-image-service: dependencies: '@astrojs/cloudflare':