next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Client Side Rendering using Layouts without Reloading the entire page?

Open jfarris587 opened this issue 7 years ago • 45 comments

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

jfarris587 avatar Jul 31 '19 20:07 jfarris587

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

Yuripetusko avatar Aug 22 '19 09:08 Yuripetusko

any progress or answer about it ?

tky5622 avatar Oct 15 '19 17:10 tky5622

@Timer @timneutkens any updates on this? I didn’t realize next js doesn’t do this already

lifeiscontent avatar Feb 22 '20 04:02 lifeiscontent

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/

borabuilds avatar Feb 25 '20 01:02 borabuilds

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

jerrygreen avatar Feb 25 '20 02:02 jerrygreen

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

rayronvictor avatar May 01 '20 01:05 rayronvictor

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.

revskill10 avatar May 03 '20 22:05 revskill10

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?

laurencefass avatar May 25 '20 08:05 laurencefass

revskill10: link in https://github.com/zeit/next.js/issues/8193#issuecomment-623194233 is a 404.

laurencefass avatar May 27 '20 12:05 laurencefass

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

Alt Text

postnd avatar Jul 21 '20 17:07 postnd

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

abhijithvijayan avatar Jul 22 '20 15:07 abhijithvijayan

Can't wait for the solution 🥰

Exelord avatar Aug 11 '20 22:08 Exelord

https://github.com/moxystudio/next-layout

ghost avatar Aug 21 '20 20:08 ghost

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.

neekey avatar Sep 08 '20 13:09 neekey

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?

hacknlove avatar Sep 11 '20 06:09 hacknlove

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

Yuripetusko avatar Sep 11 '20 07:09 Yuripetusko

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]!)

jerrygreen avatar Sep 13 '20 20:09 jerrygreen

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!

jerrygreen avatar Nov 27 '20 13:11 jerrygreen

Any update on when and or if this will be added?

Wolfleader101 avatar Mar 23 '21 23:03 Wolfleader101

@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

jerrygreen avatar Mar 24 '21 00:03 jerrygreen