Skip to content

Commit

Permalink
fix: only update session cookie when changed (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenle authored Sep 18, 2024
1 parent 67d2882 commit f1293f1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-ladybugs-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@blinkk/root': patch
---

fix: only update session cookie when changed
6 changes: 6 additions & 0 deletions packages/root/src/middleware/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -54,6 +57,7 @@ export function sessionMiddleware(options?: SessionMiddlewareOptions) {

export class Session {
private data: Record<string, string> = {};
hasChanges = false;

constructor(data?: Record<string, string>) {
this.data = data || {};
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f1293f1

Please sign in to comment.