Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: small improvements/fixes to cookie chunker in ssr #760

Merged
merged 1 commit into from
Apr 10, 2024
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/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;
hf marked this conversation as resolved.
Show resolved Hide resolved
}

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