From a71ccbdf2237f813e462fd7f374acb7dfa7684ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20J=C3=A4derberg?= Date: Tue, 13 May 2025 13:19:09 +0000 Subject: [PATCH] Merged in feature/hardcode-release-branch-warming (pull request #2052) fix: hardcode release branch warmup * fix: hardcode release branch warmup * . Approved-by: Anton Gunnarsson --- .../netlify/functions/warmup-background.mts | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/apps/scandic-web/netlify/functions/warmup-background.mts b/apps/scandic-web/netlify/functions/warmup-background.mts index 0e95318a3..ecdbf67d1 100644 --- a/apps/scandic-web/netlify/functions/warmup-background.mts +++ b/apps/scandic-web/netlify/functions/warmup-background.mts @@ -19,7 +19,7 @@ async function WarmupHandler(request: Request, context: Context) { case ErrorCodes.WARMUP_DISABLED: console.warn("[WARMUP] Warmup is disabled") return - case ErrorCodes.URLS_DONT_MATCH: + case ErrorCodes.REQUEST_NOT_FOR_CURRENT_CONTEXT: // This is expected, this webhook will be called for all deployments // and we only want to warmup the ones that match our URL return @@ -63,9 +63,13 @@ async function validateRequest( throw new Error(ErrorCodes.UNABLE_TO_PARSE_DEPLOYMENT_INFO) } - const deployedUrl = deployment.deploy_ssl_url - if (deployedUrl !== context.url.origin) { - throw new Error(ErrorCodes.URLS_DONT_MATCH) + const isForCurrentContext = isRequestForCurrentContext({ + currentUrl: context.url.origin, + deployedUrl: deployment.deploy_ssl_url, + }) + + if (!isForCurrentContext) { + throw new Error(ErrorCodes.REQUEST_NOT_FOR_CURRENT_CONTEXT) } console.log("[WARMUP] Warmup request", deployment) @@ -177,6 +181,20 @@ function hasSha256(decoded: unknown): decoded is { sha256: string } { ) } +function isRequestForCurrentContext({ + currentUrl, + deployedUrl, +}: { + currentUrl: string + deployedUrl: string +}) { + const isForProduction = + currentUrl === "https://web-scandic-hotels.netlify.app" && + deployedUrl === "https://release--web-scandic-hotels.netlify.app" + + return isForProduction || currentUrl === deployedUrl +} + type DeploymentInfo = { id: string site_id: string @@ -243,7 +261,7 @@ const ErrorCodes = { MISSING_SIGNATURE_HEADER: "MISSING SIGNATURE HEADER", MISSING_SIGNATURE: "MISSING SIGNATURE", UNABLE_TO_PARSE_DEPLOYMENT_INFO: "UNABLE TO PARSE DEPLOYMENT INFO", - URLS_DONT_MATCH: "URLS DONT MATCH", + REQUEST_NOT_FOR_CURRENT_CONTEXT: "REQUEST NOT FOR CURRENT CONTEXT", WARMUP_DISABLED: "WARMUP IS DISABLED", } as const type ErrorCode = (typeof ErrorCodes)[keyof typeof ErrorCodes]