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
7 changes: 7 additions & 0 deletions .changeset/fix-session-regenerate-dirty.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'astro': patch
---

Fixes a bug that caused `session.regenerate()` to silently lose session data

Previously, regenerated session data was not saved under the new session ID unless `set()` was also called.
1 change: 1 addition & 0 deletions packages/astro/src/core/session/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ export class AstroSession {
// Create new session
this.#sessionID = crypto.randomUUID();
this.#data = data;
this.#dirty = true;
await this.#setCookie();

// Clean up old session asynchronously
Expand Down
33 changes: 33 additions & 0 deletions packages/astro/test/units/sessions/astro-session.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,39 @@ describe('AstroSession - Session Regeneration', () => {

assert.notEqual(initialId, newId);
});

it('should persist data after regeneration without a subsequent set()', async () => {
const store = new Map();
const mockStorage = {
get: async (key) => {
const raw = store.get(key);
return raw ? JSON.parse(raw) : null;
},
setItem: async (key, value) => {
store.set(key, value);
},
removeItem: async (key) => {
store.delete(key);
},
};

const session = createSession(defaultConfig, defaultMockCookies, mockStorage);

session.set('user', 'alice');
await session[PERSIST_SYMBOL]();

await session.regenerate();
await session[PERSIST_SYMBOL]();

// Create a new session that reads from the same storage
const newSession = createSession(defaultConfig, {
...defaultMockCookies,
get: () => ({ value: session.sessionID }),
}, mockStorage);

const value = await newSession.get('user');
assert.equal(value, 'alice');
});
});

describe('AstroSession - Data Persistence', () => {
Expand Down