oedotme / generouted

Generated file-based routes for Vite
https://stackblitz.com/github.com/oedotme/generouted/tree/main/explorer
MIT License
1.02k stars 47 forks source link

Layout loader reruns on <Link> click from the same route instead of reusing the data #124

Closed henrikvilhelmberglund closed 11 months ago

henrikvilhelmberglund commented 11 months ago

Describe the Bug

Not sure if this is a bug or me not knowing what I'm doing but I have an issue where if I click a link that is the same URL as the route I'm on the loader in my layout (_app.jsx) reruns.

I want to basically fetch once in the global layout and reuse that data everywhere using useOutletContext but if I click the active page link in my nav the loader reruns as if I refreshed to page. This doesn't happen if I go to / and back to the route for example.

Anyway this is easier seen than explained so I made a small repro below. I noticed it doesn't really work in stackblitz though so you may have to clone it locally.

Generouted Version

1.15.8

Your Example Website or App or Reproduction

https://stackblitz.com/github/henrikvilhelmberglund/generouted-layout-fetch?file=src%2Fpages%2F_app.jsx

Steps to Reproduce the Bug or Issue

(clone locally to make the fetch work (???))

  1. Click Cat in the nav.
  2. You can see a cat.
  3. Click Home in the nav.
  4. Click Cat in the nav.
  5. You can see the same cat.
  6. Click Cat in the nav. Result: You see a new cat.

Expected Behavior

Expected: You see the same cat.

Screenshots or Videos

No response

Platform

Additional context

No response

henrikvilhelmberglund commented 11 months ago

It seems like this is related to shouldRevalidate so I took a look at https://github.com/oedotme/generouted/issues/103, however that only allows the function in normal routes and I need it in the _app.jsx layout route.

I'm not sure what I need to add in routes.tsx in order to be able to add a ShouldRevalidate function in there.

oedotme commented 11 months ago

Hey @henrikvilhelmberglund, yes exactly you should add a custom routes.tsx and accept ShouldRevalidate from the _app.tsx. Here's a working example based on the example you've provided: https://stackblitz.com/edit/github-cvgpop?file=src%2Froutes.tsx%3AL87

Hope that helps!

henrikvilhelmberglund commented 11 months ago

Thanks for the example, it works perfectly!