This repository has been archived by the owner on Jan 28, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 463
/
Copy pathreadAssetsDirectory.ts
63 lines (55 loc) · 1.69 KB
/
readAssetsDirectory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import path from "path";
import fs from "fs-extra";
const IMMUTABLE_CACHE_CONTROL_HEADER = "public, max-age=31536000, immutable";
const SERVER_CACHE_CONTROL_HEADER =
"public, max-age=0, s-maxage=2678400, must-revalidate";
const DEFAULT_PUBLIC_DIR_CACHE_CONTROL =
"public, max-age=31536000, must-revalidate";
type CacheConfig = Record<
string,
{
cacheControl: string;
path: string;
}
>;
const filterNonExistentPathKeys = (config: CacheConfig) => {
return Object.keys(config).reduce(
(newConfig, nextConfigKey) => ({
...newConfig,
...(fs.pathExistsSync(config[nextConfigKey].path)
? { [nextConfigKey]: config[nextConfigKey] }
: {})
}),
{} as CacheConfig
);
};
const readAssetsDirectory = (options: {
assetsDirectory: string;
}): CacheConfig => {
const { assetsDirectory } = options;
const publicFiles = path.join(assetsDirectory, "public");
const staticFiles = path.join(assetsDirectory, "static");
const staticPages = path.join(assetsDirectory, "static-pages");
const nextData = path.join(assetsDirectory, "_next", "data");
const nextStatic = path.join(assetsDirectory, "_next", "static");
return filterNonExistentPathKeys({
publicFiles: {
path: publicFiles,
cacheControl: DEFAULT_PUBLIC_DIR_CACHE_CONTROL
},
staticFiles: {
path: staticFiles,
cacheControl: DEFAULT_PUBLIC_DIR_CACHE_CONTROL
},
staticPages: {
path: staticPages,
cacheControl: SERVER_CACHE_CONTROL_HEADER
},
nextData: { path: nextData, cacheControl: SERVER_CACHE_CONTROL_HEADER },
nextStatic: {
path: nextStatic,
cacheControl: IMMUTABLE_CACHE_CONTROL_HEADER
}
});
};
export { readAssetsDirectory };