From e1785bfd805759a3a392b05eac7e5e0d72c90a66 Mon Sep 17 00:00:00 2001 From: Luuk Date: Tue, 1 Oct 2024 15:21:48 +0200 Subject: [PATCH] Fixed middleware --- src/middleware.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/middleware.ts b/src/middleware.ts index 64400dcc..84600adc 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -15,8 +15,10 @@ export const hashSecret = async (secret: string) => { }; export const datocms = defineMiddleware(async ({ locals }, next) => { - locals.datocmsEnvironment = datocmsEnvironment; - locals.datocmsToken = getSecret('DATOCMS_READONLY_API_TOKEN'); + Object.assign(locals, { + datocmsEnvironment: datocmsEnvironment, + datocmsToken: getSecret('DATOCMS_READONLY_API_TOKEN') + }); const response = await next(); return response; }); @@ -32,16 +34,18 @@ const i18n = defineMiddleware(async ({ params, request }, next) => { Object.assign(params, { locale }); } setLocale(params.locale as SiteLocale); - + const response = await next(); return response; }); const preview = defineMiddleware(async ({ cookies, locals }, next) => { - const previewSecret = getSecret('HEAD_START_PREVIEW_SECRET'); - locals.isPreview = getSecret('HEAD_START_PREVIEW'); - locals.isPreviewAuthOk = Boolean(previewSecret) && cookies.get(previewCookieName)?.value === await hashSecret(previewSecret); - locals.previewSecret = previewSecret; + const previewSecret = getSecret('HEAD_START_PREVIEW_SECRET') || ''; + Object.assign(locals, { + isPreview: getSecret('HEAD_START_PREVIEW'), + isPreviewAuthOk: Boolean(previewSecret) && cookies.get(previewCookieName)?.value === await hashSecret(previewSecret), + previewSecret: previewSecret + }); const response = await next(); if (locals.isPreview) {