Skip to content

Commit

Permalink
fix: add versioning in output file and to loaded urls
Browse files Browse the repository at this point in the history
  • Loading branch information
lowlighter committed Jul 2, 2024
1 parent dd1449f commit addc87e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
3 changes: 2 additions & 1 deletion api/brew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { bundle } from "jsr:@libs/bundle@5/css"
import { banner as _banner } from "../app/build/css.ts"
import { STATUS_CODE, STATUS_TEXT } from "jsr:@std/[email protected]"
import { version } from "../app/build/version.ts"

/** API: Minify css */
export default async function (request: Request) {
Expand All @@ -14,7 +15,7 @@ export default async function (request: Request) {
}
try {
const body = await request.text()
const banner = _banner.replace("matcha.css\n", `matcha.css — Custom build (${new Date().toDateString()})\n`)
const banner = _banner.replace(`matcha.css${version}\n`, `matcha.css${version} — Custom build (${new Date().toDateString()})\n`)
const bundled = await bundle(body, { minify: true, banner, rules: { "no-descending-specificity": false, "no-duplicate-selectors": false, "declaration-no-important": false } })
return new Response(bundled, { headers: { "Content-Type": "text/css" } })
} catch (error) {
Expand Down
3 changes: 2 additions & 1 deletion app/build/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { expandGlob } from "jsr:@std/[email protected]"
import { fromFileUrl } from "jsr:@std/[email protected]"
import { bundle } from "jsr:@libs/bundle@5/css"
import { root as _root } from "./root.ts"
import { version } from "./version.ts"

/** Banner */
export const banner = [
"matcha.css",
`matcha.css${version}`,
`Copyright © ${new Date().getFullYear()} Lecoq Simon (@lowlighter)`,
"MIT license — https://github.com/lowlighter/matcha",
].join("\n")
Expand Down
8 changes: 8 additions & 0 deletions app/build/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { default as hlmd } from "https://esm.sh/[email protected]/lib/language
import { default as hlsh } from "https://esm.sh/[email protected]/lib/languages/diff"
import { DOMParser, type HTMLDocument } from "https://deno.land/x/[email protected]/deno-dom-wasm.ts"
import { gzipSize } from "https://deno.land/x/[email protected]/mod.ts"
import { version } from "./version.ts"
syntax.registerLanguage("xml", hlxml)
syntax.registerLanguage("css", hlcss)
syntax.registerLanguage("lisp", hllisp)
Expand Down Expand Up @@ -187,6 +188,13 @@ async function template({ remove }: { remove?: { parent?: string[]; selectors?:
}
const document = new DOMParser().parseFromString(html, "text/html")!
highlight(document)
// Force uncached resources on version changes
document.querySelectorAll('link[rel="stylesheet"]').forEach((element) => {
const link = element as unknown as HTMLLinkElement
if (link.getAttribute("href")?.startsWith("/")) {
link.setAttribute("href", `${link.getAttribute("href")}?v=${version}`)
}
})
// Include scripts
await Promise.all(
Array.from(document.querySelectorAll("script[data-script]")).map(async (_element) => {
Expand Down

0 comments on commit addc87e

Please sign in to comment.