-
-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ add fetching grapher configs by UUID
- Loading branch information
Showing
3 changed files
with
101 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Env } from "../thumbnail/[slug].js" | ||
import { fetchGrapherConfig } from "../../_common/grapherRenderer.js" | ||
import { IRequestStrict, Router, error, cors } from "itty-router" | ||
const { preflight, corsify } = cors({ | ||
allowMethods: "GET", | ||
}) | ||
|
||
const router = Router<IRequestStrict, [URL, Env, string]>({ | ||
before: [preflight], | ||
finally: [corsify], | ||
}) | ||
router | ||
.get( | ||
"/grapher/by-uuid/:uuid.config.json", | ||
async ({ params: { uuid } }, { searchParams }, env, etag) => | ||
handleConfigRequest(uuid, searchParams, env, etag) | ||
) | ||
.all("*", () => error(404, "Route not defined")) | ||
|
||
export const onRequestGet: PagesFunction = async (context) => { | ||
// Makes it so that if there's an error, we will just deliver the original page before the HTML rewrite. | ||
// Only caveat is that redirects will not be taken into account for some reason; but on the other hand the worker is so simple that it's unlikely to fail. | ||
context.passThroughOnException() | ||
const { request, env } = context | ||
const url = new URL(request.url) | ||
|
||
return router | ||
.fetch( | ||
request, | ||
url, | ||
{ ...env, url }, | ||
request.headers.get("if-none-match") | ||
) | ||
.catch((e) => error(500, e)) | ||
} | ||
|
||
async function handleConfigRequest( | ||
uuid: string, | ||
searchParams: URLSearchParams, | ||
env: Env, | ||
etag: string | undefined | ||
) { | ||
const shouldCache = searchParams.get("nocache") === null | ||
console.log("Preparing json response for uuid ", uuid) | ||
|
||
const grapherPageResp = await fetchGrapherConfig( | ||
{ type: "uuid", id: uuid }, | ||
env, | ||
etag | ||
) | ||
|
||
if (grapherPageResp.status === 304) { | ||
return new Response(null, { status: 304 }) | ||
} | ||
|
||
console.log("Grapher page response", grapherPageResp.grapherConfig.title) | ||
|
||
const cacheControl = shouldCache | ||
? "public, s-maxage=3600, max-age=0, must-revalidate" | ||
: "public, s-maxage=0, max-age=0, must-revalidate" | ||
|
||
return new Response(JSON.stringify(grapherPageResp.grapherConfig), { | ||
headers: { | ||
"content-type": "application/json", | ||
"Cache-Control": cacheControl, | ||
ETag: grapherPageResp.etag, | ||
}, | ||
}) | ||
} |