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

Docs: Document caching mechanisms #52514

Merged
merged 164 commits into from Jul 31, 2023
Merged

Docs: Document caching mechanisms #52514

merged 164 commits into from Jul 31, 2023

Conversation

delbaoliveira
Copy link
Contributor

@delbaoliveira delbaoliveira commented Jul 10, 2023

This PR document the caching semantics in Next.js, how they interact, and what APIs affect them. We're also taking the opportunity to consolidate terminology, remove duplicate content, and update sections of the docs that relate to caching.

Documentation

  • Create a new section for caching
  • Explain how the different caching mechanisms work
    • Request Memoization (React Cache)
    • Persistent Data Cache
    • Persistent Full Route Cache
    • In-memory, client-side Router Cache
  • Document how different APIs affect caching
  • Document cache interactions
  • Clean up stale information in the other docs sections
    • Routing Section
      • Move advanced navigation topics from fundamentals to How Navigation Works section
      • Rewrite the How Navigation Works section
    • Rendering Section
    • Data fetching section
      • Consolidate data fetching story for fetching, caching, and revalidating
      • Clarify data fetching story with 3rd party libraries and React cache
      • Create Data Fetching Patterns page
  • Document other related behaviors:
    • Update information on scroll position for back/forward navigation
    • Remove the concepts of soft and hard navigation
    • Remove the concepts of static and dynamic data fetching
    • Use consistent terminology runtime 👉🏼 request time. Runtime for Edge and Node.js, request time to describe when dynamic stuff happens
    • generateStaticParams being able to seed the Full Route Cache
  • Polish 💅🏼

Related PRs:

@koichik
Copy link

koichik commented Jul 12, 2023

@delbaoliveira Thanks for your great work! Could you please take a look at #52069?

Comment on lines +176 to +186
### Opting out of Data Caching

`fetch` requests are **not** cached if:

- The `cache: 'no-store` is added to `fetch` requests.
- The `revalidate: 0` option is added to individual `fetch` requests.
- The `fetch` request is inside a Router Handler that uses the `POST` method.
- The `fetch` request comes after the usage of `headers` or `cookies`.
- The `const dynamic = 'force-dynamic'` route segment option is used.
- The `fetchCache` route segment option is configured to skip cache by default.
- The `fetch` request uses `Authorization` or `Cookie` headers and there's an uncached request above it in the component tree.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to verify if these cases are correct

@delbaoliveira
Copy link
Contributor Author

Thank you @manovotny, @TkDodo and @alvarlagerlof for the feedback! 🙏🏼

leerob
leerob previously approved these changes Jul 31, 2023
@kodiakhq kodiakhq bot merged commit 98c3076 into canary Jul 31, 2023
87 of 89 checks passed
@kodiakhq kodiakhq bot deleted the docs-7gjo branch July 31, 2023 17:03
@IGassmann
Copy link
Contributor

Looking forward to reading these! :)

@takefumi-yoshii
Copy link

It's also worth mentioning the Data Cache created by next dev.The Data Cache is a constant source of confusion during development.It's good to mention that .next/cache files needs to be removed explicitly.

@koichik
Copy link

koichik commented Aug 1, 2023

@delbaoliveira 🎉🎉🎉🎉🎉🎉Congrats Marge!🎉🎉🎉🎉🎉🎉
What an unbelievable work!

I was able to see the diagram now, but I think the React Memoization and the Data Cache positions are inverted.

https://nextjs.org/_next/image?url=%2Fdocs%2Flight%2Fcaching-overview.png&w=1920&q=75&dpl=dpl_6jEE2o8UhNfkbiXNT6wnDf4UwJwt
https://nextjs.org/_next/image?url=%2Fdocs%2Flight%2Frequest-memoization.png&w=1920&q=75&dpl=dpl_6jEE2o8UhNfkbiXNT6wnDf4UwJwt

In fact, the Next.js' fetch is applied first, then the React's fetch applied only if the Data Cache misses.
Should I file a new issue?

kodiakhq bot pushed a commit that referenced this pull request Aug 1, 2023
Follow up from #52514.

We're still missing the static and dynamic diagram, it was missed in the PR to `front` to add the original diagrams. We'll need to get that in as well, could be here, or in a follow up.
@delbaoliveira
Copy link
Contributor Author

Hey @koichik,

Thank you!

I hear your point about the implementation order. This is the limitation with a 2d graphic, I think it is slightly more important to show that Request Memoization is on the rendering server and the Data Cache is on a separate network. We have a similar challenge visualizing an initial visit request passing through the client-side Router Cache even though it wouldn't exist yet.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet