Skip to content

Commit 014c802

Browse files
committed
fix(docs redirects): use a plugin to patch the client build and remove csr redirects
1 parent 0a68706 commit 014c802

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

apps/docs/astro.config.mjs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,40 @@
11
// @ts-check
22
import { defineConfig } from "astro/config";
33
import tailwindcss from "@tailwindcss/vite";
4+
import { unlink } from "node:fs/promises";
5+
import { join } from "node:path";
46

57
import mdx from "@astrojs/mdx";
68

79
import react from "@astrojs/react";
810

11+
/**
12+
* Patches the CSR redirect to delete the root index files.
13+
* @returns {import("astro").AstroIntegration}
14+
*/
15+
function patchCsrRedirect() {
16+
return {
17+
name: "patch-csr-redirect",
18+
hooks: {
19+
"astro:build:done": async ({ dir }) => {
20+
const filesToDelete = [
21+
"index.html",
22+
"en/index.html",
23+
"pt-br/index.html",
24+
];
25+
for (const file of filesToDelete) {
26+
try {
27+
await unlink(join(dir.pathname, file));
28+
console.log(`[CSR Redirect Patch] Deleted ${file}`);
29+
} catch {
30+
// File may not exist, ignore
31+
}
32+
}
33+
},
34+
},
35+
};
36+
}
37+
938
// https://astro.build/config
1039
export default defineConfig({
1140
root: "client",
@@ -22,7 +51,7 @@ export default defineConfig({
2251
prefixDefaultLocale: true,
2352
},
2453
},
25-
integrations: [mdx(), react()],
54+
integrations: [mdx(), react(), patchCsrRedirect()],
2655
vite: {
2756
plugins: [
2857
// @ts-ignore: tailwindcss plugin type issue

apps/docs/server/main.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,20 @@ interface Env {
77
};
88
}
99

10+
const rootRedirects: Record<string, string> = {
11+
"/": "/en/introduction",
12+
"/en": "/en/introduction",
13+
"/pt-br": "/pt-br/introduction",
14+
};
15+
1016
const runtime = withRuntime<Env>({
1117
fetch: async (req, env) => {
1218
const url = new URL(req.url);
13-
if (url.pathname === "/" || url.pathname === "" || url.pathname === "/en") {
14-
return Response.redirect(new URL("/en/introduction", req.url), 302);
19+
if (rootRedirects[url.pathname]) {
20+
return Response.redirect(
21+
new URL(rootRedirects[url.pathname], req.url),
22+
302,
23+
);
1524
}
1625

1726
const assetsHandler =

apps/docs/wrangler.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ custom_domain = true
1212
[assets]
1313
directory = "./dist/client"
1414
binding = "ASSETS"
15-
run_worker_first = true
1615

1716
[deco]
1817
workspace = "deco.cx"

0 commit comments

Comments
 (0)