-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Knowing if we are in a client side navigation or direct hit #4447
Comments
I wonder how common a use-case this is? Since workarounds are possible and they aren't even that hairy, we should consider if it warrants a new feature. As for the API, since I'd love to hear others' thoughts. |
The workaround is not very complicated and works well yes. But it's one step that all my library users need to do to enjoy it fully. (BTW, I wrapped the 3 lines in a component to put in For sure it's not a blocker 🙃, but I wanted to check before working on it if you would accept something in this direction or not. Adding it to the export async function load({ clientNavigation }) {
KitQL.query({ clientNavigation })
return {};
} Today, with the workaround, it's export async function load() {
KitQL.query()
return {};
} |
I'm actually not sure I follow your use-case after re-reading the proposal. If you only care about knowing whether or not it's the first time Some well placed |
I started to think... But why I didn't think about it earlier!!! Because, I can do: export async function load({ fetch }) {
await KitQL.query1({ fetch })
await KitQL.query2({ fetch })
return {};
} And in both query1 & query2 I would need to know in what mode I'm in. And I can't have import { afterNavigate } from '$app/navigation';
afterNavigate(() => console.log('Hello you')); Because it's telling me: |
I believe the explicit addition to Anyway, you can still have an equivalent guard for your scenario without user input with something like this: import { browser } from '$app/env';
export let clientStarted = false;
if (browser) {
addEventListener('sveltekit:start', () => {
clientStarted = true;
});
}
|
WoW, this works like a charm! It will improve usability of my lib 👍 |
I found a case where the trick is not working. To avoid this, I have to put But coming back to the origin, it's where |
I use this approach to handle switching from server-loaded Firestore data to client-side subscriptions, without re-fetching data twice when subsequently navigating between routes on the client (same deal - you want to know when the initial server -> client mode changeover has happened). It would be convenient to have a flag, similar to Alternatively, some client side root hook / function would be somewhere it could go. |
Great new video: Rich Harris - The Road to SvelteKit 1.0 (Svelte Society NYC) I didn't find another issue to link this one too. |
In our case, a Doing the "library runtime" si quite some work, but allows us to support other environments! (Today: Svelte, Sapper, SvelteKit) |
Both the navigation store and the before/afterNavigate hooks have a type property as of #6537 , so you can detect from that what kind of navigation you're in. Does this solve the issue? |
Not really as these stores & hooks are not available in This (👇) is throwing import { navigating } from '$app/stores';
import { get } from 'svelte/store';
import type { PageLoad } from './$types';
export const load: PageLoad = async (event) => {
get(navigating); // <-- error: Function called outside component initialization
return {};
};
|
I was using the event to be able to determine if the Is it possible to do this anymore (inside the load function)? It doesn't seem so. |
Shouldn't this be possible with #6100? import { browser } from '$app/environment';
import { navigating } from '$app/stores';
import { get } from 'svelte/store';
import type { PageLoad } from './$types';
export const load: PageLoad = async (event) => {
if (browser) {
get(navigating); // <-- should work
}
return {};
}; |
Per #6555 (comment), this is easily solved: // src/lib/hydrated.js
export let hydrated = false
export function update() {
hydrated = true;
} <!-- src/routes/+layout.svelte -->
<script>
import { onMount } from 'svelte';
import { update } from '$lib/hydrated.js';
onMount(update);
</script> |
(The reason this wasn't possible when discussing this problem earlier is that until recently there wasn't a guaranteed root layout) |
Yeah, I was trying to remember "why on earth didn't I just set a flag to true?!" |
Per #6555 (comment), I close this issue as we have:
|
Describe the problem
Let's try to describe the Feature Request with a small graph:
As you can see, I have 2 routes: Home and About.
On a direct hit, the load function is executed 2 times once
!browser
oncebrowser
✅On navigation from About to Home, the load function of Home is executed 1-time
browser
✅The problem: how can I know in a load function if I'm in a client side navigation or direct hit?
Describe the proposed solution
The best to my mind would be to have something similar to
browser
like:When it's a direct hit, the 2 load functions would have clientNavigation to
false
.When it's a client side navigation, the load function would have clientNavigation to
true
.Alternatives considered
Workaround I use today:
I have a file
clientNavigation.ts
And in
__layout.svelte
I haveAnd now I can use
clientNavigation
value to know if I'm in a client side navigation or direct hit.Importance
would make my life easier
Additional Information
This would allow nice scenarios for my library https://github.com/jycouet/kitql awaiting or not to fill stores depending on the situation.
I would be happy to contribute (very happy actually!), just let me know:
The text was updated successfully, but these errors were encountered: