Skip to content

Commit

Permalink
fix: cache and cache bust font woff2 (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato authored Feb 28, 2024
1 parent 8d240e4 commit 4a41476
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions .licenserc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"api/testdata/",
"frontend/_fresh/",
"frontend/node_modules/",
"frontend/fresh.gen.ts",
"target/",
".gcs/",
"terraform/.terraform/",
Expand Down
37 changes: 35 additions & 2 deletions frontend/fresh.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,40 @@
// Copyright 2024 the JSR authors. All rights reserved. MIT license.
import { defineConfig } from "$fresh/server.ts";
import { defineConfig, Plugin } from "$fresh/server.ts";
import { asset } from "$fresh/runtime.ts";
import tailwind from "$fresh/plugins/tailwind.ts";
import { join } from "$std/path/mod.ts";

export default defineConfig({
plugins: [tailwind()],
plugins: [tailwind(), assetifyCssUrl()],
});

const CSS_URL_REGEX =
/url\((?:(?<quote>['"])(?<quoted>(?:(?!\k<quote>|\\).|\\.)*)\k<quote>|(?<unquoted>[^'")]*))\)/g;

// This plugin reads the generated style.css file from tailwind plugin and
// replaces the url() (for font paths) with paths that include asset queries for
// caching and cache busting.
function assetifyCssUrl() {
let outDir: string;
return {
name: "assetify-css-url",
buildStart(config) {
outDir = config.build.outDir;
},
async buildEnd() {
const stylePath = join(outDir, "static", "styles.css");
let styleCss = await Deno.readTextFile(stylePath);
styleCss = styleCss.replaceAll(CSS_URL_REGEX, (...args) => {
const groups = args.at(-1) as Record<string, string>;
let path: string;
if (groups.quoted) {
path = groups.quoted.replaceAll(/\\./g, (s) => JSON.parse(`"${s}"`));
} else {
path = groups.unquoted;
}
return `url(${JSON.stringify(asset(path))})`;
});
await Deno.writeTextFile(stylePath, styleCss);
},
} satisfies Plugin;
}
1 change: 0 additions & 1 deletion frontend/fresh.gen.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Copyright 2024 the JSR authors. All rights reserved. MIT license.
// DO NOT EDIT. This file is generated by Fresh.
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running `dev.ts`.
Expand Down

0 comments on commit 4a41476

Please sign in to comment.