Skip to content

Commit

Permalink
integrated cacheBusterLinks into htmlRewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-preston committed Jul 18, 2024
1 parent 206b2e4 commit 19f9e4c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 41 deletions.
34 changes: 14 additions & 20 deletions _all-pages/cacheBuster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,31 @@ import { encodeHex } from "lume/deps/hex.ts";

const urlMap: { [k: string]: string } = {};

export const cacheBusterAssets = async (pages: Array<Lume.Page>) => {
for (const page of pages) {
const buffer = new TextEncoder().encode(page.content! as string);
const hash = encodeHex(await crypto.subtle.digest("SHA-1", buffer));
const oldUrl = page.data.url;
const extension = oldUrl.split(".").pop();
page.data.url = `/${hash}.${extension}`;
urlMap[oldUrl] = page.data.url;
}
export const cacheBusterUrl = async (
content: string,
url: string
): Promise<string> => {
const buffer = new TextEncoder().encode(content);
const hash = encodeHex(await crypto.subtle.digest("SHA-1", buffer));
const extension = url.split(".").pop();
const newUrl = `/${hash}.${extension}`;
urlMap[url] = newUrl;
return newUrl;
};

export const cacheBusterLinks = (pages: Array<Lume.Page>) => {
let basename = "";
let document: Document | undefined;

export const cacheBusterLinks = (document: Document, basename: string) => {
const domModify = (querySelector: string, urlAttribute: string) => {
const links = document!.querySelectorAll(querySelector);
for (const link of links) {
const oldUrl = link.getAttribute(urlAttribute)!;
const newUrl = urlMap[oldUrl];
if (typeof newUrl == "undefined") {
if (newUrl == undefined) {
throw new Error(`Can't find ${oldUrl} from ${basename}`);
}
link.setAttribute(urlAttribute, newUrl);
}
};

for (const page of pages) {
basename = page.data.basename;
document = page.document;
domModify('link[rel="stylesheet"]', "href");
domModify("script", "src");
}
domModify('link[rel="stylesheet"]', "href");
domModify("script", "src");
};
13 changes: 0 additions & 13 deletions _all-pages/htmlFormat.ts

This file was deleted.

21 changes: 21 additions & 0 deletions _all-pages/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { cacheBusterLinks, cacheBusterUrl } from "./cacheBuster.ts";
import { hljsWorkaround } from "./hljsWorkaround.ts";
import { stripWhitespace } from "./stripWhitespace.ts";

export const assetProcess = async (pages: Array<Lume.Page>) => {
for (const page of pages) {
page.data.url = await cacheBusterUrl(
page.content! as string,
page.data.url
);
}
}

export const htmlRewrite = (pages: Array<Lume.Page>) => {
for (const page of pages) {
const document = page.document!;
cacheBusterLinks(document, page.data.basename);
hljsWorkaround(document);
stripWhitespace(document);
}
};
11 changes: 3 additions & 8 deletions _config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import lume from "lume/mod.ts";
import codeHighlight from "lume/plugins/code_highlight.ts";
import {
cacheBusterAssets,
cacheBusterLinks
} from "./_all-pages/cacheBuster.ts";
import { htmlFormat } from "./_all-pages/htmlFormat.ts";
import { assetProcess, htmlRewrite } from "./_all-pages/mod.ts";
import { transformer as markdownTransform } from "./articles/_build/mod.ts";
import { esBuildPlugin } from "./cover-pic/_build.ts";
import { postCss } from "./style/_postCss.ts";
Expand All @@ -22,9 +18,8 @@ siteBuilder
.use(codeHighlight())
.use(esBuildPlugin)
.use(postCss)
.process([".css", ".ts"], cacheBusterAssets)
.process([".html"], cacheBusterLinks)
.process([".html"], htmlFormat)
.process([".css", ".ts"], assetProcess)
.process([".html"], htmlRewrite)
.copy("fixed", ".");

// biome-ignore lint/style/noDefaultExport: required by Lume API
Expand Down

0 comments on commit 19f9e4c

Please sign in to comment.