Skip to content

Commit 29af6da

Browse files
authored
Fix FetchState manifest resolution in development (#17812)
1 parent d38ed60 commit 29af6da

3 files changed

Lines changed: 53 additions & 1 deletion

File tree

.changeset/calm-maps-travel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes a bug where `new FetchState(request)` could fail in development when server dependencies were optimized

packages/astro/src/manifest/serialized.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,24 @@ export function serializedManifestPlugin({
7777
return {
7878
name: SERIALIZED_MANIFEST_ID,
7979
enforce: 'pre',
80+
// Dependency optimization runs as a nested Rolldown build that cannot load Vite virtual
81+
// modules. Keep this import external so the server module graph resolves it below.
82+
configEnvironment(environmentName) {
83+
if (
84+
command === 'dev' &&
85+
(environmentName === ASTRO_VITE_ENVIRONMENT_NAMES.astro ||
86+
environmentName === ASTRO_VITE_ENVIRONMENT_NAMES.ssr ||
87+
environmentName === ASTRO_VITE_ENVIRONMENT_NAMES.prerender)
88+
) {
89+
return {
90+
optimizeDeps: {
91+
rolldownOptions: {
92+
external: [AMBIENT_MANIFEST_SPECIFIER],
93+
},
94+
},
95+
};
96+
}
97+
},
8098
configureServer(server) {
8199
server.watcher.on('add', (path) => reloadManifest(path, server));
82100
server.watcher.on('unlink', (path) => reloadManifest(path, server));

packages/integrations/cloudflare/test/custom-entryfile-fetch-state.test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as assert from 'node:assert/strict';
2+
import { rmSync } from 'node:fs';
23
import { after, before, describe, it } from 'node:test';
3-
import { type Fixture, loadFixture, type PreviewServer } from './test-utils.ts';
4+
import { fileURLToPath } from 'node:url';
5+
import { type DevServer, type Fixture, loadFixture, type PreviewServer } from './test-utils.ts';
46

57
// Regression test for https://github.com/withastro/astro/issues/17591:
68
// a custom worker entryfile that builds its own request state with
@@ -38,3 +40,30 @@ describe('Custom entry file using astro/fetch', () => {
3840
);
3941
});
4042
});
43+
44+
describe('Custom entry file using astro/fetch in dev', () => {
45+
let fixture: Fixture;
46+
let devServer: DevServer;
47+
48+
before(async () => {
49+
fixture = await loadFixture({
50+
root: './fixtures/custom-entryfile-fetch-state/',
51+
});
52+
// Optimizer plugins are not included in Vite's cache key.
53+
const viteCacheDir = new URL('./node_modules/.vite/', fixture.config.root);
54+
rmSync(fileURLToPath(viteCacheDir), { recursive: true, force: true });
55+
devServer = await fixture.startDevServer();
56+
});
57+
58+
after(async () => {
59+
await devServer.stop();
60+
});
61+
62+
it('renders an SSR page from a state built with new FetchState(request)', async () => {
63+
const response = await fixture.fetch('/');
64+
const html = await response.text();
65+
assert.equal(response.status, 200, html);
66+
assert.equal(response.headers.get('X-Fetch-State-Entrypoint'), 'true');
67+
assert.match(html, /astro-cloudflare-custom-entryfile-fetch-state/);
68+
});
69+
});

0 commit comments

Comments
 (0)