Skip to content

Commit 3e2bbf2

Browse files
committed
Scope middleware into a sub folder
1 parent 32305f4 commit 3e2bbf2

File tree

21 files changed

+5
-71
lines changed

21 files changed

+5
-71
lines changed

packages/gitbook/src/app/(site)/(content)/[[...pathname]]/not-found.tsx packages/gitbook/src/app/middleware/(site)/(content)/[[...pathname]]/not-found.tsx

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,16 @@
1-
import Link from 'next/link';
2-
31
import { TrackPageViewEvent } from '@/components/Insights';
42
import { getSpaceLanguage, t } from '@/intl/server';
5-
import { languages } from '@/intl/translations';
63
import { getSiteData, getSpaceContentData } from '@/lib/api';
7-
import { checkIsFromMiddleware } from '@/lib/pages';
84
import { getSiteContentPointer } from '@/lib/pointer';
95
import { tcls } from '@/lib/tailwind';
106

117
export default async function NotFound() {
12-
const fromMiddleware = await checkIsFromMiddleware();
13-
if (!fromMiddleware) {
14-
return (
15-
<div className="flex flex-1 flex-row items-center justify-center py-9 h-screen">
16-
<div className="max-w-80">
17-
<h2 className="text-2xl font-semibold mb-2">Not found</h2>
18-
<p className="text-base mb-4">This page could not be found</p>
19-
<Link href="/" className="text-blue-500 hover:text-blue-700 underline">
20-
Go back to home
21-
</Link>
22-
</div>
23-
</div>
24-
);
25-
}
268
const pointer = await getSiteContentPointer();
279
const [{ space }, { customization }] = await Promise.all([
2810
getSpaceContentData(pointer, pointer.siteShareKey),
2911
getSiteData(pointer),
3012
]);
13+
3114
const language = getSpaceLanguage(customization);
3215

3316
return (

packages/gitbook/src/app/(site)/(content)/[[...pathname]]/page.tsx packages/gitbook/src/app/middleware/(site)/(content)/[[...pathname]]/page.tsx

+1-19
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { CustomizationHeaderPreset, CustomizationThemeMode } from '@gitbook/api';
22
import { Metadata, Viewport } from 'next';
3-
import { headers } from 'next/headers';
43
import { notFound, redirect } from 'next/navigation';
54
import React from 'react';
65

76
import { PageAside } from '@/components/PageAside';
87
import { PageBody, PageCover } from '@/components/PageBody';
98
import { PageHrefContext, getAbsoluteHref, getPageHref } from '@/lib/links';
10-
import { checkIsFromMiddleware, getPagePath, resolveFirstDocument } from '@/lib/pages';
9+
import { getPagePath, resolveFirstDocument } from '@/lib/pages';
1110
import { ContentRefContext } from '@/lib/references';
1211
import { isSpaceIndexable, isPageIndexable } from '@/lib/seo';
1312
import { getContentTitle } from '@/lib/utils';
@@ -25,8 +24,6 @@ export default async function Page(props: {
2524
params: Promise<PagePathParams>;
2625
searchParams: Promise<{ fallback?: string }>;
2726
}) {
28-
await ensureIsFromMiddleware();
29-
3027
const { params: rawParams, searchParams: rawSearchParams } = props;
3128

3229
const params = await rawParams;
@@ -130,7 +127,6 @@ export async function generateViewport({
130127
}: {
131128
params: Promise<PagePathParams>;
132129
}): Promise<Viewport> {
133-
await ensureIsFromMiddleware();
134130
const { customization } = await fetchPageData(await params);
135131
return {
136132
colorScheme: customization.themes.toggeable
@@ -148,8 +144,6 @@ export async function generateMetadata({
148144
params: Promise<PagePathParams>;
149145
searchParams: Promise<{ fallback?: string }>;
150146
}): Promise<Metadata> {
151-
await ensureIsFromMiddleware();
152-
153147
const { space, pages, page, customization, site, ancestors } = await getPageDataWithFallback({
154148
pagePathParams: await params,
155149
searchParams: await searchParams,
@@ -212,15 +206,3 @@ async function getPageDataWithFallback(args: {
212206
page,
213207
};
214208
}
215-
216-
/**
217-
* Returns a page not found if the request is not from the middleware.
218-
* Some pages can be
219-
*/
220-
async function ensureIsFromMiddleware() {
221-
// To check if the request is from the middleware, we check if the x-gitbook-token is set in the headers.
222-
const fromMiddleware = await checkIsFromMiddleware();
223-
if (!fromMiddleware) {
224-
notFound();
225-
}
226-
}

packages/gitbook/src/app/(site)/(content)/layout.tsx packages/gitbook/src/app/middleware/(site)/(content)/layout.tsx

-18
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { assetsDomain } from '@/lib/assets';
1414
import { buildVersion } from '@/lib/build';
1515
import { getContentSecurityPolicyNonce } from '@/lib/csp';
1616
import { getAbsoluteHref, getBaseUrl } from '@/lib/links';
17-
import { checkIsFromMiddleware } from '@/lib/pages';
1817
import { isSpaceIndexable } from '@/lib/seo';
1918
import { getContentTitle } from '@/lib/utils';
2019

@@ -30,10 +29,6 @@ export const dynamic = 'force-dynamic';
3029
*/
3130
export default async function ContentLayout(props: { children: React.ReactNode }) {
3231
const { children } = props;
33-
const fromMiddleware = await checkIsFromMiddleware();
34-
if (!fromMiddleware) {
35-
return props.children;
36-
}
3732

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

113108
export async function generateViewport(): Promise<Viewport> {
114-
const fromMiddleware = await checkIsFromMiddleware();
115-
if (!fromMiddleware) {
116-
return {};
117-
}
118-
119109
const { customization } = await fetchContentData();
120110
return {
121111
colorScheme: customization.themes.toggeable
@@ -127,14 +117,6 @@ export async function generateViewport(): Promise<Viewport> {
127117
}
128118

129119
export async function generateMetadata(): Promise<Metadata> {
130-
const fromMiddleware = await checkIsFromMiddleware();
131-
if (!fromMiddleware) {
132-
return {
133-
title: 'Not found',
134-
robots: 'noindex, nofollow',
135-
};
136-
}
137-
138120
const { space, site, customization } = await fetchContentData();
139121
const customIcon = 'icon' in customization.favicon ? customization.favicon.icon : null;
140122

Original file line numberDiff line numberDiff line change
@@ -1,30 +1,18 @@
11
import { CustomizationRootLayout } from '@/components/RootLayout';
22
import { getSiteData } from '@/lib/api';
3-
import { checkIsFromMiddleware } from '@/lib/pages';
43
import { getSiteContentPointer } from '@/lib/pointer';
54

65
/**
76
* Layout to be used for the site root. It fetches the customization data for the site
87
* and initializes the CustomizationRootLayout with it.
98
*/
109
export default async function SiteRootLayout(props: { children: React.ReactNode }) {
11-
const fromMiddleware = await checkIsFromMiddleware();
12-
if (!fromMiddleware) {
13-
return (
14-
<html lang="en">
15-
<body className="font-[sans-serif]">
16-
<main>{props.children}</main>
17-
</body>
18-
</html>
19-
);
20-
}
10+
const { children } = props;
2111

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

2515
return (
26-
<CustomizationRootLayout customization={customization}>
27-
{props.children}
28-
</CustomizationRootLayout>
16+
<CustomizationRootLayout customization={customization}>{children}</CustomizationRootLayout>
2917
);
3018
}

packages/gitbook/src/middleware.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import jwt from 'jsonwebtoken';
55
import type { ResponseCookie } from 'next/dist/compiled/@edge-runtime/cookies';
66
import { NextResponse, NextRequest } from 'next/server';
77
import hash from 'object-hash';
8-
import rison from 'rison';
98

109
import {
1110
PublishedContentWithCache,
@@ -278,7 +277,7 @@ export async function middleware(request: NextRequest) {
278277
headers.set('x-gitbook-visitor-token', resolved.visitorToken);
279278
}
280279

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

284283
const response = writeCookies(

0 commit comments

Comments
 (0)