fix(docs-site): add docs index redirect, test, and trim Accept media type

Redirect bare /docs to the introduction page with proper metadata
resolution. Add a test for the redirect. Trim the media type in the
Accept header parser so whitespace around semicolons does not break
markdown content negotiation. Add a test script to package.json.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Luca Martial 2026-05-11 20:37:28 -07:00
parent e79caa8fb9
commit b4a302edfa
4 changed files with 33 additions and 5 deletions

View file

@ -5,15 +5,26 @@ import {
DocsTitle,
DocsDescription,
} from "fumadocs-ui/page";
import { notFound } from "next/navigation";
import { notFound, redirect } from "next/navigation";
import defaultMdxComponents from "fumadocs-ui/mdx";
import { CodeBlock } from "@/components/code-block";
import { DocsPageActions } from "@/components/docs-page-actions";
const docsIndexPath = "/docs/getting-started/introduction";
const docsIndexSlug = ["getting-started", "introduction"] as const;
function isDocsIndex(slug: string[] | undefined) {
return slug === undefined || slug.length === 0 || slug.join("/") === "";
}
export default async function Page(props: {
params: Promise<{ slug?: string[] }>;
}) {
const params = await props.params;
if (isDocsIndex(params.slug)) {
redirect(docsIndexPath);
}
const page = source.getPage(params.slug);
if (!page) notFound();
@ -35,14 +46,16 @@ export default async function Page(props: {
}
export function generateStaticParams() {
return source.generateParams();
return [{ slug: [""] }, ...source.generateParams()];
}
export async function generateMetadata(props: {
params: Promise<{ slug?: string[] }>;
}) {
const params = await props.params;
const page = source.getPage(params.slug);
const page = source.getPage(
isDocsIndex(params.slug) ? [...docsIndexSlug] : params.slug,
);
if (!page) notFound();
return {

View file

@ -38,7 +38,7 @@ function isMarkdownPreferred(acceptHeader: string | null) {
.find((parameter) => parameter.startsWith("q="));
return {
type: type.toLowerCase(),
type: type.trim().toLowerCase(),
quality: quality ? Number.parseFloat(quality.slice(2)) : 1,
index,
};

View file

@ -6,7 +6,8 @@
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
"start": "next start",
"test": "node --test tests/*.test.mjs"
},
"dependencies": {
"fumadocs-core": "15.7.13",

View file

@ -0,0 +1,14 @@
import assert from "node:assert/strict";
import test from "node:test";
const docsSiteUrl = process.env.DOCS_SITE_URL ?? "http://localhost:3000";
test("/docs redirects to the docs introduction", async () => {
const response = await fetch(`${docsSiteUrl}/docs`, { redirect: "manual" });
assert.equal(response.status, 307);
assert.equal(
response.headers.get("location"),
"/docs/getting-started/introduction",
);
});