Is there a way to include build id in cache key? #158
-
Hi, First of all, thank you for your work. Great stuff! I have a question regarding the cache key. Is there a way to include the build ID in the cache key as well? Next.js has a method for setting a stable build ID in next.config.js via the generateBuildId property. It would be helpful if this build ID could be part of the cache key. Thank you a lot for your answer in advance. 🙂 Samuel |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hey @samueldusek, thank you for your interest in my project. There are two ways to achieve your goals. You can use both with custom Handlers, but I will only show 1. Use env variablesAccording to the generateBuildId docs, you can use env variables to generate a In your module.exports = {
generateBuildId: async () => {
// This could be anything, using the latest git hash
return process.env.GIT_HASH;
},
}; Then, in your IncrementalCache.onCreation(
createHandler({
client,
keyPrefix: process.env.GIT_HASH,
})
); If this isn't feasible for you, I have a workaround. 2. The workaroundIn your IncrementalCache.onCreation((options) => {
const { serverDistDir } = options;
let buildId;
try {
buildId = fs.readFileSync(
path.join(serverDistDir, "..", "BUILD_ID"),
"utf-8"
);
} catch (error) {
return;
}
// You will see this in your logs. If it shows up, you're good to go and can remove the console.log
console.log("buildId", buildId);
const handler = createHandler({
client,
keyPrefix: `${buildId}:`,
});
return handler(options);
}); I hope this helps. Let me know if you have any questions. When I restructure this information, I will update the documentation to clarify for others. |
Beta Was this translation helpful? Give feedback.
-
In version You can find more information about it in the documentation. |
Beta Was this translation helpful? Give feedback.
Hey @samueldusek, thank you for your interest in my project. There are two ways to achieve your goals. You can use both with custom Handlers, but I will only show
createHandler
in this example for simplicity.1. Use env variables
According to the generateBuildId docs, you can use env variables to generate a
buildId
. It is the recommended way to do it.In your
next.config.js
:Then, in your
cache-handler.js
:If this isn't feasible for you, …