Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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/sharp-yaks-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@react-router/cloudflare": minor
---

Allow prefix on createWorkersKVSessionStorare
Comment thread
MichaelDeBoey marked this conversation as resolved.
Outdated
16 changes: 11 additions & 5 deletions packages/react-router-cloudflare/sessions/workersKVStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ interface WorkersKVSessionStorageOptions {
* The KVNamespace used to store the sessions.
*/
kv: KVNamespace;

/**
* Optional prefix for the session keys in the KV store.
*/
prefix?: string;
}

/**
Expand All @@ -30,6 +35,7 @@ export function createWorkersKVSessionStorage<
>({
cookie,
kv,
prefix,
}: WorkersKVSessionStorageOptions): SessionStorage<Data, FlashData> {
return createSessionStorage({
cookie,
Expand All @@ -44,11 +50,11 @@ export function createWorkersKVSessionStorage<
.map((x) => x.toString(16).padStart(2, "0"))
.join("");

if (await kv.get(id, "json")) {
if (await kv.get(`${prefix}${id}`, "json")) {
continue;
}

await kv.put(id, JSON.stringify(data), {
await kv.put(`${prefix}${id}`, JSON.stringify(data), {
expiration: expires
? Math.round(expires.getTime() / 1000)
: undefined,
Expand All @@ -58,7 +64,7 @@ export function createWorkersKVSessionStorage<
}
},
async readData(id) {
let session = await kv.get(id);
let session = await kv.get(`${prefix}${id}`);

if (!session) {
return null;
Expand All @@ -67,12 +73,12 @@ export function createWorkersKVSessionStorage<
return JSON.parse(session);
},
async updateData(id, data, expires) {
await kv.put(id, JSON.stringify(data), {
await kv.put(`${prefix}${id}`, JSON.stringify(data), {
expiration: expires ? Math.round(expires.getTime() / 1000) : undefined,
});
},
async deleteData(id) {
await kv.delete(id);
await kv.delete(`${prefix}${id}`);
},
});
}