Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/documentation/src/routes/docs/-$.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,27 @@ describe("Accept header negotiation", () => {
const text = await res.text();
expect(text).toContain("<!DOCTYPE html>");
});

test("Accept: text/markdown works with base-path prefix", async () => {
const res = await fetch(`${BASE_URL}/graphql/hive/docs/gateway`, {
headers: { Accept: "text/markdown" },
redirect: "follow",
});
expect(res.status).toBe(200);
expect(res.headers.get("content-type")).toBe("text/markdown");
const text = await res.text();
expect(text).toContain("---");
expect(text).toContain("title:");
});

test("Accept: text/plain works with base-path prefix", async () => {
const res = await fetch(`${BASE_URL}/graphql/hive/docs`, {
headers: { Accept: "text/plain" },
redirect: "follow",
});
expect(res.status).toBe(200);
expect(res.headers.get("content-type")).toBe("text/markdown");
});
});

describe("prerendered HTML routing", () => {
Expand Down
21 changes: 17 additions & 4 deletions packages/documentation/src/server/cloudflare-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,23 @@ function aliasRequest(
env: CloudflareEnv,
context: CloudflareContext,
) {
if (!baseURL || hasBasePath(url.pathname)) {
if (!baseURL) {
return request;
}

if (hasBasePath(url.pathname)) {
// For base-path requests that prefer markdown, rewrite to the llms.mdx
// route so the request doesn't get served as prerendered HTML by nitro.
const strippedPathname = stripBasePath(url.pathname);
if (shouldAliasMarkdownDocs(request, strippedPathname)) {
const nextURL = new URL(request.url);
nextURL.pathname = withBasePath(
`/llms.mdx${normalizeDocsPathname(strippedPathname)}`,
);
const aliasedRequest = new Request(nextURL, request);
augmentReq(aliasedRequest, { context, env });
return aliasedRequest;
}
return request;
}

Expand Down Expand Up @@ -332,10 +348,8 @@ async function tryServeAsset(
env: CloudflareEnv,
context: CloudflareContext,
requestURL: URL,
isAliasedRequest: boolean,
): Promise<Response | undefined> {
if (
!isAliasedRequest ||
!env.ASSETS ||
!shouldTryAssetRequest({
baseURL,
Expand Down Expand Up @@ -409,7 +423,6 @@ export default createHandler({
env,
context,
requestURL,
isAliasedRequest,
);
if (assetResponse) {
return rewriteAliasedResponse(
Expand Down
1 change: 1 addition & 0 deletions packages/documentation/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default defineConfig(async ({ command }) => ({
wrangler: {
assets: {
html_handling: "drop-trailing-slash",
run_worker_first: true,
},
},
}
Expand Down
Loading