Next.js Discord

Discord Forum

App Router - How to navigate between URLs with SSR (for SEO) but without slow hard refresh of Map

Answered
Spotted Towhee posted this in #help-forum
Open in Discord
Avatar
Spotted TowheeOP
How do I get from current behaviour to desired behaviour? Relevant code in the images attached, happy to provide more context!

Desired behaviour: I am trying to build a google maps clone (ish). When I select a location on the map, the map should open a sidebar with information about that location, and smoothly pan to centre on the location. I also want the content associated with each location (which is stored in Postgres) to be SEO friendly. Ideally selecting the location would change to a URL specific to that location and pan the map at the same time.

Current Behaviour: I have a map (Leaflet.js) which is a client side component using useLayoutEffect. I can select locations on the map. When I select them (see code) I currently dynamically route to /topic/[topicName] , where my /topic/[topicName]/page.tsx file uses generateStaticParams and fetch() to store the information about each location in an SEO friendly way, and then I return the content in a sidebar, as well as loading the Leaflet map beneath it. The issue is that this reloads the map every time I select a new location, which is a terrible experience, but it was my best attempt so far.
Image
Image
Answered by Laustin Spayce
Put leaflet inside of a layout.tsx file so it is not reloaded on every url change.
View full answer

5 Replies

Avatar
Spotted TowheeOP
bump
Avatar
Put leaflet inside of a layout.tsx file so it is not reloaded on every url change.
Answer
Avatar
Spotted TowheeOP
Oh my god that makes so much sense. THANK YOU! Off to try it and will report back.
Avatar
Spotted TowheeOP
@Laustin Spayce It worked! You sent me down a whole rabbit hole of refactoring my code given how layout.tsx works & wraps, and my code is looking a lot healthier right now - and the app performing better.
Avatar
Mark the question as answered. Glad to have helped!