-
Notifications
You must be signed in to change notification settings - Fork 27.1k
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
Using rewrites
in next.config.js
causes double renders
#33028
Comments
rewrites
in next.config.js
causes a double renderrewrites
in next.config.js
causes every page to render twice
rewrites
in next.config.js
causes every page to render twicerewrites
in next.config.js
causes double renders
Thank you for raising this issue. This is actually expected when you have a
So what is happening in your code is that the page gets statically optimized at build time without query params. Rewrites can have params that we need to provide in the query e.g. We will try to make this more clear in the docs. |
Mind providing more implementation details around this please? Specifically, I'm confused by query params that are static and not dynamic.
Since
What about pages that are ISR? Since it's runtime, what are the limitations to access the query params for ISR pages? |
I understand the reason for this to happen, but is there a userland solution? @ijjk said that
..but in my case, the double render problems have nothing to do with route-specific code or a global store/entrypoint. It's just that a lot of my code relies on hydrated data, so in reality I shouldn't need to check for This is a reocurring issue and I would highly appreciate if this is explained in the docs in more detail:
The explanation right now does not make it clear that a double render will be the result of using Context: In my case, I render huge charts, so a double initial render is causing massive problems. Specifically, I use hydration with React Query and |
@bennettdams not directly related to this issue, but have you considered memoizing your components? |
@valerie-makes My answer is also off-topic now: Sure, I already do. I have a lot of performance optimizations in place, e.g. also That's what I meant with...
|
Update: Not sure why this not occured to me, but besides the proposed solution of using the router's export const getStaticProps = async () => {
return {
props: {
date: JSON.parse(JSON.stringify(new Date())),
},
revalidate: 10,
};
};
// Option 1: Route with date as props
// `date` will change two times
export default function RouteWithProps(props) {
return (
<ShowDate date={new Date(props.date)} />
);
}
// Option 2: Route with date as props, but memoized
// `date` will change one time
export default function RouteWithPropsMemoized(props) {
// this memoization is the important part
const date = useMemo(() => new Date(props.date), [props.date]);
return (
<ShowDate date={date} />
);
} Here's a reproduction which has two roues, one with and one without memoizing at the root render function: https://stackblitz.com/edit/nextjs-ubqwdg?file=pages%2Findex.js You have to go to a route directly and not programmatically, otherwise the double render won't trigger this issue. If you go to At the other route ( Same goes for hydration e.g. with React Query - you just have to memoize the dehydrated state in your route's root render function.
This is no solution to the double-render problem, but a good way to prevent the referential equality problems that come with it - you just have to be aware of it. |
Submitted PR to update these details. :) |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Run
next info
(available from version 12.0.8 and up)What version of Next.js are you using?
12.0.7 and 12.0.8-canary.17 both reproduce
What version of Node.js are you using?
16.13.1
What browser are you using?
Chrome and Safari both reproduce
What operating system are you using?
macOS 12.1 (21C52)
How are you deploying your application?
Vercel,
next dev
andnext start
all reproduceDescribe the Bug
When a
rewrites
function is specified innext.config.js
, every page renders twice in the browser, regardless of the nature of this function.Expected Behavior
Specifying a
rewrites
function does not cause double renders.To Reproduce
https://github.com/valerie-makes/nextjs-rewrites-bug
npm install
npm run build && npm start
rewrites
function innext.config.js
The text was updated successfully, but these errors were encountered: