Client Side Rendering using Layouts without Reloading the entire page?
Edit July 2021: The layouts example has been updated to persist state across page transitions: https://github.com/vercel/next.js/tree/canary/examples/layout-component
Would love this, especially with static export there's more cases of not re-rendering whole page, We have a mobile menu tabs in footer for example and change the active button on that menu, and re-rendering of whole page when page changes with next router blocks rendering of new active menu button, which just doesn't look really snappy
any progress or answer about it ?
@Timer @timneutkens any updates on this? I didn’t realize next js doesn’t do this already
Unfortunately, NextJS doesn't support persistent layouts yet but there is a nice article about how you can implement SPA user experience with NextJS.
https://adamwathan.me/2019/10/17/persistent-layout-patterns-in-nextjs/
There are several examples of how to layout, but only this one DOES NOT CAUSE re-rendering of whole component tree:
https://github.com/zeit/next.js/tree/canary/examples/with-dynamic-app-layout
Little preview of the code:
https://github.com/zeit/next.js/blob/c57cdbb31de6143b86b9091afb8d4dc2a2f46cc5/examples/with-dynamic-app-layout/pages/_app.js#L6-L17
https://github.com/zeit/next.js/blob/c57cdbb31de6143b86b9091afb8d4dc2a2f46cc5/examples/with-dynamic-app-layout/pages/red.js#L6-L23
I like the Remix and UmiJs strategy to solve this problem.
https://blog.remix.run/p/remix-preview
https://v2.umijs.org/guide/router.html#nested-routing
I have a demo for this issue here
As you click link to switch between pokemon, only a part of a page is re-rendered.
Wanna see such example with Next.js, too. (I'm using react-router) for the demo.
I've only used Next js router for basic page routing. How do I achieve client side component based (lazy-load?) routing? I can't imagine writing an app without it. How would you?
revskill10: link in https://github.com/zeit/next.js/issues/8193#issuecomment-623194233 is a 404.
The solution i came across is to use dynamic app layouts with React.memo method wrapped around <NavBar/> and </Footer> components which caused my NavBar and Footer render only one time during url changings
For example when i navigate through:
mysite.com/
mysite.com/me
mysite.com/questions
and so on, clicking <Link> component from 'next/link' ,
the only time my NavBar and Footer did render is first time - mysite.com/
So, doing this i achieved the same thing like if we use React Router's switch method

What about having a layout page(which doesnt render more than once) that connects to a socket server?
I tried this connection in useEffect hook for _app.js, but on routing to another page, the _app.js gets re-rendered causing to establish a new socket connection!
Does anyone have an effective way to tackle this issue?
Update: The issue was with routing to a dynamic route(/stats/[id].jsx), what I did to fix the issue was to replace the dynamic route with a static route with passing id as query param within the Link
Can't wait for the solution 🥰
https://github.com/moxystudio/next-layout
second UmiJS's solution, it makes sense as nextJS is already using conventional routes (based on the file/folder names):
+ pages/
- _layout.js
- index.js
+ users
- index.js
+ [id]
- _layout.js
- posts.js
- orders.js
- about.js
It also makes permission control fairly easy, you just need to put the permission check logic in a certain level layout file then all the children pages will inherit the check.
I might be wrong, but I thought react takes care of this.
Yes, surely react will "calculate" the whole _app, but in the end, it is only updating the parts of the dom that have changed, so from the user point of view, there only is re-rendered the page, not the header or the footer.
Am I right?
I might be wrong, but I thought react takes care of this.
Yes, surely react will "calculate" the whole _app, but in the end, it is only updating the parts of the dom that have changed, so from the user point of view, there only is re-rendered the page, not the header or the footer.
Am I right?
Not for Nextjs where each page is almost like a separate app
I might be wrong, but I thought react takes care of this.
You can try it yourself. Btw “each page is almost like a separate app” from a comment above sounds fair enough. Though, there’s already an answer of how to hack it so no rerendering will happen:
https://github.com/vercel/next.js/issues/8193#issuecomment-590654825
It works in modern versions of Next even though I heard that this should stop working at some newer versions of react (but still works fine with [email protected]!)
I confirm that with newer versions of React, this workaround doesn't work anymore: https://github.com/vercel/next.js/issues/8193#issuecomment-590654825
Might consider using other solutions like https://github.com/moxystudio/next-layout, I haven't tried it yet, but looks promising, despite of being HOC (high order-component).
UPD.
Whoops, I simply misused the example, sorry for bothering. Still works!
Any update on when and or if this will be added?
@Wolfleader101 theres nothing to be added actually. It just works a little different than create-react-app, and it IS POSSIBLE to use layouts without rerendering the whole tree (read my previous comment)
IMO this issue might be closed, though it might be good idea to add some section about how to properly deal with layouts in NextJS, to official tutorials and/or docs