-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Description
Link to the code that reproduces this issue
https://github.com/gtjamesa/next-revalidate-bug
To Reproduce
# 1. Start app
$ pnpm dev
# 2. (optional) Perform a first request to compile the route
$ curl -s http://127.0.0.1:3000
# 3. In another terminal, remove `fetch-cache` and watch files for date changes
$ rm -rf .next/cache/fetch-cache; watch -n1 stat .next/cache/fetch-cache/*
# 4. Send requests to server
$ while true; do curl -s http://127.0.0.1:3000 | grep -oP '<p>Server time: .*?</p>' --color=none; sleep 5; doneCurrent vs. Expected behavior
Current behaviour
When isrMemoryCacheSize: 0 is set in next.config.js, we disable the in-memory cache and rely only on the filesystem cache. The cache tags are incorrectly being read from disk, and this is resulting in the cache being rewritten every time.
This means that if you have a cache with 30s revalidate time, and request the data every 5 seconds, the file is rewritten 6 times and the revalidate will never happen, because the last modified timestamp will be ~5 seconds ago. The cache will only revalidated if you wait 30 seconds from the last read and then request the data again.
next.js/packages/next/src/server/lib/incremental-cache/file-system-cache.ts
Lines 178 to 185 in 9ab8828
| const storedTags = data.value?.data?.tags | |
| // update stored tags if a new one is being added | |
| // TODO: remove this when we can send the tags | |
| // via header on GET same as SET | |
| if (!tags?.every((tag) => storedTags?.includes(tag))) { | |
| await this.set(key, data.value, { tags }) | |
| } |
Please see the video for a full example of the incorrect behaviour. You can see in the top-right pane that the file access times are updating every 5-seconds.
Expected behaviour
Each call to read from the cache does not write to the file, updating its timestamp. I will submit a PR to fix this issue
Verify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
Platform: linux
Arch: x64
Version: #98-Ubuntu SMP Mon Oct 2 15:18:56 UTC 2023
Binaries:
Node: 20.9.0
npm: 10.1.0
Yarn: 1.22.19
pnpm: 8.10.2
Relevant Packages:
next: 14.0.3-canary.9
eslint-config-next: 14.0.2
react: 18.2.0
react-dom: 18.2.0
typescript: 5.2.2
Next.js Config:
output: N/AWhich area(s) are affected? (Select all that apply)
App Router, Data fetching (gS(S)P, getInitialProps)