feat: add support for static asset caching (#133)

This commit is contained in:
Sylvain Cau
2022-03-16 16:49:47 +01:00
committed by GitHub
parent 8aa1b250b8
commit 6cbcc27646
9 changed files with 93 additions and 20 deletions
+2
View File
@@ -1,3 +1,4 @@
import { BUILD_ID } from "./constants.ts";
import { denoPlugin, esbuild, toFileUrl } from "./deps.ts";
import { Island } from "./types.ts";
@@ -40,6 +41,7 @@ export class Bundler {
await ensureEsbuildInialized();
const bundle = await esbuild.build({
bundle: true,
define: { __FRSH_BUILD_ID: `"${BUILD_ID}"` },
entryPoints,
format: "esm",
metafile: true,
+7 -1
View File
@@ -1,4 +1,5 @@
export const INTERNAL_PREFIX = "/_frsh";
import { INTERNAL_PREFIX } from "../runtime/utils.ts";
export const REFRESH_JS_URL = `${INTERNAL_PREFIX}/refresh.js`;
export const ALIVE_URL = `${INTERNAL_PREFIX}/alive`;
export const BUILD_ID = Deno.env.get("DENO_DEPLOYMENT_ID") ||
@@ -10,8 +11,13 @@ export function bundleAssetUrl(path: string) {
return `${INTERNAL_PREFIX}${JS_PREFIX}/${BUILD_ID}${path}`;
}
globalThis.__FRSH_BUILD_ID = BUILD_ID;
declare global {
interface Crypto {
randomUUID(): string;
}
// deno-lint-ignore no-var
var __FRSH_BUILD_ID: string;
}
+26 -12
View File
@@ -10,13 +10,7 @@ import {
} from "./deps.ts";
import { Manifest } from "./mod.ts";
import { Bundler } from "./bundle.ts";
import {
ALIVE_URL,
BUILD_ID,
INTERNAL_PREFIX,
JS_PREFIX,
REFRESH_JS_URL,
} from "./constants.ts";
import { ALIVE_URL, BUILD_ID, JS_PREFIX, REFRESH_JS_URL } from "./constants.ts";
import DefaultErrorHandler from "./default_error_page.tsx";
import {
AppModule,
@@ -36,6 +30,7 @@ import {
import { render as internalRender } from "./render.tsx";
import { ContentSecurityPolicyDirectives, SELF } from "../runtime/csp.ts";
import { h } from "../runtime/deps.ts";
import { asset, INTERNAL_PREFIX } from "../runtime/utils.ts";
interface StaticFile {
/** The URL to the static file on disk. */
@@ -319,6 +314,9 @@ export class ServerContext {
}
// Add the static file routes.
// each files has 2 static routes:
// - one serving the file at its location without a "cache bursting" mechanism
// - one containing the BUILD_ID in the path that can be cached
for (
const { localUrl, path, size, contentType, etag } of this.#staticFiles
) {
@@ -329,6 +327,15 @@ export class ServerContext {
contentType,
etag,
);
const hashedRoute = sanitizePathToRegex(asset(path));
routes[`GET@${hashedRoute}`] = this.#staticFileHandler(
localUrl,
size,
contentType,
etag,
true,
);
}
const genRender = <Data = undefined>(
@@ -442,18 +449,25 @@ export class ServerContext {
size: number,
contentType: string,
etag: string,
isCacheable?: boolean,
): router.MatchHandler {
return async (req: Request) => {
if (req.headers.get("if-none-match") === etag) {
return new Response(null, { status: 304 });
} else {
const resp = await fetch(localUrl);
const headers = new Headers({
"content-type": contentType,
"content-length": String(size),
"etag": etag,
});
if (isCacheable) {
headers.set("Cache-Control", "public, max-age=31536000, immutable");
}
return new Response(resp.body, {
headers: {
"content-type": contentType,
"content-length": String(size),
"etag": etag,
},
headers,
});
}
};
+1 -2
View File
@@ -5,7 +5,6 @@ import {
ComponentType,
h,
options,
VNode,
} from "../runtime/deps.ts";
import {
AppModule,
@@ -18,7 +17,7 @@ import {
import { HEAD_CONTEXT } from "../runtime/head.ts";
import { CSP_CONTEXT, nonce, NONE, UNSAFE_INLINE } from "../runtime/csp.ts";
import { ContentSecurityPolicy } from "../runtime/csp.ts";
import { bundleAssetUrl } from "./constants.ts";
import { BUILD_ID, bundleAssetUrl } from "./constants.ts";
export interface RenderOptions<Data> {
page: Page<Data> | UnknownPage | ErrorPage;