feat: automatically apply asset caching to img src and srcset (#184)

This commit is contained in:
Sylvain Cau
2022-05-19 19:19:59 +02:00
committed by GitHub
parent c1a9d53b18
commit e8b1e229b3
11 changed files with 254 additions and 50 deletions
+36 -2
View File
@@ -219,9 +219,11 @@ Deno.test("static file - by file path", async () => {
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 resp = await router(
new Request("https://fresh.deno.dev/assetsCaching"),
);
const body = await resp.text();
const imgFilePath = body.match(/img src="(.*?)"/)?.[1];
const imgFilePath = body.match(/img id="img-with-hashing" src="(.*?)"/)?.[1];
assert(imgFilePath);
assert(imgFilePath.includes(`?__frsh_c=${globalThis.__FRSH_BUILD_ID}`));
@@ -244,6 +246,38 @@ Deno.test("static file - by 'hashed' path", async () => {
}),
);
assertEquals(resp3.status, 304);
// ensure asset hook is not applied on file explicitly excluded with attribute
const imgFilePathWithNoCache = body.match(
/img id="img-without-hashing" src="(.*?)"/,
)?.[1];
assert(imgFilePathWithNoCache);
assert(
!imgFilePathWithNoCache.includes(globalThis.__FRSH_BUILD_ID),
"img-without-hashing",
);
// ensure asset hook is applied on img within an island
const imgInIsland = body.match(/img id="img-in-island" src="(.*?)"/)?.[1];
assert(imgInIsland);
assert(imgInIsland.includes(globalThis.__FRSH_BUILD_ID), "img-in-island");
// verify that the asset hook is applied to the srcset
const imgInIslandSrcSet = body.match(/srcset="(.*?)"/)?.[1];
assert(imgInIslandSrcSet);
console.log(imgInIslandSrcSet);
assert(
imgInIslandSrcSet.includes(globalThis.__FRSH_BUILD_ID),
"img-in-island-srcset",
);
// verify that the asset hook is not applied to img outside reference out of the static folder
const imgMissing = body.match(/img id="img-missing" src="(.*?)"/)?.[1];
assert(imgMissing);
assert(
!imgMissing.includes(globalThis.__FRSH_BUILD_ID),
"Applying hash on unknown asset",
);
});
Deno.test("/params/:path*", async () => {