Skip to content

Commit

Permalink
[code-infra] Fix nextjs build cache (#43467)
Browse files Browse the repository at this point in the history
  • Loading branch information
JCQuintas committed Aug 27, 2024
1 parent 2d581b6 commit 0d1578f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
49 changes: 27 additions & 22 deletions packages/netlify-plugin-cache-docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,46 @@ function generateAbsolutePaths(context) {
const workspaceRoot = path.dirname(constants.CONFIG_PATH);
const docsWorkspacePath = path.join(workspaceRoot, 'docs');

const nextjsBuildDir = path.join(docsWorkspacePath, '.next');
const digests = [path.join(workspaceRoot, 'pnpm-lock.yaml')];
const nextjsCacheDir = path.join(docsWorkspacePath, '.next', 'cache');

return { digests, nextjsBuildDir };
return { nextjsCacheDir };
}

module.exports = {
// Restore the `.next/cache` folder
// based on: https://github.com/netlify/next-runtime/blob/733a0219e5413aa1eea790af48c745322dbce917/src/index.ts
async onPreBuild(context) {
const { constants, utils } = context;
const { nextjsBuildDir } = generateAbsolutePaths({ constants });
const success = await utils.cache.restore(nextjsBuildDir);
const { nextjsCacheDir } = generateAbsolutePaths({ constants });

console.log("'%s' exists: %s", nextjsBuildDir, String(fse.existsSync(nextjsBuildDir)));
const cacheDirExists = fse.existsSync(nextjsCacheDir);
console.log("'%s' exists: %s", nextjsCacheDir, String(cacheDirExists));

console.log(
"Restored the cached 'docs/.next' folder at the location '%s': %s",
nextjsBuildDir,
String(success),
);
const success = await utils.cache.restore(nextjsCacheDir);

console.log("Restored the cached '%s' folder: %s", nextjsCacheDir, String(success));

const restoredCacheDir = fse.existsSync(nextjsCacheDir);
console.log("'%s' exists: %s", nextjsCacheDir, String(restoredCacheDir));
},
async onPostBuild(context) {
// On build, cache the `.next/cache` folder
// based on: https://github.com/netlify/next-runtime/blob/733a0219e5413aa1eea790af48c745322dbce917/src/index.ts
// This hook is called immediately after the build command is executed.
async onBuild(context) {
const { constants, utils } = context;
const { digests, nextjsBuildDir } = generateAbsolutePaths({ constants });
const { nextjsCacheDir } = generateAbsolutePaths({ constants });

const cacheExists = fse.existsSync(nextjsCacheDir);

console.log("'%s' exists: %s", nextjsBuildDir, String(fse.existsSync(nextjsBuildDir)));
if (cacheExists) {
console.log("'%s' exists: %s", nextjsCacheDir, String(cacheExists));

const success = await utils.cache.save(nextjsBuildDir, {
digests,
});
const success = await utils.cache.save(nextjsCacheDir);

console.log(
"Cached 'docs/.next' folder at the location '%s': %s",
nextjsBuildDir,
String(success),
);
console.log("Cached '%s' folder: %s", nextjsCacheDir, String(success));
} else {
console.log("'%s' does not exist", nextjsCacheDir);
}
},
// debug
// based on: https://github.com/netlify-labs/netlify-plugin-debug-cache/blob/v1.0.3/index.js
Expand Down
2 changes: 1 addition & 1 deletion packages/netlify-plugin-cache-docs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "netlify-plugin-cache-docs",
"version": "5.0.0",
"version": "6.0.0",
"private": true,
"author": "MUI Team",
"description": "Alternative to netlify-plugin-cache-nextjs",
Expand Down

0 comments on commit 0d1578f

Please sign in to comment.