feat(www): serve cached markdown documentation
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import { cp } from "node:fs/promises"
|
||||
import config from "../astro.config"
|
||||
|
||||
const base = config.base?.replace(/\/$/, "") ?? ""
|
||||
const docs = `dist/client${base}/docs`
|
||||
|
||||
await Bun.$`pagefind --site ${`dist/client${base}/docs`}`
|
||||
await cp(`dist/client${base}/docs-index`, docs, { recursive: true })
|
||||
await Bun.$`pagefind --site ${docs}`
|
||||
|
||||
@@ -1,10 +1,21 @@
|
||||
export {}
|
||||
import { rm } from "node:fs/promises"
|
||||
import path from "node:path"
|
||||
import astro from "../astro.config"
|
||||
|
||||
const path = "dist/server/wrangler.json"
|
||||
const config = await Bun.file(path).json()
|
||||
const base = astro.base?.replace(/\/$/, "") ?? ""
|
||||
const snapshots = `dist/client${base}/docs-index`
|
||||
|
||||
await Promise.all(
|
||||
(await Array.fromAsync(new Bun.Glob("**/*.html").scan({ cwd: snapshots }))).map((file) =>
|
||||
rm(path.join(`dist/client${base}/docs`, file)),
|
||||
),
|
||||
)
|
||||
await rm(snapshots, { recursive: true })
|
||||
|
||||
const config = await Bun.file("dist/server/wrangler.json").json()
|
||||
|
||||
delete config.kv_namespaces
|
||||
delete config.images
|
||||
delete config.previews
|
||||
|
||||
await Bun.write(path, JSON.stringify(config))
|
||||
await Bun.write("dist/server/wrangler.json", JSON.stringify(config))
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
---
|
||||
import { render, type CollectionEntry } from "astro:content"
|
||||
import Callout from "./Callout.astro"
|
||||
import Card from "./Card.astro"
|
||||
import CardGroup from "./CardGroup.astro"
|
||||
import CodeBlock from "./CodeBlock.astro"
|
||||
import DocsLayout from "../layouts/DocsLayout.astro"
|
||||
|
||||
interface Props {
|
||||
entry: CollectionEntry<"docs">
|
||||
}
|
||||
|
||||
const entry = Astro.props.entry
|
||||
const rendered = await render(entry)
|
||||
---
|
||||
|
||||
<DocsLayout
|
||||
title={entry.data.title}
|
||||
description={entry.data.description}
|
||||
currentSlug={entry.id}
|
||||
headings={rendered.headings}
|
||||
showTableOfContents={entry.data.tableOfContents !== false}
|
||||
>
|
||||
<rendered.Content components={{ Callout, Card, CardGroup, CodeBlock }} />
|
||||
</DocsLayout>
|
||||
@@ -0,0 +1,21 @@
|
||||
---
|
||||
import { getCollection, type CollectionEntry } from "astro:content"
|
||||
import DocsPage from "../../docs/components/DocsPage.astro"
|
||||
|
||||
interface Props {
|
||||
entry: CollectionEntry<"docs">
|
||||
}
|
||||
|
||||
export async function getStaticPaths() {
|
||||
return (await getCollection("docs")).map((entry) => ({
|
||||
params: {
|
||||
slug: entry.id === "index" ? undefined : entry.id.replace(/\/index$/, ""),
|
||||
},
|
||||
props: { entry },
|
||||
}))
|
||||
}
|
||||
|
||||
export const prerender = true
|
||||
---
|
||||
|
||||
<DocsPage entry={Astro.props.entry} />
|
||||
@@ -1,36 +1,26 @@
|
||||
---
|
||||
import { getCollection, render, type CollectionEntry } from "astro:content"
|
||||
import Callout from "../../docs/components/Callout.astro"
|
||||
import Card from "../../docs/components/Card.astro"
|
||||
import CardGroup from "../../docs/components/CardGroup.astro"
|
||||
import CodeBlock from "../../docs/components/CodeBlock.astro"
|
||||
import DocsLayout from "../../docs/layouts/DocsLayout.astro"
|
||||
import { getEntry } from "astro:content"
|
||||
import DocsPage from "../../docs/components/DocsPage.astro"
|
||||
|
||||
interface Props {
|
||||
entry: CollectionEntry<"docs">
|
||||
export const prerender = false
|
||||
|
||||
const slug = Astro.params.slug || "index"
|
||||
const entry = (await getEntry("docs", slug)) ?? (await getEntry("docs", `${slug}/index`))
|
||||
if (!entry) return new Response("Not found", { status: 404, headers: { "Cache-Control": "no-store" } })
|
||||
|
||||
const headers = {
|
||||
"Cache-Control": "public, max-age=300, stale-while-revalidate=3600",
|
||||
Vary: "Accept",
|
||||
}
|
||||
|
||||
export async function getStaticPaths() {
|
||||
return (await getCollection("docs")).map((entry) => ({
|
||||
params: {
|
||||
slug: entry.id === "index" ? undefined : entry.id.replace(/\/index$/, ""),
|
||||
},
|
||||
props: { entry },
|
||||
}))
|
||||
const accept = Astro.request.headers.get("accept") ?? ""
|
||||
if (accept.includes("text/markdown") || accept.includes("text/x-markdown")) {
|
||||
return new Response(`# ${entry.data.title}\n\n${entry.body ?? ""}`, {
|
||||
headers: { ...headers, "Content-Type": "text/markdown; charset=utf-8" },
|
||||
})
|
||||
}
|
||||
|
||||
export const prerender = true
|
||||
|
||||
const entry = Astro.props.entry
|
||||
const rendered = await render(entry)
|
||||
Object.entries(headers).forEach(([name, value]) => Astro.response.headers.set(name, value))
|
||||
---
|
||||
|
||||
<DocsLayout
|
||||
title={entry.data.title}
|
||||
description={entry.data.description}
|
||||
currentSlug={entry.id}
|
||||
headings={rendered.headings}
|
||||
showTableOfContents={entry.data.tableOfContents !== false}
|
||||
>
|
||||
<rendered.Content components={{ Callout, Card, CardGroup, CodeBlock }} />
|
||||
</DocsLayout>
|
||||
<DocsPage entry={entry} />
|
||||
|
||||
@@ -1,5 +1,13 @@
|
||||
---
|
||||
export const prerender = false
|
||||
|
||||
return fetch("https://raw.githubusercontent.com/anomalyco/opencode/v2/install")
|
||||
const response = await fetch("https://raw.githubusercontent.com/anomalyco/opencode/v2/install")
|
||||
const headers = new Headers(response.headers)
|
||||
headers.set("Cache-Control", "no-store")
|
||||
|
||||
return new Response(response.body, {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
headers,
|
||||
})
|
||||
---
|
||||
|
||||
@@ -4,6 +4,9 @@
|
||||
"compatibility_date": "2026-07-25",
|
||||
"workers_dev": false,
|
||||
"preview_urls": false,
|
||||
"cache": {
|
||||
"enabled": true,
|
||||
},
|
||||
"assets": {
|
||||
"binding": "ASSETS",
|
||||
"directory": "./dist/client",
|
||||
|
||||
Reference in New Issue
Block a user