Skip to content

Commit

Permalink
Scope middleware into a sub folder
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Jan 15, 2025
1 parent 32305f4 commit 3e2bbf2
Show file tree
Hide file tree
Showing 21 changed files with 5 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
import Link from 'next/link';

import { TrackPageViewEvent } from '@/components/Insights';
import { getSpaceLanguage, t } from '@/intl/server';
import { languages } from '@/intl/translations';
import { getSiteData, getSpaceContentData } from '@/lib/api';
import { checkIsFromMiddleware } from '@/lib/pages';
import { getSiteContentPointer } from '@/lib/pointer';
import { tcls } from '@/lib/tailwind';

export default async function NotFound() {
const fromMiddleware = await checkIsFromMiddleware();
if (!fromMiddleware) {
return (
<div className="flex flex-1 flex-row items-center justify-center py-9 h-screen">
<div className="max-w-80">
<h2 className="text-2xl font-semibold mb-2">Not found</h2>
<p className="text-base mb-4">This page could not be found</p>
<Link href="/" className="text-blue-500 hover:text-blue-700 underline">
Go back to home
</Link>
</div>
</div>
);
}
const pointer = await getSiteContentPointer();
const [{ space }, { customization }] = await Promise.all([
getSpaceContentData(pointer, pointer.siteShareKey),
getSiteData(pointer),
]);

const language = getSpaceLanguage(customization);

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { CustomizationHeaderPreset, CustomizationThemeMode } from '@gitbook/api';
import { Metadata, Viewport } from 'next';
import { headers } from 'next/headers';
import { notFound, redirect } from 'next/navigation';
import React from 'react';

import { PageAside } from '@/components/PageAside';
import { PageBody, PageCover } from '@/components/PageBody';
import { PageHrefContext, getAbsoluteHref, getPageHref } from '@/lib/links';
import { checkIsFromMiddleware, getPagePath, resolveFirstDocument } from '@/lib/pages';
import { getPagePath, resolveFirstDocument } from '@/lib/pages';
import { ContentRefContext } from '@/lib/references';
import { isSpaceIndexable, isPageIndexable } from '@/lib/seo';
import { getContentTitle } from '@/lib/utils';
Expand All @@ -25,8 +24,6 @@ export default async function Page(props: {
params: Promise<PagePathParams>;
searchParams: Promise<{ fallback?: string }>;
}) {
await ensureIsFromMiddleware();

const { params: rawParams, searchParams: rawSearchParams } = props;

const params = await rawParams;
Expand Down Expand Up @@ -130,7 +127,6 @@ export async function generateViewport({
}: {
params: Promise<PagePathParams>;
}): Promise<Viewport> {
await ensureIsFromMiddleware();
const { customization } = await fetchPageData(await params);
return {
colorScheme: customization.themes.toggeable
Expand All @@ -148,8 +144,6 @@ export async function generateMetadata({
params: Promise<PagePathParams>;
searchParams: Promise<{ fallback?: string }>;
}): Promise<Metadata> {
await ensureIsFromMiddleware();

const { space, pages, page, customization, site, ancestors } = await getPageDataWithFallback({
pagePathParams: await params,
searchParams: await searchParams,
Expand Down Expand Up @@ -212,15 +206,3 @@ async function getPageDataWithFallback(args: {
page,
};
}

/**
* Returns a page not found if the request is not from the middleware.
* Some pages can be
*/
async function ensureIsFromMiddleware() {
// To check if the request is from the middleware, we check if the x-gitbook-token is set in the headers.
const fromMiddleware = await checkIsFromMiddleware();
if (!fromMiddleware) {
notFound();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { assetsDomain } from '@/lib/assets';
import { buildVersion } from '@/lib/build';
import { getContentSecurityPolicyNonce } from '@/lib/csp';
import { getAbsoluteHref, getBaseUrl } from '@/lib/links';
import { checkIsFromMiddleware } from '@/lib/pages';
import { isSpaceIndexable } from '@/lib/seo';
import { getContentTitle } from '@/lib/utils';

Expand All @@ -30,10 +29,6 @@ export const dynamic = 'force-dynamic';
*/
export default async function ContentLayout(props: { children: React.ReactNode }) {
const { children } = props;
const fromMiddleware = await checkIsFromMiddleware();
if (!fromMiddleware) {
return props.children;
}

const nonce = await getContentSecurityPolicyNonce();
const {
Expand Down Expand Up @@ -111,11 +106,6 @@ export default async function ContentLayout(props: { children: React.ReactNode }
}

export async function generateViewport(): Promise<Viewport> {
const fromMiddleware = await checkIsFromMiddleware();
if (!fromMiddleware) {
return {};
}

const { customization } = await fetchContentData();
return {
colorScheme: customization.themes.toggeable
Expand All @@ -127,14 +117,6 @@ export async function generateViewport(): Promise<Viewport> {
}

export async function generateMetadata(): Promise<Metadata> {
const fromMiddleware = await checkIsFromMiddleware();
if (!fromMiddleware) {
return {
title: 'Not found',
robots: 'noindex, nofollow',
};
}

const { space, site, customization } = await fetchContentData();
const customIcon = 'icon' in customization.favicon ? customization.favicon.icon : null;

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
import { CustomizationRootLayout } from '@/components/RootLayout';
import { getSiteData } from '@/lib/api';
import { checkIsFromMiddleware } from '@/lib/pages';
import { getSiteContentPointer } from '@/lib/pointer';

/**
* Layout to be used for the site root. It fetches the customization data for the site
* and initializes the CustomizationRootLayout with it.
*/
export default async function SiteRootLayout(props: { children: React.ReactNode }) {
const fromMiddleware = await checkIsFromMiddleware();
if (!fromMiddleware) {
return (
<html lang="en">
<body className="font-[sans-serif]">
<main>{props.children}</main>
</body>
</html>
);
}
const { children } = props;

const pointer = await getSiteContentPointer();
const { customization } = await getSiteData(pointer);

return (
<CustomizationRootLayout customization={customization}>
{props.children}
</CustomizationRootLayout>
<CustomizationRootLayout customization={customization}>{children}</CustomizationRootLayout>
);
}
3 changes: 1 addition & 2 deletions packages/gitbook/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import jwt from 'jsonwebtoken';
import type { ResponseCookie } from 'next/dist/compiled/@edge-runtime/cookies';
import { NextResponse, NextRequest } from 'next/server';
import hash from 'object-hash';
import rison from 'rison';

import {
PublishedContentWithCache,
Expand Down Expand Up @@ -278,7 +277,7 @@ export async function middleware(request: NextRequest) {
headers.set('x-gitbook-visitor-token', resolved.visitorToken);
}

const target = new URL(rewritePathname, request.nextUrl.toString());
const target = new URL('/middleware' + rewritePathname, request.nextUrl.toString());
target.search = url.search;

const response = writeCookies(
Expand Down

0 comments on commit 3e2bbf2

Please sign in to comment.