Skip to content

Commit

Permalink
Safer check of in-app navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Nov 26, 2024
1 parent 39b5dcd commit d7010f4
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/gitbook/src/app/(site)/(content)/[[...pathname]]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,8 @@ function getTitle(input: Awaited<ReturnType<typeof getPageDataWithFallback>>) {
}

export async function generateMetadata(props: PageProps): Promise<Metadata> {
const headerList = await headers();
const fetchMode = headerList.get('sec-fetch-mode');

// We only generate metadata in navigation mode. Else we let the browser handle it.
if (fetchMode !== 'navigate') {
if (await checkIsInAppNavigation()) {
return {};
}

Expand All @@ -183,6 +180,16 @@ export async function generateMetadata(props: PageProps): Promise<Metadata> {
};
}

/**
* Check if the navigation is in-app, meaning the user clicks on a link.
*/
async function checkIsInAppNavigation() {
const headerList = await headers();
const fetchMode = headerList.get('sec-fetch-mode');

return fetchMode === 'cors';
}

/**
* Fetches the page data matching the requested pathname and fallback to root page when page is not found.
*/
Expand Down

0 comments on commit d7010f4

Please sign in to comment.