From 93782549c929ad6c3301a2a63bbc0dd498a86c2f Mon Sep 17 00:00:00 2001 From: Jeff Emmett <46964190+Jeff-Emmett@users.noreply.github.com> Date: Sat, 7 Dec 2024 12:58:25 -0500 Subject: [PATCH] fix CORS policy --- worker/TldrawDurableObject.ts | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/worker/TldrawDurableObject.ts b/worker/TldrawDurableObject.ts index 5e4a496..a101989 100644 --- a/worker/TldrawDurableObject.ts +++ b/worker/TldrawDurableObject.ts @@ -67,15 +67,32 @@ export class TldrawDurableObject { } return this.handleConnect(request) }) - .get('/room/:roomId', async () => { + .get('/room/:roomId', async (request) => { const room = await this.getRoom() const snapshot = room.getCurrentSnapshot() - return new Response(JSON.stringify(snapshot.documents)) + return new Response(JSON.stringify(snapshot.documents), { + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': request.headers.get('Origin') || '*', + 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS', + 'Access-Control-Allow-Headers': 'Content-Type', + 'Access-Control-Max-Age': '86400', + } + }) }) .post('/room/:roomId', async (request) => { const records = await request.json() as TLRecord[] const mergedRecords = await this.mergeCrdtState(records) - return new Response(JSON.stringify(Array.from(mergedRecords))) + + return new Response(JSON.stringify(Array.from(mergedRecords)), { + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': request.headers.get('Origin') || '*', + 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS', + 'Access-Control-Allow-Headers': 'Content-Type', + 'Access-Control-Max-Age': '86400', + } + }) }) // `fetch` is the entry point for all requests to the Durable Object