What if your app could remember data so it never asks twice?
Why Request memoization in NextJS? - Purpose & Use Cases
Imagine your Next.js app fetching the same data from a server every time a user visits a page, even if the data hasn't changed.
Manually fetching data repeatedly wastes time and bandwidth, making your app slower and causing unnecessary load on servers.
Request memoization remembers previous data fetches and reuses them, so your app avoids repeating the same requests.
const data = await fetch('/api/data').then(res => res.json()); // fetches every timeconst data = await memoizedFetch('/api/data'); // reuses cached responseYour app becomes faster and more efficient by avoiding repeated network calls for the same data.
When a user navigates back and forth between pages showing the same product info, request memoization loads data instantly without waiting.
Manual repeated requests slow down apps and waste resources.
Request memoization caches and reuses data automatically.
This leads to faster, smoother user experiences and less server load.
Practice
Solution
Step 1: Understand request memoization concept
Request memoization stores the results of previous fetches to avoid repeating the same network calls.Step 2: Identify the effect on performance
By remembering previous results, it reduces server load and speeds up response times.Final Answer:
To save repeated fetches by remembering previous results -> Option BQuick Check:
Request memoization = Save repeated fetches [OK]
- Thinking memoization increases server requests
- Confusing memoization with disabling cache
- Assuming memoization slows down fetches
Solution
Step 1: Recall Next.js cache usage
Next.js provides acachefunction that wraps async functions to memoize their results.Step 2: Check correct syntax
The correct syntax iscache(async () => await fetch(...)), which matches const cachedFetch = cache(async () => await fetch('/api/data')).Final Answer:
const cachedFetch = cache(async () => await fetch('/api/data')) -> Option CQuick Check:
cache wraps async fetch function = const cachedFetch = cache(async () => await fetch('/api/data')) [OK]
- Placing cache inside async incorrectly
- Using non-existent fetch.cache method
- Calling cache.fetch which does not exist
getData()?
import { cache } from 'react';
const getData = cache(async () => {
console.log('Fetching data');
return await fetch('/api/data').then(res => res.json());
});
await getData();
await getData();Solution
Step 1: Understand cache behavior on repeated calls
Thecachefunction memoizes the async function result, so the fetch and console.log run only once.Step 2: Analyze the second call effect
On the second call, the cached result is returned without running the function body again, so no console.log output.Final Answer:
Fetching data -> Option AQuick Check:
cache memoizes, so console.log runs once [OK]
- Expecting console.log to run twice
- Thinking cache disables function execution
- Confusing cache with normal fetch calls
import { cache } from 'react';
const getData = async cache(() => {
return await fetch('/api/data').then(res => res.json());
});Solution
Step 1: Check cache usage syntax
Thecachefunction must wrap the async function, not be called inside the async declaration.Step 2: Identify the syntax mistake
Writingasync cache(() => ...)is invalid syntax; it should becache(async () => ...).Final Answer:
SyntaxError: 'cache' must wrap the async function, not be called inside it -> Option DQuick Check:
cache wraps async function syntax = SyntaxError: 'cache' must wrap the async function, not be called inside it [OK]
- Placing async before cache call incorrectly
- Assuming fetch is undefined without context
- Thinking cache is not imported
Solution
Step 1: Understand memoization with parameters
To memoize requests per unique URL, the cache must consider the URL argument to store separate results.Step 2: Use cache with parameterized function
Wrapping a function that takes URL as input withcachememoizes each unique URL separately.Final Answer:
Usecachewith a function that takes the URL as argument and memoizes internally -> Option AQuick Check:
cache with parameters memoizes per input [OK]
- Using single cache for all URLs causing wrong data
- Ignoring memoization for dynamic inputs
- Assuming cache disables dynamic fetching
Ready to prove it?
Pass the test and this pattern moves unknown
