Skip to content

Commit

Permalink
fix: small improvements/fixes to cookie chunker in ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
hf committed Apr 10, 2024
1 parent 5f3ef0a commit 31b8f99
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 73 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-ravens-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@supabase/ssr': minor
---

Improves the cookie chunker in @supabase/ssr to get rid of edge cases in saving and removing cookies.
94 changes: 51 additions & 43 deletions packages/ssr/src/createBrowserClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,38 @@ export function createBrowserClient<
};
}

const deleteAllChunks = async (key: string) => {
await deleteChunks(
key,
async (chunkName) => {
if (typeof cookies.get === 'function') {
return await cookies.get(chunkName);
}
if (isBrowser()) {
const documentCookies = parse(document.cookie);
return documentCookies[chunkName];
}
},
async (chunkName) => {
if (typeof cookies.remove === 'function') {
await cookies.remove(chunkName, {
...DEFAULT_COOKIE_OPTIONS,
...cookieOptions,
maxAge: 0
});
} else {
if (isBrowser()) {
document.cookie = serialize(chunkName, '', {
...DEFAULT_COOKIE_OPTIONS,
...cookieOptions,
maxAge: 0
});
}
}
}
);
};

const cookieClientOptions = {
global: {
headers: {
Expand Down Expand Up @@ -85,26 +117,30 @@ export function createBrowserClient<
return chunkedCookie;
},
setItem: async (key: string, value: string) => {
// first remove all chunks so there is no overlap
await deleteAllChunks(key);

const chunks = await createChunks(key, value);
await Promise.all(
chunks.map(async (chunk) => {
if (typeof cookies.set === 'function') {
await cookies.set(chunk.name, chunk.value, {

for (let i = 0; i < chunks.length; i += 1) {
const chunk = chunks[i];

if (typeof cookies.set === 'function') {
await cookies.set(chunk.name, chunk.value, {
...DEFAULT_COOKIE_OPTIONS,
...cookieOptions,
maxAge: DEFAULT_COOKIE_OPTIONS.maxAge
});
} else {
if (isBrowser()) {
document.cookie = serialize(chunk.name, chunk.value, {
...DEFAULT_COOKIE_OPTIONS,
...cookieOptions,
maxAge: DEFAULT_COOKIE_OPTIONS.maxAge
});
} else {
if (isBrowser()) {
document.cookie = serialize(chunk.name, chunk.value, {
...DEFAULT_COOKIE_OPTIONS,
...cookieOptions,
maxAge: DEFAULT_COOKIE_OPTIONS.maxAge
});
}
}
})
);
}
}
},
removeItem: async (key: string) => {
if (typeof cookies.remove === 'function' && typeof cookies.get !== 'function') {
Expand All @@ -114,35 +150,7 @@ export function createBrowserClient<
return;
}

await deleteChunks(
key,
async (chunkName) => {
if (typeof cookies.get === 'function') {
return await cookies.get(chunkName);
}
if (isBrowser()) {
const documentCookies = parse(document.cookie);
return documentCookies[chunkName];
}
},
async (chunkName) => {
if (typeof cookies.remove === 'function') {
await cookies.remove(chunkName, {
...DEFAULT_COOKIE_OPTIONS,
...cookieOptions,
maxAge: 0
});
} else {
if (isBrowser()) {
document.cookie = serialize(chunkName, '', {
...DEFAULT_COOKIE_OPTIONS,
...cookieOptions,
maxAge: 0
});
}
}
}
);
await deleteAllChunks(key);
}
}
}
Expand Down
66 changes: 37 additions & 29 deletions packages/ssr/src/createServerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ export function createServerClient<
};
}

const deleteAllChunks = async (key: string) => {
await deleteChunks(
key,
async (chunkName) => {
if (typeof cookies.get === 'function') {
return await cookies.get(chunkName);
}
},
async (chunkName) => {
if (typeof cookies.remove === 'function') {
return await cookies.remove(chunkName, {
...DEFAULT_COOKIE_OPTIONS,
...cookieOptions,
maxAge: 0
});
}
}
);
};

const cookieClientOptions = {
global: {
headers: {
Expand All @@ -69,18 +89,22 @@ export function createServerClient<
return chunkedCookie;
},
setItem: async (key: string, value: string) => {
const chunks = createChunks(key, value);
await Promise.all(
chunks.map(async (chunk) => {
if (typeof cookies.set === 'function') {
await cookies.set(chunk.name, chunk.value, {
...DEFAULT_COOKIE_OPTIONS,
...cookieOptions,
maxAge: DEFAULT_COOKIE_OPTIONS.maxAge
});
}
})
);
if (typeof cookies.set === 'function') {
// first delete all chunks so that there would be no overlap
await deleteAllChunks(key);

const chunks = createChunks(key, value);

for (let i = 0; i < chunks.length; i += 1) {
const chunk = chunks[i];

await cookies.set(chunk.name, chunk.value, {
...DEFAULT_COOKIE_OPTIONS,
...cookieOptions,
maxAge: DEFAULT_COOKIE_OPTIONS.maxAge
});
}
}
},
removeItem: async (key: string) => {
if (typeof cookies.remove === 'function' && typeof cookies.get !== 'function') {
Expand All @@ -90,23 +114,7 @@ export function createServerClient<
return;
}

deleteChunks(
key,
async (chunkName) => {
if (typeof cookies.get === 'function') {
return await cookies.get(chunkName);
}
},
async (chunkName) => {
if (typeof cookies.remove === 'function') {
return await cookies.remove(chunkName, {
...DEFAULT_COOKIE_OPTIONS,
...cookieOptions,
maxAge: 0
});
}
}
);
await deleteAllChunks(key);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/ssr/src/utils/chunker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export async function deleteChunks(

if (value) {
await removeChunk(key);
return;
}

for (let i = 0; ; i++) {
Expand Down

0 comments on commit 31b8f99

Please sign in to comment.