-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: cache and cache bust font woff2 (#48)
- Loading branch information
1 parent
8d240e4
commit 4a41476
Showing
3 changed files
with
36 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters