Skip to content

Commit

Permalink
feat: disable Netlify caching for schemas serving + rely in GH caching (
Browse files Browse the repository at this point in the history
  • Loading branch information
smoya authored Aug 3, 2023
1 parent 6dfb16a commit 632eb93
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
2 changes: 0 additions & 2 deletions netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
[[edge_functions]]
function = "serve-definitions"
path = "/definitions/*"
cache = "manual"

# Used by JSON Schema definitions fetched from schemastore.org
[[redirects]]
Expand All @@ -34,7 +33,6 @@ cache = "manual"
[[edge_functions]]
function = "serve-definitions"
path = "/schema-store/*"
cache = "manual"

[[plugins]]
package = "@netlify/plugin-nextjs"
31 changes: 19 additions & 12 deletions netlify/edge-functions/serve-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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));
}
Expand All @@ -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,
});
}

Expand Down

0 comments on commit 632eb93

Please sign in to comment.