Skip to content
This repository was archived by the owner on Jan 2, 2022. It is now read-only.

Commit 96f4261

Browse files
committed
feat: support custom build dir names and paths
Signed-off-by: Jonah Snider <[email protected]>
1 parent 7f8d4b3 commit 96f4261

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

readme.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![Build Status](https://github.com/pizzafox/netlify-cache-nextjs/workflows/CI/badge.svg)](https://github.com/pizzafox/netlify-cache-nextjs/actions)
55
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo)
66

7-
Cache the `.next` folder in Netlify builds.
7+
Cache the Next.js build folder in Netlify builds.
88
A [Netlify plugin](https://www.netlify.com/build/plugins-beta/) which is [published on npm](https://www.npmjs.com/package/netlify-plugin-cache-nextjs).
99

1010
## Usage
@@ -20,6 +20,20 @@ To install, add the following lines to your `netlify.toml` file:
2020
package = "netlify-plugin-cache-nextjs"
2121
```
2222

23+
And if you want to configure the plugin:
24+
25+
```toml
26+
[[plugins]]
27+
package = "netlify-plugin-cache-nextjs"
28+
29+
# These options will cache the build directory at `${NETLIFY_BASE}/frontend/.next-build`
30+
[plugins.inputs]
31+
# The path to the build directory
32+
build_dir_path = "frontend"
33+
# Custom build directory if you aren't using `.next` (https://nextjs.org/docs/api-reference/next.config.js/setting-a-custom-build-directory)
34+
custom_build_dir_name = ".next-build"
35+
```
36+
2337
Note: The `[[plugins]]` line is required for each plugin, even if you have other plugins in your `netlify.toml` file already.
2438

2539
## Contributing

src/index.ts

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,53 @@ interface NetlifyUtils {
77
};
88
}
99

10+
interface NetlifyInputs {
11+
custom_build_dir_name?: string;
12+
build_dir_path?: string;
13+
}
14+
1015
interface NetlifyOpts {
1116
utils: NetlifyUtils;
1217
netlifyConfig: {build: {base: string}};
18+
inputs: NetlifyInputs;
1319
}
1420

15-
const buildCachePath = '.next';
16-
const manifestPath = joinPaths(buildCachePath, 'build-manifest.json');
21+
function generateAbsolutePaths(options: Pick<NetlifyOpts, 'inputs' | 'netlifyConfig'>) {
22+
/** The name of the build folder. `.next`, unless specially configured. */
23+
const buildDirName = options.inputs.custom_build_dir_name ?? '.next';
24+
/** The directory the build folder is in. Defaults to current directory, although some larger repositories might keep this in a `frontend` folder. */
25+
const buildDirPathFromProject = options.inputs.build_dir_path ?? '.';
26+
27+
/** The absolute path to the build folder for Next.js. */
28+
const absoluteBuildDirPath = joinPaths(options.netlifyConfig.build.base, buildDirPathFromProject, buildDirName);
29+
/** The absolute path to the build manifest Next.js uses. */
30+
const manifestPath = joinPaths(absoluteBuildDirPath, 'build-manifest.json');
31+
32+
return {
33+
absolute: {
34+
buildDir: absoluteBuildDirPath,
35+
manifest: manifestPath
36+
},
37+
buildDirName
38+
};
39+
}
1740

1841
module.exports = {
1942
name: 'cache-nextjs',
2043
// Restore file/directory cached in previous builds.
2144
// Does not do anything if:
2245
// - the file/directory already exists locally
2346
// - the file/directory has not been cached yet
24-
async onPreBuild({utils, netlifyConfig}: NetlifyOpts) {
25-
const directory = joinPaths(netlifyConfig.build.base, buildCachePath);
26-
27-
const success = await utils.cache.restore(directory, {
28-
digest: [joinPaths(netlifyConfig.build.base, manifestPath)]
47+
async onPreBuild({utils, netlifyConfig, inputs}: NetlifyOpts) {
48+
const paths = generateAbsolutePaths({netlifyConfig, inputs});
49+
const success = await utils.cache.restore(paths.absolute.buildDir, {
50+
digest: [paths.absolute.manifest]
2951
});
3052

3153
if (success) {
32-
console.log(`Restored the cached .next folder at the location \`${directory}\``);
54+
console.log(`Restored the cached ${paths.buildDirName} folder at the location \`${paths.absolute.buildDir}\``);
3355
} else {
34-
console.log(`Unable to restore the cached .next folder at the location \`${directory}\``);
56+
console.log(`Unable to restore the cached ${paths.buildDirName} folder at the location \`${paths.absolute.buildDir}\``);
3557
}
3658
},
3759
// Cache file/directory for future builds.
@@ -40,17 +62,17 @@ module.exports = {
4062
// - the file/directory is already cached and its contents has not changed
4163
// If this is a directory, this includes children's contents
4264
// Note that this will cache after the build, even if it fails, which fcould be unwanted behavior
43-
async onPostBuild({utils, netlifyConfig}: NetlifyOpts) {
44-
const directory = joinPaths(netlifyConfig.build.base, buildCachePath);
65+
async onPostBuild({utils, netlifyConfig, inputs}: NetlifyOpts) {
66+
const paths = generateAbsolutePaths({netlifyConfig, inputs});
4567

46-
const success = await utils.cache.save(directory, {
47-
digest: [joinPaths(netlifyConfig.build.base, manifestPath)]
68+
const success = await utils.cache.save(paths.absolute.buildDir, {
69+
digest: [paths.absolute.manifest]
4870
});
4971

5072
if (success) {
51-
console.log(`Cached the .next folder at the location \`${directory}\``);
73+
console.log(`Cached the ${paths.buildDirName} folder at the location \`${paths.absolute.buildDir}\``);
5274
} else {
53-
console.error(`An error occurred and the .next folder at the location \`${directory}\` could not be cached`);
75+
console.error(`An error occurred and the ${paths.buildDirName} folder at the location \`${paths.absolute.buildDir}\` could not be cached`);
5476
}
5577
}
5678
};

0 commit comments

Comments
 (0)