From ff34e8f9ed2d4d3a78c43cfb2b6ac36310b71b28 Mon Sep 17 00:00:00 2001 From: Cody Olsen Date: Thu, 21 Nov 2024 02:13:55 +0100 Subject: [PATCH] add a little stagger --- api/.env.local.example | 2 ++ api/api/random-color-theme.ts | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 api/.env.local.example diff --git a/api/.env.local.example b/api/.env.local.example new file mode 100644 index 0000000..901cfb9 --- /dev/null +++ b/api/.env.local.example @@ -0,0 +1,2 @@ +# Required for the random-color-theme API, needs editor role or higher +SANITY_API_WRITE_TOKEN= \ No newline at end of file diff --git a/api/api/random-color-theme.ts b/api/api/random-color-theme.ts index 5fd3eea..813b50f 100644 --- a/api/api/random-color-theme.ts +++ b/api/api/random-color-theme.ts @@ -70,7 +70,11 @@ export default async function handler(request: Request) { }) const _id = 'theme' const patch = client.patch(_id).set(generateThemeColors()) - await client.transaction().createIfNotExists({_id, _type: _id}).patch(patch).commit() + await Promise.allSettled([ + client.transaction().createIfNotExists({_id, _type: _id}).patch(patch).commit(), + // Wait 3 seconds to stagger requests a little bit + new Promise((resolve) => setTimeout(resolve, 3_000)), + ]) return new Response(JSON.stringify({patch}), {status: 200, headers}) } catch (err) {