-
-
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
A function for getting the current load
arguments
#2979
Comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Hey all, Just chiming in here as someone who has been trying to integrate To give a little detail on how we work in In Next we've had to do this a bit more linear to this here, we have a load-function where we manually call the functions needed for the page we are rendering and then call Just throwing these out here so we can have a wide variety of use-cases, thank you for starting this @Rich-Harris! 🙌 |
This comment has been minimized.
This comment has been minimized.
Going to hide the comments about removing @JoviDeCroock thanks for sharing those links. It definitely feels like this could help improve the ergonomics. It feels like the |
If the |
It's designed to work that way — during SSR it will hash the request body... kit/packages/kit/src/runtime/server/page/render.js Lines 179 to 188 in ae7f790
...then if a kit/packages/kit/src/runtime/client/renderer.js Lines 47 to 63 in ae7f790
|
This is not well thought-out, but is there any reasonable way to combine this feature with making the I have various helper functions in my app that I need to sometimes call within a With the changes proposed in this issue, I couldn't quite get rid of these extra hoops, because these functions still won't know whether to use the new |
I was just thinking about this again and ... how is this feature actually going to work on the server? Doesn't this basically require some sort of continuation-local storage system? If we could let anyone grab these arguments at any time, they wouldn't even need to be arguments - they could just be globals. Are we going to say that |
yes, the async-ness of a function has no bearing on the matter — this will always log 'before', 'fooing', 'after': async function foo() {
console.log('fooing');
}
function test() {
console.log('before');
foo();
console.log('after');
}
test(); Which means that figuring out which export function getLoadArguments() {
return current_load_arguments;
}
function navigate() {
current_load_arguments = { url, params, session, stuff, fetch };
const promise = load.call(null, current_load_arguments);
current_load_arguments = null;
} |
Gotcha, okay, so we'll just need to document that |
Would there be benefit in making the arguments available during the whole |
It doesn't work because there might be multiple requests being served concurrently by the server - the same reason that they are arguments to |
Hi there, I would really like this proposal to work but I think the fact that I would like to propose an other solution that introduces some kind of hook that makes it possible to customize the way function loadHook(node: BranchNode, load_input: LoadInput): LoadOutput {
const api = API.createWithLoadArguments(load_input);
return node.module.load(load_input, api);
} The async function load({page}, api) {
const group = await api.getGroup(page.params.groupid);
const members = await api.followLink(group, 'members');
return {
props: { group, members }
}
} The src/runtime/client/renderer.js#L676 would have to be changed as follows. - const loaded = await module.load.call(null, load_input);
+ const loaded = await (loadHook ? loadHook(node, load_input) : module.load.call(null, load_input)); This solution is of cause not limited to the api example but could be used for logging or transformation of the |
@Conduitry, load & stores are already magic. 1/ In SSR, Not that nice to pass the I'll be very interested in adding import { KQL_AllContinents } from '$lib/graphql/_kitql/graphqlStores';
export async function load() {
await KQL_AllContinents.queryLoad(); // Work in SSR & Client mode
// await KQL_another-operation-filling-another-store.queryLoad();
return {};
} And getting rid of the |
I've just posted this discussion which is along the same lines of the thinking here - how to reduce the cumbersome boilerplate in using SvelteKit's I did have one question in response to this from @Rich-Harris:
Can I confirm that, outside of a |
Someone else can feel free to correct me, but afaik the only thing sveltekit’s provided fetch does is hydrate the response fetched during |
#4625 is one thing the client-side |
maybe getLoadInput()? Is the work on this goal advanced? Because I don't know whether to build access to the external API with tricks or wait for the completion of this track? By the way, I wanted to ask about this: <script context="module">
export async function load({ stuff }) {
const data = stuff.api.get('endpoint');
// ...
}
</script> Rich in the initiating post gave two examples that he called "monstrosities", the first I understand, but the one above does not. Where in stuff does the api come from? |
We realised last night that this feature is unnecessary — you can do everything with a higher order function: export const load = extra_fancy_load(({ client }) => {
return client.get_stuff();
}); This is much less magical, and doesn't come with any caveats around timing. The one major caveat is that
...which would have the effect of triggering getters for |
Could you please elaborate a bit more on this technique (like how to define and type |
@gterras a simple implementation of const extra_fancy_load = (loadFunction) => {
return async function load(args) {
const client = API.createClientWithLoadArguments(args);
return await loadFunction({...args, client});
}
} The beauty of this (compared to the initial examples) is that the load arguments don't need to be passed around in the background and it has no side effects. And it is much more composeable. Example: const logging_load = (loadFunction) => {
return async function load(args) {
const start = Date.now();
const result = await loadFunction(args);
console.log(`Loaded ${args.url.href} in ${Date.now() - start}ms`);
return result;
}
} Simple usage: export const load = logging_load(async ({fetch}) => {
const res = await fetch('/some/data');
const json = await res.json();
return json;
}) Or compose it with export const load = logging_load(extra_fancy_load(({ client }) => {
return client.get_stuff();
})) |
Thanks for this, very interesting this should be mentioned in the docs. What would be the proper way to TS type |
Describe the problem
fetch
is great, but it's common to need a more powerful abstraction when loading data — a GraphQL client, or a customapi.get('endpoint')
helper, or even just something that adds some logging around the fetch.Right now that's somewhat cumbersome, because in order to make credentialled fetches during SSR, you need the instance of
fetch
that was passed intoload
. So you have to pick between these two monstrosities:It gets worse if you need to pass
page
orsession
around for logging purposes.The reason we have to do this is that the global
fetch
(supplied bynode-fetch
, in environments that don't natively support it) has no concept of credentials. A request for the/account
page receives cookies; those cookies need to be forwarded when the page'sload
makes a request for the/account.json
data that powers it, and the load-specificfetch
argument makes that possible.It would be nice if using an abstraction around
fetch
was less cumbersome.Describe the proposed solution
Just like
onMount
registers a callback with the component that is currently initialising, we could introduce a function that makes the arguments to the currentload
invocation available to a function that gets called during that invocation. For example:This function (hopefully we can come up with a better name than
getLoadArguments
) would only work during the invocation of aload
; if called at any other time, it would throw an error.Alternatives considered
No response
Importance
would make my life easier
Additional Information
No response
The text was updated successfully, but these errors were encountered: