kit icon indicating copy to clipboard operation
kit copied to clipboard

data-sveltekit-preload-code runs the universal hook reroute function, missing docs

Open ayyash opened this issue 3 weeks ago • 1 comments

Describe the problem

I was testing with reroute function, but I noticed that it ran multiple time when data-sveltekit-preload-code="eager" was added to the app.html. (there were multiple navigation links on the page). Is that by design?

in app.html: <body data-sveltekit-preload-code="eager">

In hook.ts

export const reroute: Reroute = ({ url }) => {
  //  save in authstate then return
  if (url.pathname.indexOf(Config.Auth.loginRoute) < 0) {
    console.log(url.pathname, 'catch reroute here');
    AuthState.redirectUrl = url.pathname;
  }
 // do something and return
  return reroutePath(url.pathname);
};

Describe the proposed solution

If it is by design, it should be documented because I was under the assumption it just preloads code without running anything specific

Alternatives considered

No response

Importance

would make my life easier

Additional Information

No response

ayyash avatar Dec 05 '25 16:12 ayyash

How could we preload code for a route without knowing which route to preload the code for?

yes but for someone who uses the black box, it really saves the day just mentioning it

ayyash avatar Dec 11 '25 18:12 ayyash

I'm having a hard time seeing when it would be an unpleasant surprise for someone that their reroute hook is being run, even if for some reason they weren't expecting it to be in this case. The docs do mention that it is assumed to be a pure, idempotent function. Saying anything further here in the docs seems like a distraction.

Conduitry avatar Dec 11 '25 18:12 Conduitry

I had a hard time, took me an hour to realize the reason my site was not acting properly is that i was placing a statement in reroute hook, when I finally figured it out, I moved out the statement

ayyash avatar Dec 13 '25 05:12 ayyash