Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[code-infra] Fix nextjs build cache #43467

Merged
merged 3 commits into from
Aug 27, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading