diff --git a/.changeset/spotty-ladybugs-change.md b/.changeset/spotty-ladybugs-change.md new file mode 100644 index 00000000..8df19a67 --- /dev/null +++ b/.changeset/spotty-ladybugs-change.md @@ -0,0 +1,5 @@ +--- +'@blinkk/root': patch +--- + +fix: only update session cookie when changed diff --git a/packages/root/src/middleware/session.ts b/packages/root/src/middleware/session.ts index 9f6d97a0..83de2e29 100644 --- a/packages/root/src/middleware/session.ts +++ b/packages/root/src/middleware/session.ts @@ -26,6 +26,9 @@ export function sessionMiddleware(options?: SessionMiddlewareOptions) { req.session = session; res.session = session; res.saveSession = (saveSessionOptions?: SaveSessionOptions) => { + if (!session.hasChanges) { + return; + } // "secure" cookies require https, so disable "secure" when in development. const secureCookie = Boolean(process.env.NODE_ENV !== 'development'); const cookieValue = session.toString(); @@ -54,6 +57,7 @@ export function sessionMiddleware(options?: SessionMiddlewareOptions) { export class Session { private data: Record = {}; + hasChanges = false; constructor(data?: Record) { this.data = data || {}; @@ -82,10 +86,12 @@ export class Session { setItem(key: string, value: string) { this.data[key] = value; + this.hasChanges = true; } removeItem(key: string) { delete this.data[key]; + this.hasChanges = true; } toString(): string {