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
+8 -1
View File
@@ -1,4 +1,5 @@
import { ComponentType, h, render } from "./deps.ts";
import { ComponentType, h, options, render } from "./deps.ts";
import { assetHashingHook } from "./utils.ts";
function createRootFragment(
parent: Element,
@@ -61,3 +62,9 @@ export function revive(islands: Record<string, ComponentType>) {
}
walk(document.body);
}
const originalHook = options.vnode;
options.vnode = (vnode) => {
assetHashingHook(vnode);
if (originalHook) originalHook(vnode);
};
+40
View File
@@ -1,3 +1,5 @@
import { VNode } from "./deps.ts";
export const INTERNAL_PREFIX = "/_frsh";
export const ASSET_CACHE_BUST_KEY = "__frsh_c";
@@ -28,3 +30,41 @@ export function asset(path: string) {
return path;
}
}
/** Apply the `asset` function to urls in a `srcset` attribute. */
export function assetSrcSet(srcset: string): string {
if (srcset.includes("(")) return srcset; // Bail if the srcset contains complicated syntax.
const parts = srcset.split(",");
const constructed = [];
for (const part of parts) {
const trimmed = part.trimStart();
const leadingWhitespace = part.length - trimmed.length;
if (trimmed === "") return srcset; // Bail if the srcset is malformed.
let urlEnd = trimmed.indexOf(" ");
if (urlEnd === -1) urlEnd = trimmed.length;
const leading = part.substring(0, leadingWhitespace);
const url = trimmed.substring(0, urlEnd);
const trailing = trimmed.substring(urlEnd);
constructed.push(leading + asset(url) + trailing);
}
return constructed.join(",");
}
export function assetHashingHook(
vnode: VNode<{
src?: string;
srcset?: string;
["data-fresh-disable-lock"]?: boolean;
}>,
) {
if (vnode.type === "img" || vnode.type === "source") {
const { props } = vnode;
if (props["data-fresh-disable-lock"]) return;
if (typeof props.src === "string") {
props.src = asset(props.src);
}
if (typeof props.srcset === "string") {
props.srcset = assetSrcSet(props.srcset);
}
}
}
+42 -1
View File
@@ -1,5 +1,5 @@
import { assertEquals } from "../../tests/deps.ts";
import { asset } from "./utils.ts";
import { asset, assetSrcSet } from "./utils.ts";
globalThis.__FRSH_BUILD_ID = "ID123";
@@ -17,3 +17,44 @@ Deno.test("asset", () => {
"https://example.com/logo.png",
);
});
Deno.test("assetSrcSet", () => {
assertEquals(assetSrcSet("/img.png"), "/img.png?__frsh_c=ID123");
assertEquals(
assetSrcSet("/img.png, /img.png 2x"),
"/img.png?__frsh_c=ID123, /img.png?__frsh_c=ID123 2x",
);
assertEquals(assetSrcSet("/img.png 1x"), "/img.png?__frsh_c=ID123 1x");
assertEquals(
assetSrcSet("/img.png 1x, /img.png 2x"),
"/img.png?__frsh_c=ID123 1x, /img.png?__frsh_c=ID123 2x",
);
assertEquals(
assetSrcSet("/img.png 1.5x, /img.png 3x"),
"/img.png?__frsh_c=ID123 1.5x, /img.png?__frsh_c=ID123 3x",
);
//test with queries
assertEquals(
assetSrcSet("/img.png?w=140, /img.png?w=200 2x"),
"/img.png?w=140&__frsh_c=ID123, /img.png?w=200&__frsh_c=ID123 2x",
);
// test with extra spaces
assertEquals(
assetSrcSet("/img-s.png 300w, /img-l.png 600w , /img-xl.png 900w"),
"/img-s.png?__frsh_c=ID123 300w, /img-l.png?__frsh_c=ID123 600w , /img-xl.png?__frsh_c=ID123 900w",
);
// test with ( syntax
assertEquals(
assetSrcSet("/img.png ( 140,0w)"),
"/img.png ( 140,0w)",
);
// test with invalid parts
assertEquals(
assetSrcSet("/img.png,, /img-s.png 300w"),
"/img.png,, /img-s.png 300w",
);
});
+2
View File
@@ -18,6 +18,7 @@ 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 { assetHashingHook } from "../runtime/utils.ts";
export interface RenderOptions<Data> {
page: Page<Data> | UnknownPage | ErrorPage;
@@ -296,6 +297,7 @@ let ISLAND_PROPS: unknown[] = [];
const originalHook = options.vnode;
let ignoreNext = false;
options.vnode = (vnode) => {
assetHashingHook(vnode);
const originalType = vnode.type as ComponentType<unknown>;
if (typeof vnode.type === "function") {
const island = ISLANDS.find((island) => island.component === originalType);