Skip to content

Commit 44604e7

Browse files
committed
Fix cache path creation
1 parent 5109c4a commit 44604e7

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

index.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function makeAssetCachePath(cacheDir, cacheKey) {
3333
return {
3434
dir1: bucket1HexString,
3535
dir2: bucket2HexString,
36+
dir3: hash.toString(),
3637
path: path.join(
3738
cacheDir,
3839
bucket1HexString,
@@ -61,17 +62,17 @@ const middleWare = (module.exports = function(options) {
6162
? options.cacheDir
6263
: path.join(process.cwd(), "/tmp");
6364

64-
const { dir1, dir2, path: assetCachePath } = middleWare.makeAssetCachePath(
65+
const {
66+
dir1,
67+
dir2,
68+
dir3,
69+
path: assetCachePath
70+
} = middleWare.makeAssetCachePath(
6571
options.cacheDir,
6672
res.locals.cacheKey || res.locals.fetchUrl
6773
);
6874

6975
try {
70-
// node 10 supports recursive: true, but who knows?
71-
middleWare.makeDirIfNotExists(options.cacheDir);
72-
middleWare.makeDirIfNotExists(path.join(options.cacheDir, dir1));
73-
middleWare.makeDirIfNotExists(path.join(options.cacheDir, dir1, dir2));
74-
7576
if (fs.existsSync(assetCachePath)) {
7677
const firstFile = fs.readdirSync(assetCachePath)[0];
7778

@@ -89,6 +90,14 @@ const middleWare = (module.exports = function(options) {
8990

9091
res.locals.buffer = fs.readFileSync(`${assetCachePath}/${firstFile}`);
9192
} else {
93+
// node 10 supports recursive: true, but who knows?
94+
middleWare.makeDirIfNotExists(options.cacheDir);
95+
middleWare.makeDirIfNotExists(path.join(options.cacheDir, dir1));
96+
middleWare.makeDirIfNotExists(path.join(options.cacheDir, dir1, dir2));
97+
middleWare.makeDirIfNotExists(
98+
path.join(options.cacheDir, dir1, dir2, dir3)
99+
);
100+
92101
const blob = await (await fetch(res.locals.fetchUrl)).blob();
93102

94103
const fileName = middleWare.encodeAssetCacheName(blob.type, blob.size);

0 commit comments

Comments
 (0)