Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ten-parks-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes sessions breaking in dev mode with the Cloudflare adapter when middleware is present
1 change: 1 addition & 0 deletions packages/astro/dev-only.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ declare module 'virtual:astro:app' {
declare module 'virtual:astro:fetchable' {
const fetchable: { fetch: import('./src/core/fetch/types.js').FetchHandler };
export default fetchable;
export const isDefaultFetchHandler: true | undefined;
}

declare module 'virtual:astro:get-image' {
Expand Down
9 changes: 7 additions & 2 deletions packages/astro/src/core/app/entrypoints/virtual/dev.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fetchable from 'virtual:astro:fetchable';
import fetchable, { isDefaultFetchHandler } from 'virtual:astro:fetchable';
import { manifest } from 'virtual:astro:manifest';
import { clearActions } from '../../../../actions/load.js';
import { createNonRunnableEnvironment } from '../../../environment/dev-nonrunnable.js';
Expand All @@ -24,7 +24,12 @@ export const createApp: CreateApp = ({ streaming } = {}) => {
setLogger(manifest, createConsoleLogger({ level: manifest.logLevel }));
setEnvironment(manifest, createNonRunnableEnvironment());
const app = new DevFacadeApp(manifest, streaming);
app.setFetchHandler(fetchable);
// Keep the facade's own DefaultFetchHandler when the virtual module is the
// built-in fallback: `instanceof` can't recognize the fallback in dev, where
// its class identity is unstable. https://github.com/withastro/astro/issues/17927
if (!isDefaultFetchHandler) {
app.setFetchHandler(fetchable);
}

// The HMR listeners target the MANIFEST via the functional core: one
// atomic route-table replacement is visible to every consumer — matcher,
Expand Down
39 changes: 38 additions & 1 deletion packages/integrations/cloudflare/test/sessions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import * as devalue from 'devalue';
import cloudflare from '../dist/index.js';
import { type Fixture, loadFixture, type PreviewServer } from './test-utils.ts';
import { type DevServer, type Fixture, loadFixture, type PreviewServer } from './test-utils.ts';
import type { AstroInlineConfig } from 'astro';

describe('sessions', () => {
Expand Down Expand Up @@ -84,6 +84,43 @@ describe('sessions', () => {
});
});

describe('sessions in dev mode with middleware', () => {
let fixture: Fixture;
let devServer: DevServer;
before(async () => {
fixture = await loadFixture({
root: './fixtures/sessions/',
});
devServer = await fixture.startDevServer();
// Prime preloading
await fixture.fetch('/');
});

after(async () => {
await devServer.stop();
});

it('can write and read session data when middleware is present', async () => {
const firstResponse = await fixture.fetch('/update', { method: 'GET' });
assert.equal(firstResponse.status, 200, 'Expected 200 OK for session write');
const firstValue = await firstResponse.json();
assert.equal(firstValue.previousValue, 'none');

const firstHeaders = firstResponse.headers.get('set-cookie')!.split(',');
const firstSessionId = firstHeaders[0].split(';')[0].split('=')[1];

const secondResponse = await fixture.fetch('/update', {
method: 'GET',
headers: {
cookie: `astro-session=${firstSessionId}`,
},
});
assert.equal(secondResponse.status, 200, 'Expected 200 OK for session read');
const secondValue = await secondResponse.json();
assert.equal(secondValue.previousValue, 'expected');
});
});

describe('sessions with custom binding name', () => {
let fixture: Fixture;
before(async () => {
Expand Down
Loading