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/persist-session-delete-without-prior-mutation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes session persistence when `session.delete()` is the first mutation in a request (no prior `get`, `set`, `has`, or `keys`). The session was marked dirty in memory, but persistence skipped the save because `#data` stayed `undefined`, so the backing store could still return the deleted key on the next request.
7 changes: 4 additions & 3 deletions packages/astro/e2e/view-transitions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ test.afterAll(async () => {

function collectLoads(page: Page) {
const loads: string[] = [];
page.on('load', async () => {
// Push synchronously; an async handler awaiting `page.title()` races with assertions (often on Firefox).
page.on('load', () => {
const url = page.url();
if (url !== 'about:blank') loads.push(await page.title());
if (url !== 'about:blank') loads.push(url);
});
return loads;
}
Expand Down Expand Up @@ -147,7 +148,7 @@ test.describe('View Transitions', () => {

expect(
loads.length,
'There should be 2 page loads. The original, then going from 3 to 2',
'There should be 2 page loads. The initial navigation, then the full navigation from page 1 to page 3',
).toEqual(2);
});

Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/core/session/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ export class AstroSession {
* Deletes a session value.
*/
delete(key: string) {
this.#data?.delete(key);
this.#data ??= new Map();
this.#data.delete(key);
if (this.#partial) {
this.#toDelete.add(key);
}
Expand Down
30 changes: 28 additions & 2 deletions packages/astro/test/units/sessions/astro-session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,15 +385,41 @@ describe('AstroSession - Sparse Data Operations', () => {
session.delete('key');
await session[PERSIST_SYMBOL]();

// Create a new session using the stored data
// Create a new session using the stored data (get must return parsed JSON like unstorage)
const newSession = createSession(defaultConfig, defaultMockCookies, {
get: async () => storedData,
get: async () => (storedData != null ? JSON.parse(storedData) : null),
setItem: async () => {},
} as unknown as Storage);

assert.equal(await newSession.get('key'), undefined);
});

it('should persist delete as the first mutation (no prior get/set)', async () => {
const store = new Map<string, string>();
const sessionId = 'sessionid';
store.set(sessionId, devalueStringify(new Map([['token', { data: 'secret' }]])));

const mockStorage = {
get: async (key: string) => {
const raw = store.get(key);
return raw ? JSON.parse(raw) : null;
},
setItem: async (key: string, value: string) => {
store.set(key, value);
},
removeItem: async (key: string) => {
store.delete(key);
},
} as unknown as Storage;

const session = createSession(defaultConfig, defaultMockCookies, mockStorage);
session.delete('token');
await session[PERSIST_SYMBOL]();

const newSession = createSession(defaultConfig, defaultMockCookies, mockStorage);
assert.equal(await newSession.get('token'), undefined);
});

it('should update existing values in sparse mode', async () => {
const existingData = stringify(new Map([['key', 'old']]));
const mockStorage = {
Expand Down
1 change: 0 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading