Repository navigation
Expand file tree
/
Copy pathserver.ts
More file actions
64 lines (53 loc) · 2.81 KB
/
Copy pathserver.ts
File metadata and controls
64 lines (53 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import handler, { createServerEntry } from "@tanstack/react-start/server-entry";
import { isMarkdownPreferred, rewritePath } from "fumadocs-core/negotiation";
import { BASE_PATH } from "#src/lib/base-path.ts";
import { SITE_URL } from "#src/lib/site-url.ts";
// The docs used to live at the root of this host. Old URLs never start with `BASE_PATH`,
// where the static assets live, so they reach the Worker and get redirected.
const LEGACY_HOST = "env.oss.variable.land";
// env's Project page, which Variable Lab's site serves at `/env` itself. It reads like
// `SITE_URL`, but this Worker serves only what is under `/env/`.
const PROJECT_URL = "https://lab.variable.land/env";
// `/env`, `/env/` and `/env/index.html` belong to the Project page, not to this Worker.
// There is no prerendered `env/index.html`, so the static assets miss and these requests
// reach the Worker.
const PROJECT_PAGE_PATHS = new Set([BASE_PATH, `${BASE_PATH}/`, `${BASE_PATH}/index.html`]);
// Where an old link lands, in one hop. The static assets 307 a page path to its
// trailing-slash form, so page paths get the slash here; paths that already end in `/`
// and files (`.mdx`, `llms.txt`, images) go as they are. The root and `/index.html` go
// straight to the Project page.
function legacyTarget(pathname: string) {
if (pathname === "/" || pathname === "/index.html") return PROJECT_URL;
const isFile = /\.[^/]+$/.test(pathname);
return pathname.endsWith("/") || isFile ? `${SITE_URL}${pathname}` : `${SITE_URL}${pathname}/`;
}
// Public `/docs/foo.mdx` URLs and `Accept: text/markdown` requests on `/docs/*`
// both serve raw markdown via the internal `/raw/{slug}` route. We rewrite here
// so the route tree stays a vanilla splat (TanStack Router has no documented
// pattern that combines `$` splat with a literal `.mdx` suffix).
const { rewrite: stripMdxSuffix } = rewritePath(`${BASE_PATH}/docs{/*path}.mdx`, `${BASE_PATH}/raw{/*path}`);
const { rewrite: rewriteToRaw } = rewritePath(`${BASE_PATH}/docs{/*path}`, `${BASE_PATH}/raw{/*path}`);
export default createServerEntry({
fetch(request) {
const url = new URL(request.url);
if (PROJECT_PAGE_PATHS.has(url.pathname)) {
return Response.redirect(`${PROJECT_URL}${url.search}`, 301);
}
if (url.hostname === LEGACY_HOST) {
return Response.redirect(`${legacyTarget(url.pathname)}${url.search}`, 301);
}
const explicitMdx = stripMdxSuffix(url.pathname);
if (explicitMdx) {
const rewritten = new Request(new URL(explicitMdx, url), request);
return handler.fetch(rewritten);
}
if (isMarkdownPreferred(request)) {
const negotiated = rewriteToRaw(url.pathname);
if (negotiated) {
const rewritten = new Request(new URL(negotiated, url), request);
return handler.fetch(rewritten);
}
}
return handler.fetch(request);
},
});