From e4e9b0d7e9436ab428dda992630fcd80f8b28927 Mon Sep 17 00:00:00 2001 From: Stojan Dimitrovski Date: Fri, 5 Apr 2024 13:08:26 +0200 Subject: [PATCH] fix: small improvements/fixes to cookie chunker in ssr --- packages/ssr/src/createBrowserClient.ts | 94 ++++++++++++++----------- packages/ssr/src/createServerClient.ts | 66 +++++++++-------- packages/ssr/src/utils/chunker.ts | 1 - 3 files changed, 88 insertions(+), 73 deletions(-) diff --git a/packages/ssr/src/createBrowserClient.ts b/packages/ssr/src/createBrowserClient.ts index bab59d1c..be6243c7 100644 --- a/packages/ssr/src/createBrowserClient.ts +++ b/packages/ssr/src/createBrowserClient.ts @@ -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: { @@ -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') { @@ -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); } } } diff --git a/packages/ssr/src/createServerClient.ts b/packages/ssr/src/createServerClient.ts index 5ed21d68..9dfba7e8 100644 --- a/packages/ssr/src/createServerClient.ts +++ b/packages/ssr/src/createServerClient.ts @@ -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: { @@ -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') { @@ -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); } } } diff --git a/packages/ssr/src/utils/chunker.ts b/packages/ssr/src/utils/chunker.ts index 9efcefe0..792c835d 100644 --- a/packages/ssr/src/utils/chunker.ts +++ b/packages/ssr/src/utils/chunker.ts @@ -99,7 +99,6 @@ export async function deleteChunks( if (value) { await removeChunk(key); - return; } for (let i = 0; ; i++) {