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 6, 2024
1 parent 720c351 commit 5c8f38c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 73 deletions.
94 changes: 51 additions & 43 deletions packages/ssr/src/createBrowserClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,38 @@ export function createBrowserClient<
cookies = cookies || {};
}

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 @@ -78,26 +110,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 @@ -107,35 +143,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 coookies.set === 'function') {
// first delete all chunks so that there would be no overlap
await deleteAllChunks();

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 5c8f38c

Please sign in to comment.