Next.js Discord

Discord Forum

Is there a way to register to an event for when a file's cache is revalidated?

Unanswered
American black bear posted this in #help-forum
Open in Discord
American black bearOP
In my build process, in order to maintain the best performance, I make everything static. Because of that, I can't use a nonce and have to use hashes for CSP. This requires me to parse the generated html, calculate the hashes, and write them to the file.

The problem however is with revalidation of the cache once it's up on the server. Next will essentially erase all the hashes on the script tags as it renews the cache. I would like to always run my parser after a cache update or new cached item. I can probably accomplish this with S3 triggers on that directory but it wouldn't be as straightforward as if I can simply tie into the Next lifecycle and run it then.

2 Replies

American black bearOP
What would be really cool, since this is a pretty generic feature and necessary for CSP with SSG, is if we could opt-in to having integrity hashes generated by setting a config in next config. Then it could run at build time and at cache regeneration every time.
I might try this approach with patch-package if there's no simpler solution and see if it can be turned into a PR afterwards.