Next.js Discord

Discord Forum

I need help in understanding how data caching works with url params

Unanswered
Pacific sand lance posted this in #help-forum
Open in Discord
Pacific sand lanceOP
Good afternoon I need help in understanding how data caching works.
The documentation says that fetch() always caches requests. But it's hard for me to understand how it will behave on pages with url parameters.
For example, there are pages:
https://domain.com/projects
и
https://domain.com/projects?type=seo

Will next cache data for each address separately or will they have a single data cache (which is not acceptable to me because these pages have different data)?

1 Reply

Pacific sand lanceOP
I used a local gpt service and he gave me the following answer. Can anyone tell me if his answer is correct.

Answer:
Good afternoon! In Next.js, the fetch() function indeed caches requests by default. However, it's important to note that the caching mechanism treats each unique URL as a separate entity. This means that the pages https://domain.com/projects and https://domain.com/projects?type=seo will have their data cached separately, as they are considered different due to the unique URL parameters.

This behavior is part of Next.js's automatic fetch request deduping feature. If you need to fetch the same data in multiple components in a tree, Next.js will automatically cache fetch requests (GET) that have the same input in a temporary cache. This optimization prevents the same data from being fetched more than once during a rendering pass. However, if the input (in this case, the URL) is different, the data will be fetched and cached separately.