question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[feat] pass data from `locals` to `load`

See original GitHub issue

Describe the problem

My site queries an external API with the following pattern:

  • handle extracts user ID from cookie
  • +layout.svelte fetches user by user ID
  • +page.svelte fetches profile by user ID

These fetches previously happened in parallel. With $session removed I have no way to write this code without introducing a waterfall

Describe the proposed solution

I’m happy with any solution folks can think of that solves this problem

I think we could potentially solve this as part of https://github.com/sveltejs/kit/issues/7105

Alternatives considered

No response

Importance

i cannot use SvelteKit in a performant manner without it

Additional Information

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:11
  • Comments:17 (12 by maintainers)

github_iconTop GitHub Comments

6reactions
dummdidummcommented, Oct 11, 2022

Copying my Discord-thoughts into here:

What became clear to me that getSession was used for multiple things at once.

  1. pass data from server to client load functions
  2. make cross-cutting data easily available across all client load functions
  3. trigger a load function to rerun when data changes (e.g. $session.user = null)
  4. a convenient writable store that is somewhat global

Because it had so many use cases in once, it became somewhat confusing what runs when, because people may only think about 4 and then wonder about 3. So I think removing it in its form was correct, but we need some features that bring back the mentioned points in a easy-to-use way. Right now we have this situation:

  1. pass data -> can do this now by having a +layout.server.js and passing whatever you want to the client
  2. cross-cutting data -> use await parent() -> this introduces a waterfall because you can’t tell “hey I’m only interested in the data from parent X, don’t wait on the 3 other layout loads after that”. Something like locals for the client side would help with this.
  3. trigger rerun -> there’s nothing built-in for this right now. Rich shared a pattern somewhere about creating a custom state which triggers a unique invalidate function under the hood. Maybe it’s enough to make this a blessed pattern and/or tidy it up a little and provide a wrapper around it.
  4. convenient writable store -> again, nothing built-in, this would be #7105 3+4. It’s not clear to me how to achieve a combination of these two in a nice API. session made it possible because it was on both sides.

–> I feel like we need to add recipes and/or APIs to ease this

2reactions
dummdidummcommented, Oct 19, 2022

The idea of more fine-grained await parent() is a interesting idea. Another way could be to optionally pass parent a function to tell it what to wait for, or a list of top level keys.

export function load({ parent }) {
  const user = await parent(['user']); // resolves as soon as `user` resolves, only reruns when `user` changes
}

Edit: They key-version is probably not possible. How would the function know that the user key that is returned by some upper load function is actually the last with that key? If there are two load functions returning user, we need to wait for the later load function in the chain, but we don’t know that there’s something coming later on.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FEAT/UserGuide - FSL - FslWiki
Highpass temporal filtering uses a local fit of a straight line (Gaussian-weighted within the line to give a smooth response) to remove low ......
Read more >
Introduction to Keras, Part One: Data Loading | by Samhita Alla
To load images from a URL, use the get_file() method to fetch the data by passing the URL as an arguement. This stores...
Read more >
Local State - FlutterFlow Docs
1. Create a local state variable · 2. Set data into the local state variable · 3. Create/add a custom widget · 4....
Read more >
MySQL 8.0 Reference Manual :: 5.1.8 Server System Variables
To explicitly cause the server to refuse or permit LOAD DATA LOCAL ... However, the server does not send to the client more...
Read more >
Pass a variable from javascript to ejs - node.js - Stack Overflow
The app.locals.myVar approach should work, so something must be getting in the way. But you could avoid using app.locals.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found