Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix api/auth route conflict #9153

Merged
merged 4 commits into from
Feb 15, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Cache the apiCategories() method using a promise
Commit 680c7c4 added memoized the fetch for OpenAPI JSON in the
`resolveOpenAPI` function, but that particular implementation caused some
problems with the build output. Some of the pages in the API section went
missing.

The previous implementation was susceptible to a bit of a race condition, since
the cached value wasn't set until after several async calls. When Next tries to
aggressively build pages, multiple calls could have gotten through to do live
calls, and then all of them would have tried to set the cache variable. I don't
know the exact pathway to the issue but I suspect this had something to do with
the missing pages (some sort of data corruption?). Sorry, I know this is handwavy.

Anyway, replacing that implementation with a more robust one - one that caches
a promise on the first call, and reuses that promise on all future calls - will
definitely only fetch once per process. When the fetch completes, everyone
waiting on the promise will awaken, and then future callers of the method will
get a resolved promise for immediate results.

And, most importantly, this method of caching doesn't cause the `api/auth` and
`api/guides` pages to go missing.
mjq committed Feb 15, 2024
commit 9d36622f45862fc6c2bb28f94802ac894071ba81
17 changes: 13 additions & 4 deletions src/build/resolveOpenAPI.ts
Original file line number Diff line number Diff line change
@@ -4,8 +4,6 @@

import {promises as fs} from 'fs';

import {cache} from 'react';

import {DeRefedOpenAPI} from './open-api/types';

// SENTRY_API_SCHEMA_SHA is used in the sentry-docs GHA workflow in getsentry/sentry-api-schema.
@@ -77,7 +75,18 @@ function slugify(s: string): string {
.toLowerCase();
}

export const apiCategories = cache(async (): Promise<APICategory[]> => {
let apiCategoriesCache: Promise<APICategory[]> | undefined;

export function apiCategories(): Promise<APICategory[]> {
if (apiCategoriesCache) {
return apiCategoriesCache;
}
apiCategoriesCache = apiCategoriesUncached();
return apiCategoriesCache;
}

async function apiCategoriesUncached(): Promise<APICategory[]> {
console.log('apiCategories');
const data = await resolveOpenAPI();

const categoryMap: {[name: string]: APICategory} = {};
@@ -142,7 +151,7 @@ export const apiCategories = cache(async (): Promise<APICategory[]> => {
c.apis.sort((a, b) => a.name.localeCompare(b.name));
});
return categories;
});
}

function getBodyParameters(apiData): APIParameter[] {
const content = apiData.requestBody?.content;