Skip to content

[Cloudflare Adapter] SSR build generates incorrect import paths - files in dist/_astro but worker imports from dist/_worker.js/_astro #14945

@jroth55

Description

@jroth55

Astro Info

Astro: 5.16.3
@astrojs/cloudflare: 12.6.3
Node: v22.x
Package Manager: pnpm 10.11.0

Describe the Bug

When building an Astro project with the Cloudflare adapter in hybrid mode, the generated dist/_worker.js/index.js contains import paths that reference files in dist/_worker.js/_astro/, but the actual files are generated in dist/_astro/.

Example of the incorrect import in dist/_worker.js/index.js:

import { renderers } from './_astro/[email protected]';

Actual file location:

dist/_astro/[email protected]

Expected file location (per the import):

dist/_worker.js/_astro/[email protected]

This causes deployment to Cloudflare Pages to fail with module resolution errors.

Steps to Reproduce

  1. Create a new Astro project with hybrid output mode
  2. Add @astrojs/cloudflare adapter with config:
// astro.config.mjs
import { defineConfig } from 'astro/config';
import cloudflare from '@astrojs/cloudflare';

export default defineConfig({
  output: 'hybrid',
  adapter: cloudflare({
    platformProxy: { enabled: true },
    routes: { strategy: 'auto' }
  })
});
  1. Run pnpm build
  2. Observe that dist/_worker.js/index.js imports from ./_astro/ path
  3. Observe that actual files are in dist/_astro/ not dist/_worker.js/_astro/
  4. Deploy with wrangler pages deploy dist fails with: Could not resolve "./_astro/_@astrojs-ssr-adapter..."

Expected Behavior

Either:

  1. The import paths in dist/_worker.js/index.js should reference ../_astro/ (going up one level), OR
  2. The SSR adapter files should be copied/generated into dist/_worker.js/_astro/

Current Workaround

We use a post-build script that copies files:

// fix-worker-imports.mjs
import { cpSync, existsSync } from 'fs';
import { join } from 'path';

const distDir = './dist';
const astroDir = join(distDir, '_astro');
const workerAstroDir = join(distDir, '_worker.js', '_astro');

if (existsSync(astroDir)) {
  cpSync(astroDir, workerAstroDir, { recursive: true });
  console.log('Copied _astro to _worker.js/_astro');
}

Then in package.json:

"build": "astro build && node fix-worker-imports.mjs"

Environment

  • OS: macOS
  • Node: v22.x
  • pnpm: 10.11.0
  • Deployment: Cloudflare Pages

Additional Context

This bug was also reported in #13163 but was closed as needs-repro. This issue provides a clear reproduction and workaround. This is a critical bug that prevents deployment to Cloudflare Pages without workarounds.

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageIssue needs to be triaged

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions