Skip to content

Commit

Permalink
Fix missing api/auth, api/guides pages (#9153)
Browse files Browse the repository at this point in the history
The `api/auth` route added for the changelog conflicts with the `api/auth` docs
page that would normally be handled by the catchall `[[...path]]` route.
Move it under the `changelog` path to fix the route conflict.

Even so, the pages continued to 404.

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.
  • Loading branch information
mjq authored Feb 15, 2024
1 parent 5dae0d5 commit 37b224a
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/changelog/%5Fadmin/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {type ReactNode, Suspense} from 'react';
import {GET} from 'app/api/auth/[...nextauth]/route';
import {GET} from 'app/changelog/api/auth/[...nextauth]/route';
import {getServerSession} from 'next-auth/next';

import LoginButton from 'sentry-docs/components/changelog/loginButton';
Expand Down
2 changes: 1 addition & 1 deletion app/changelog/%5Fadmin/upload/route.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import crypto from 'crypto';

import {Storage} from '@google-cloud/storage';
import {GET as sessionHandler} from 'app/api/auth/[...nextauth]/route';
import {GET as sessionHandler} from 'app/changelog/api/auth/[...nextauth]/route';
import {NextRequest} from 'next/server';
import {getServerSession} from 'next-auth/next';

Expand Down
2 changes: 1 addition & 1 deletion app/changelog/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Fragment, Suspense} from 'react';
import {type Category, type Changelog} from '@prisma/client';
import * as Sentry from '@sentry/nextjs';
import {GET} from 'app/api/auth/[...nextauth]/route';
import {GET} from 'app/changelog/api/auth/[...nextauth]/route';
import type {Metadata, ResolvingMetadata} from 'next';
import Link from 'next/link';
import {getServerSession} from 'next-auth/next';
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/actions/changelog.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use server';

import {GET as handler} from 'app/api/auth/[...nextauth]/route';
import {GET as handler} from 'app/changelog/api/auth/[...nextauth]/route';
import {revalidatePath} from 'next/cache';
import {redirect} from 'next/navigation';
import {getServerSession} from 'next-auth/next';
Expand Down
28 changes: 14 additions & 14 deletions src/build/resolveOpenAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -14,8 +12,6 @@ const SENTRY_API_SCHEMA_SHA = '78a8da372a1912d69e27b7f36603017cd29b6815';

const activeEnv = process.env.GATSBY_ENV || process.env.NODE_ENV || 'development';

let cachedResonse: DeRefedOpenAPI | null = null;

async function resolveOpenAPI(): Promise<DeRefedOpenAPI> {
if (activeEnv === 'development' && process.env.OPENAPI_LOCAL_PATH) {
try {
Expand All @@ -29,16 +25,10 @@ async function resolveOpenAPI(): Promise<DeRefedOpenAPI> {
);
}
}

if (cachedResonse) {
return cachedResonse;
}
const response = await fetch(
`https://raw.githubusercontent.com/getsentry/sentry-api-schema/${SENTRY_API_SCHEMA_SHA}/openapi-derefed.json`,
{cache: 'no-store'}
`https://raw.githubusercontent.com/getsentry/sentry-api-schema/${SENTRY_API_SCHEMA_SHA}/openapi-derefed.json`
);
cachedResonse = await response.json();
return cachedResonse!;
return await response.json();
}

export type APIParameter = {
Expand Down Expand Up @@ -85,7 +75,17 @@ 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[]> {
const data = await resolveOpenAPI();

const categoryMap: {[name: string]: APICategory} = {};
Expand Down Expand Up @@ -150,7 +150,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;
Expand Down

0 comments on commit 37b224a

Please sign in to comment.