Skip to content

Commit d2f3916

Browse files
committed
simplify manifrst creation
1 parent c4e8ded commit d2f3916

1 file changed

Lines changed: 10 additions & 40 deletions

File tree

src/presets/vercel/immutable.ts

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { defu } from "defu";
21
import { glob } from "tinyglobby";
3-
import { resolve } from "pathe";
2+
import { resolve } from "pathe";
43
import { joinURL, withLeadingSlash } from "ufo";
5-
import type { Nitro, NitroRouteRules } from "nitro/types";
4+
import type { Nitro } from "nitro/types";
65
import { writeFile } from "../_utils/fs.ts";
76

87
// Immutable Static Files
@@ -37,54 +36,25 @@ export async function generateImmutableManifest(nitro: Nitro) {
3736
return;
3837
}
3938

40-
const isImmutable = createImmutableMatcher(nitro);
41-
42-
// Walk every emitted static file and hash the immutable (content-addressed)
43-
// ones. The output public dir already includes the site `baseURL`, so scanned
44-
// paths are relative to it.
4539
const publicDir = nitro.options.output.publicDir;
46-
const files = await glob("**", { cwd: publicDir, absolute: false, dot: true });
40+
const files = await glob(`${IMMUTABLE_DIR}/**`, {
41+
cwd: publicDir,
42+
absolute: false,
43+
dot: true,
44+
});
4745

48-
const hashes: Record<string, string> = {};
46+
const manifest: ImmutableManifest = { version: 1, hashes: {} };
4947
for (const file of files) {
5048
const pathname = withLeadingSlash(file);
51-
if (!isImmutable(pathname)) {
52-
continue;
53-
}
54-
const url = joinURL(nitro.options.baseURL, pathname);
55-
hashes[url] = ""
49+
manifest.hashes[joinURL(nitro.options.baseURL, pathname)] = "";
5650
}
5751

58-
const manifest: ImmutableManifest = { version: 1, hashes };
5952
await writeFile(
6053
resolve(nitro.options.output.dir, IMMUTABLE_MANIFEST),
6154
JSON.stringify(manifest, null, 2)
6255
);
6356

6457
nitro.logger
6558
.withTag("vercel")
66-
.info(`Generated immutable manifest (${Object.keys(hashes).length} files).`);
67-
}
68-
69-
// A static file is immutable when it is served with a long-lived `immutable`
70-
// cache-control. That covers content-addressed bundler output emitted under an
71-
// assets dir (marked via a route rule, e.g. Vite's `/assets/**`) as well as
72-
// non-fallthrough public asset dirs, while excluding user-authored files in the
73-
// fallthrough `public/` dir.
74-
function createImmutableMatcher(nitro: Nitro): (pathname: string) => boolean {
75-
const immutableBaseURLs = nitro.options.publicAssets
76-
.filter((asset) => !asset.fallthrough)
77-
.map((asset) => withLeadingSlash(asset.baseURL || "/"))
78-
.filter((baseURL) => baseURL !== "/");
79-
80-
const getRouteRules = (pathname: string) =>
81-
defu({}, ...nitro.routing.routeRules.matchAll("", pathname).reverse()) as NitroRouteRules;
82-
83-
return (pathname: string) => {
84-
if (immutableBaseURLs.some((baseURL) => pathname.startsWith(baseURL + "/"))) {
85-
return true;
86-
}
87-
const cacheControl = getRouteRules(pathname).headers?.["cache-control"];
88-
return !!cacheControl && cacheControl.includes("immutable");
89-
};
59+
.info(`Generated immutable manifest (${Object.keys(manifest.hashes).length} files).`);
9060
}

0 commit comments

Comments
 (0)