www: serve fresh code at /@:version/:path* (#265)
This commit is contained in:
Vendored
+3
-1
@@ -7,6 +7,8 @@
|
||||
|
||||
"preact": "https://esm.sh/preact@10.6.6",
|
||||
"preact/": "https://esm.sh/preact@10.6.6/",
|
||||
"preact-render-to-string": "https://esm.sh/preact-render-to-string@5.1.20?deps=preact@10.6.6"
|
||||
"preact-render-to-string": "https://esm.sh/preact-render-to-string@5.1.20?deps=preact@10.6.6",
|
||||
|
||||
"$semver/": "https://deno.land/x/semver@v1.4.0/"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
import * as $0 from "./routes/docs/[...slug].tsx";
|
||||
import * as $1 from "./routes/gfm.css.ts";
|
||||
import * as $2 from "./routes/index.tsx";
|
||||
import * as $3 from "./routes/raw.ts";
|
||||
import * as $$0 from "./islands/Counter.tsx";
|
||||
import * as $$1 from "./islands/LemonDrop.tsx";
|
||||
|
||||
@@ -13,6 +14,7 @@ const manifest = {
|
||||
"./routes/docs/[...slug].tsx": $0,
|
||||
"./routes/gfm.css.ts": $1,
|
||||
"./routes/index.tsx": $2,
|
||||
"./routes/raw.ts": $3,
|
||||
},
|
||||
islands: {
|
||||
"./islands/Counter.tsx": $$0,
|
||||
|
||||
+2
-1
@@ -5,6 +5,7 @@
|
||||
"$twind/": "https://esm.sh/twind@0.16.16/",
|
||||
"preact": "https://esm.sh/preact@10.6.6",
|
||||
"preact/": "https://esm.sh/preact@10.6.6/",
|
||||
"preact-render-to-string": "https://esm.sh/preact-render-to-string@5.1.20?deps=preact@10.6.6"
|
||||
"preact-render-to-string": "https://esm.sh/preact-render-to-string@5.1.20?deps=preact@10.6.6",
|
||||
"$semver/": "https://deno.land/x/semver@v1.4.0/"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
import { RouteConfig } from "$fresh/server.ts";
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
import { parse } from "$semver/mod.ts";
|
||||
import VERSIONS from "../../versions.json" assert { type: "json" };
|
||||
|
||||
const BASE_URL = "https://raw.githubusercontent.com/lucacasonato/fresh/";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(req, ctx) {
|
||||
const accept = req.headers.get("Accept");
|
||||
const isHTML = accept?.includes("text/html");
|
||||
const { version, path } = ctx.params;
|
||||
|
||||
const semver = parse(version, { includePrerelease: true, loose: false });
|
||||
if (!semver) {
|
||||
return new Response("Invalid version", { status: 400 });
|
||||
}
|
||||
|
||||
if (!VERSIONS.includes(semver.version)) {
|
||||
return new Response("Version not found", { status: 404 });
|
||||
}
|
||||
|
||||
const url = `${BASE_URL}${semver.version}/${path}`;
|
||||
const r = await fetch(url, { redirect: "manual" });
|
||||
const response = new Response(r.body, r);
|
||||
response.headers.delete("content-encoding");
|
||||
|
||||
if (response.status === 404) {
|
||||
return new Response(
|
||||
"404: Not Found. The requested fresh release or file do not exist.",
|
||||
{ status: 404 },
|
||||
);
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
response.headers.set("Content-Type", "text/plain");
|
||||
return response;
|
||||
}
|
||||
|
||||
if (isHTML) {
|
||||
response.headers.set("Content-Type", "text/plain");
|
||||
} else if (path.endsWith(".ts")) {
|
||||
response.headers.set("Content-Type", "application/typescript");
|
||||
} else if (path.endsWith(".js")) {
|
||||
response.headers.set("Content-Type", "application/javascript");
|
||||
} else if (path.endsWith(".tsx")) {
|
||||
response.headers.set("Content-Type", "text/tsx");
|
||||
} else if (path.endsWith(".jsx")) {
|
||||
response.headers.set("Content-Type", "text/jsx");
|
||||
} else if (path.endsWith(".json")) {
|
||||
response.headers.set("Content-Type", "application/json");
|
||||
} else if (path.endsWith(".wasm")) {
|
||||
response.headers.set("Content-Type", "application/wasm");
|
||||
} else {
|
||||
response.headers.set("Content-Type", "text/plain");
|
||||
}
|
||||
return response;
|
||||
},
|
||||
};
|
||||
|
||||
export const config: RouteConfig = {
|
||||
routeOverride: "/@:version/:path*",
|
||||
};
|
||||
Reference in New Issue
Block a user