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 -34
View File
@@ -9,23 +9,24 @@ import * as $3 from "./routes/_app.tsx";
import * as $4 from "./routes/_middleware.ts";
import * as $5 from "./routes/_render.ts";
import * as $6 from "./routes/api/get_only.ts";
import * as $7 from "./routes/books/[id].tsx";
import * as $8 from "./routes/connInfo.ts";
import * as $9 from "./routes/failure.ts";
import * as $10 from "./routes/index.tsx";
import * as $11 from "./routes/intercept.tsx";
import * as $12 from "./routes/intercept_args.tsx";
import * as $13 from "./routes/layeredMdw/_middleware.ts";
import * as $14 from "./routes/layeredMdw/layer2-no-mw/without_mw.ts";
import * as $15 from "./routes/layeredMdw/layer2/_middleware.ts";
import * as $16 from "./routes/layeredMdw/layer2/abc.ts";
import * as $17 from "./routes/layeredMdw/layer2/layer3/[id].ts";
import * as $18 from "./routes/layeredMdw/layer2/layer3/_middleware.ts";
import * as $19 from "./routes/middleware_root.ts";
import * as $20 from "./routes/params.tsx";
import * as $21 from "./routes/props/[id].tsx";
import * as $22 from "./routes/static.tsx";
import * as $23 from "./routes/wildcard.tsx";
import * as $7 from "./routes/assetsCaching/index.tsx";
import * as $8 from "./routes/books/[id].tsx";
import * as $9 from "./routes/connInfo.ts";
import * as $10 from "./routes/failure.ts";
import * as $11 from "./routes/index.tsx";
import * as $12 from "./routes/intercept.tsx";
import * as $13 from "./routes/intercept_args.tsx";
import * as $14 from "./routes/layeredMdw/_middleware.ts";
import * as $15 from "./routes/layeredMdw/layer2-no-mw/without_mw.ts";
import * as $16 from "./routes/layeredMdw/layer2/_middleware.ts";
import * as $17 from "./routes/layeredMdw/layer2/abc.ts";
import * as $18 from "./routes/layeredMdw/layer2/layer3/[id].ts";
import * as $19 from "./routes/layeredMdw/layer2/layer3/_middleware.ts";
import * as $20 from "./routes/middleware_root.ts";
import * as $21 from "./routes/params.tsx";
import * as $22 from "./routes/props/[id].tsx";
import * as $23 from "./routes/static.tsx";
import * as $24 from "./routes/wildcard.tsx";
import * as $$0 from "./islands/Test.tsx";
const manifest = {
@@ -37,23 +38,24 @@ const manifest = {
"./routes/_middleware.ts": $4,
"./routes/_render.ts": $5,
"./routes/api/get_only.ts": $6,
"./routes/books/[id].tsx": $7,
"./routes/connInfo.ts": $8,
"./routes/failure.ts": $9,
"./routes/index.tsx": $10,
"./routes/intercept.tsx": $11,
"./routes/intercept_args.tsx": $12,
"./routes/layeredMdw/_middleware.ts": $13,
"./routes/layeredMdw/layer2-no-mw/without_mw.ts": $14,
"./routes/layeredMdw/layer2/_middleware.ts": $15,
"./routes/layeredMdw/layer2/abc.ts": $16,
"./routes/layeredMdw/layer2/layer3/[id].ts": $17,
"./routes/layeredMdw/layer2/layer3/_middleware.ts": $18,
"./routes/middleware_root.ts": $19,
"./routes/params.tsx": $20,
"./routes/props/[id].tsx": $21,
"./routes/static.tsx": $22,
"./routes/wildcard.tsx": $23,
"./routes/assetsCaching/index.tsx": $7,
"./routes/books/[id].tsx": $8,
"./routes/connInfo.ts": $9,
"./routes/failure.ts": $10,
"./routes/index.tsx": $11,
"./routes/intercept.tsx": $12,
"./routes/intercept_args.tsx": $13,
"./routes/layeredMdw/_middleware.ts": $14,
"./routes/layeredMdw/layer2-no-mw/without_mw.ts": $15,
"./routes/layeredMdw/layer2/_middleware.ts": $16,
"./routes/layeredMdw/layer2/abc.ts": $17,
"./routes/layeredMdw/layer2/layer3/[id].ts": $18,
"./routes/layeredMdw/layer2/layer3/_middleware.ts": $19,
"./routes/middleware_root.ts": $20,
"./routes/params.tsx": $21,
"./routes/props/[id].tsx": $22,
"./routes/static.tsx": $23,
"./routes/wildcard.tsx": $24,
},
islands: {
"./islands/Test.tsx": $$0,
+7 -2
View File
@@ -1,12 +1,17 @@
/** @jsx h */
import { asset, h } from "../client_deps.ts";
import { h } from "../client_deps.ts";
export default function Test(props: { message: string }) {
return (
<div>
<p>{props.message}</p>
<img src={asset("/image.png")} />
<img
id="img-in-island"
src="/image.png"
srcset="/image.png 1x"
height={130}
/>
</div>
);
}
@@ -0,0 +1,30 @@
/** @jsx h */
import { asset, h } from "../../client_deps.ts";
import Test from "../../islands/Test.tsx";
export default function Home() {
return (
<div>
<div style={{ marginTop: 20 }}>img-with-hashing</div>
<img id="img-with-hashing" src="/image.png" height={130} />
<div style={{ marginTop: 20 }}>img-without-hashing</div>
<img
id="img-without-hashing"
src="/image.png"
data-fresh-disable-lock
height={130}
/>
<Test message="In island" />
<div style={{ marginTop: 20 }}>img-external</div>
<img
id="img-missing"
src="https://fresh.deno.dev/favicon.ico"
height={130}
/>
</div>
);
}
+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 () => {