Next - Js - Full Route Caching
Next - Js - Full Route Caching
How it Works?
When app is build for deployment, Next.js automatically caches the entire route
segment, including the layout, page, and metadata.
Revalidating Data: Revalidating the Data Cache, will in turn invalidate the Router
Cache by re-rendering components on the server and caching the new render
output.
Redeploying: Redeploying the App with a new build will clear all Routing caches. (
Only Data-Cache persists across deployment )
Explore our latest online courses and learn new skills at your own pace. Enroll and
become a certified expert to boost your career.
Force Dynamic is a technique used to opt out of Full Route Caching. It forces Next.js to
render the page at request time, instead of serving the cached static HTML.
https://www.tutorialspoint.com/nextjs/nextjs_full_route_caching.htm 1/5
Page 2 of 5
In the code snippet below, we have a dynamic page that displays a random data. We will
display the random data with timestamp on the page.
return (
<main>
<h1>Force Dynamic Example</h1>
<div>
<p>Timestamp: {data.timestamp}</p>
<p>Random Number: {data.randomNumber}</p>
</div>
</main>
)
}
Output
The image below shows the output of the code snippet above. When we refresh the
page, we are getting a different random number for each request. This is because the
page is dynamically rendered at request time, and not cached.
https://www.tutorialspoint.com/nextjs/nextjs_full_route_caching.htm 2/5
Page 3 of 5
Note: In development mode, you may not see the effect of force-dynamic,
this is because Next.js automatically disables most caching to make
development easier. To see caching effect, you have to build the app and
start the production server.
Dynamic APIs are server-side rendered APIs that are not cached at build time. The page
containing dynamic APIs will be forcefully fetched from server. In the section below, we
will setup a dynamic API that sets a cookie based on the theme selected by the user.
In the code snippet below, we have a dynamic API that sets a cookie based on the theme
selected by the user. when we access the page, with different search parameters, we'll
see them reflected in the output. This route will always be dynamically rendered because
it uses runtime request information, which means no static HTML generated at build
time.
https://www.tutorialspoint.com/nextjs/nextjs_full_route_caching.htm 3/5
Page 4 of 5
// app/api/set-cookie/route.ts
// app/dynamic-data/page.tsx
return (
<div "p-4">
<h1>Dynamic Data Example</h1>
<div">
{/* Display Search Parameters */}
<section>
<h2>Search Parameters:</h2>
<pre>
{JSON.stringify(searchParams, null, 2)}
</pre>
https://www.tutorialspoint.com/nextjs/nextjs_full_route_caching.htm 4/5
Page 5 of 5
</section>
Output
The image below shows the output of the code snippet above. The search parameters
are displayed in the first section, and the cookies are displayed in the second section. For
each request, a new page is rendered with the search parameters and cookies displayed.
https://www.tutorialspoint.com/nextjs/nextjs_full_route_caching.htm 5/5