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
+7 -2
View File
@@ -1,7 +1,12 @@
/** @jsx h */
import { h } from "../client_deps.ts";
import { asset, h } from "../client_deps.ts";
export default function Test(props: { message: string }) {
return <p>{props.message}</p>;
return (
<div>
<p>{props.message}</p>
<img src={asset("/image.png")} />
</div>
);
}
+7 -2
View File
@@ -1,7 +1,12 @@
/** @jsx h */
import { h } from "../client_deps.ts";
import { asset, h } from "../client_deps.ts";
export default function StaticPage() {
return <p>This is a static page.</p>;
return (
<div>
<p>This is a static page.</p>
<img src={asset("/image.png")} />
</div>
);
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

+31 -1
View File
@@ -137,6 +137,7 @@ Deno.test("/static page prerender", async () => {
assert(!body.includes(`main.js`));
assert(!body.includes(`island-test.js`));
assertStringIncludes(body, "<p>This is a static page.</p>");
assertStringIncludes(body, `src="/_frsh/static`);
assert(!body.includes("__FRSH_ISLAND_PROPS"));
});
@@ -181,7 +182,7 @@ Deno.test("/foo/:path*", async () => {
assert(body.includes("bar/baz"));
});
Deno.test("static file", async () => {
Deno.test("static file - by file path", async () => {
const resp = await router(new Request("https://fresh.deno.dev/foo.txt"));
assertEquals(resp.status, 200);
const body = await resp.text();
@@ -198,6 +199,35 @@ Deno.test("static file", async () => {
assertEquals(resp2.status, 304);
});
Deno.test("static file - by 'hashed' path", async () => {
// Check that the file path have the BUILD_ID
const resp = await router(new Request("https://fresh.deno.dev/"));
const body = await resp.text();
const imgFilePath = body.match(/img src="(.*?)"/)?.[1];
assert(imgFilePath);
assert(imgFilePath.includes(globalThis.__FRSH_BUILD_ID));
// check the static file is served corectly under its cacheable route
const resp2 = await router(
new Request(`https://fresh.deno.dev${imgFilePath}`),
);
const _ = await resp2.text();
assertEquals(resp2.status, 200);
assertEquals(
resp2.headers.get("cache-control"),
"public, max-age=31536000, immutable",
);
const resp3 = await router(
new Request(`https://fresh.deno.dev${imgFilePath}`, {
headers: {
"if-none-match": resp2.headers.get("etag")!,
},
}),
);
assertEquals(resp3.status, 304);
});
Deno.test("/params/:path*", async () => {
const resp = await router(
new Request("https://fresh.deno.dev/params/bar/baz"),