diff --git a/src/middleware.ts b/src/middleware.ts index 64400dc..84600ad 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) {