Replies: 1 comment
-
I'm also looking for a way to simply clear the cache when data gets updated in a CMS. In my case revalidating all affected pages takes too long. One hacky solution is to disable the memory cache and mess with the file cache. It's a really bad idea to modify the contents of the // next.config.js
module.exports = {
experimental: {
isrMemoryCacheSize: 0,
},
} // pages/api/clear-cache.ts
import * as fs from 'fs';
import type { NextApiRequest, NextApiResponse } from 'next';
const readDirR = async (dirPath: string): Promise<string[]> => {
const files = await fs.promises.readdir(dirPath, { withFileTypes: true });
const promises = files.map(async dirent => {
const path = dirPath + '/' + dirent.name;
return dirent.isDirectory() ? await readDirR(path) : path;
});
return (await Promise.all(promises)).flat();
};
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const cacheFolder = '.next/server/pages';
// ignore api routes, _app, _document, _error, 404.html, 500.html and anything containing '['
const regex = /^\/(?!api\/|_|404\.html|500\.html)(?:(?!\[).)*$/;
const files = (await readDirR(cacheFolder))
.map(file => file.substring(cacheFolder.length))
.filter(file => regex.test(file))
.map(file => cacheFolder + file);
for (const file of files) {
await fs.promises.unlink(file);
}
res.status(200).json({ done: true });
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Describe the feature you'd like to request
Use case:
E-commerce website enables a sale, affecting 1k products, with all possible url permutations it triggers invalidation of 100k+ paths which results in API throttling of their backend systems
Describe the solution you'd like
Provide invalidation that would not trigger rendering until path is being visited (like fallback blocking does)
It would also be useful for potentially adding * matching for invalidation paths, to avoid requests spikes.
Describe alternatives you've considered
Use queues or trigger build to clear cache on all pages
Beta Was this translation helpful? Give feedback.
All reactions