Skip to content

Commit 0546673

Browse files
committed
fix preview fonts and dev-only routes
1 parent 008c465 commit 0546673

5 files changed

Lines changed: 52 additions & 82 deletions

File tree

src/app/api/md/[...path]/route.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import fs from 'fs';
99
import path from 'path';
1010
import {NextResponse} from 'next/server';
1111
import {cacheLife} from 'next/cache';
12-
import {collectAllContentPaths} from 'lib/collectPaths';
12+
import {collectAllContentPaths, isContentPageAvailable} from 'lib/collectPaths';
1313

1414
const FOOTER = `
1515
---
@@ -39,6 +39,7 @@ async function readContentMarkdown(
3939
'use cache';
4040
cacheLife('max');
4141
if (!pathSegments || pathSegments.length === 0) return null;
42+
if (!isContentPageAvailable(pathSegments)) return null;
4243

4344
const filePath = pathSegments.join('/');
4445
// Block /index.md URLs - use /foo.md instead of /foo/index.md

src/app/layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export default function RootLayout({children}: {children: React.ReactNode}) {
189189
<link
190190
key={file}
191191
rel="preload"
192-
href={`https://react.dev/fonts/${file}`}
192+
href={`/fonts/${file}`}
193193
as="font"
194194
type="font/woff2"
195195
crossOrigin="anonymous"

src/lib/collectPaths.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ const stat = promisify(fs.stat);
1717
const ROOT = path.join(process.cwd(), 'src/content');
1818
const DEV_ONLY_PAGES = new Set(['learn/rsc-sandbox-test']);
1919

20+
export function isContentPageAvailable(segments: string[]): boolean {
21+
return (
22+
process.env.NODE_ENV !== 'production' ||
23+
!DEV_ONLY_PAGES.has(segments.join('/'))
24+
);
25+
}
26+
2027
async function getFiles(dir: string, base: string): Promise<string[]> {
2128
const subdirs = await readdir(dir);
2229
const files = await Promise.all(
@@ -54,10 +61,7 @@ export async function collectSectionPaths(
5461
const files = await getFiles(dir, dir);
5562
return files
5663
.map((file) => getSegments(file))
57-
.filter((segments) => {
58-
if (process.env.NODE_ENV !== 'production') return true;
59-
return !DEV_ONLY_PAGES.has([section, ...segments].join('/'));
60-
});
64+
.filter((segments) => isContentPageAvailable([section, ...segments]));
6165
}
6266

6367
/**
@@ -76,6 +80,7 @@ export async function collectAllContentPaths(): Promise<string[][]> {
7680
// Drop the root `index.md` (-> []); `/index.md` isn't a served URL and an
7781
// empty catch-all param can't be prerendered.
7882
.filter((segments) => segments.length > 0)
83+
.filter(isContentPageAvailable)
7984
);
8085
}
8186

src/lib/readMarkdownPage.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'server-only';
99
import fs from 'fs/promises';
1010
import path from 'path';
1111
import {cacheLife} from 'next/cache';
12+
import {isContentPageAvailable} from './collectPaths';
1213
import compileMDX from 'utils/compileMDX';
1314
import type {CompiledMDX} from 'utils/compileMDX';
1415

@@ -33,6 +34,7 @@ export async function readMarkdownPage(
3334
): Promise<PageData | null> {
3435
'use cache';
3536
cacheLife('max');
37+
if (!isContentPageAvailable(segments)) return null;
3638
const routePath = segments.join('/') || 'index';
3739
let mdx: string | null = null;
3840
for (const candidate of [

0 commit comments

Comments
 (0)