From 632eb93a485624ad8a00c02e32055f9ebdf1606c Mon Sep 17 00:00:00 2001 From: Sergio Moya <1083296+smoya@users.noreply.github.com> Date: Thu, 3 Aug 2023 09:24:33 +0200 Subject: [PATCH] feat: disable Netlify caching for schemas serving + rely in GH caching (#2024) --- netlify.toml | 2 -- netlify/edge-functions/serve-definitions.ts | 31 +++++++++++++-------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/netlify.toml b/netlify.toml index ebcbcd3b212..3f3d2beb535 100644 --- a/netlify.toml +++ b/netlify.toml @@ -23,7 +23,6 @@ [[edge_functions]] function = "serve-definitions" path = "/definitions/*" -cache = "manual" # Used by JSON Schema definitions fetched from schemastore.org [[redirects]] @@ -34,7 +33,6 @@ cache = "manual" [[edge_functions]] function = "serve-definitions" path = "/schema-store/*" -cache = "manual" [[plugins]] package = "@netlify/plugin-nextjs" diff --git a/netlify/edge-functions/serve-definitions.ts b/netlify/edge-functions/serve-definitions.ts index e50747be643..3197f584478 100644 --- a/netlify/edge-functions/serve-definitions.ts +++ b/netlify/edge-functions/serve-definitions.ts @@ -19,7 +19,6 @@ const legitimateRequestRegex = /^\/[\w\-]*\/?(?:([\w\-\.]*\/)?([\w\-$%\.]*\.json export default async (request: Request, context: Context) => { let rewriteRequest = buildRewrite(request); - let response: Response; if (rewriteRequest === null) { rewriteRequest = request; @@ -33,6 +32,12 @@ export default async (request: Request, context: Context) => { const isRequestingAFile = request.url.endsWith('.json'); if (isRequestingAFile) { var metricName: string + const metricAttributes = { + 'responseStatus': response.status, + 'responseStatusText': response.statusText, + 'cached': false, + }; + if (response.ok) { // Manually cloning the response so we can modify the headers as they are immutable response = new Response(response.body, response); @@ -43,15 +48,18 @@ export default async (request: Request, context: Context) => { metricName = "asyncapi.jsonschema.download.success"; } else { - // Notifying NR of the error. - metricName = "asyncapi.jsonschema.download.error"; + switch (response.status) { + case 304: + metricName = "asyncapi.jsonschema.download.success"; + metricAttributes["cached"] = true; + break; + default: + // Notifying NR of the error. + metricName = "asyncapi.jsonschema.download.error"; + break; + } } - const metricAttributes = { - "responseStatus": response.status, - "responseStatusText": response.statusText, - }; - // Sending metrics to NR. await sendMetricToNR(context, newNRMetricCount(metricName, request, rewriteRequest, metricAttributes)); } @@ -76,12 +84,11 @@ function buildRewrite(originalRequest: Request): (Request | null) { url = URL_DEST_DEFINITIONS + `/${definitionVersion}${file}`; } + originalRequest.headers.set('Authorization', 'token ' + GITHUB_TOKEN); + return new Request(url, { method: originalRequest.method, - headers: new Headers({ - // Setting GH Token to increase GH rate limit to 5,000 req/h. - 'Authorization': "token " + GITHUB_TOKEN, - }), + headers: originalRequest.headers, }); }