-
-
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
Ability to override load or use before/after hooks #327
Comments
Can these cases be accommodated with loader context? i.e. <!-- routes/$layout.svelte -->
<script context="module">
export async function load({ fetch }) {
return {
context: {
special_fetch = make_special(fetch);
}
};
}
</script> <!-- everywhere else -->
<script context="module">
export async function load({ context }) {
const stuff = await context.special_fetch('/stuff.json');
// ...
}
</script> |
That's basically what I had originally proposed in sveltejs/sapper#1336, but @ajbouh said that something he wanted to do was collect information about In his fork he created a way to provide a function that would call |
Is the desire to override the built-in |
No, wrapping would be sufficient (there's an example of that in sveltejs/sapper#1336 (comment)) |
I think there's a difference between "add something else to the context and use that" and "wrap the core function". The latter allows for modifications in one place without affecting other places. Imagine you want to alter fetch behavior three months in, you certainly don't want to change all occurrences of fetch-usage then. |
Alternatively: Would it be confusing if you could alter core functionality like this, without there being any indication that anything special is happening in any of the page components? |
That depends on the user I would say. Having the possibility to do so of course means people could do crazy stuff, and if the functionality exists there should be a big disclaimer like "if you don't want to change the behavior for your whole app, use context instead". But not having the possibility would hurt others. I see it as a kind of a plugin or middleware with the same pros and cons. Pro: you can hide stuff and make everyone use it. Con: That stuff may be too hidden. |
I don't know if we need multiple ways of doing things. That's more for us to create/maintain and potential confusion for users as to how to accomplish something and where to look to see if any extra behavior is happening. I think if we provided the ability to wrap then we wouldn't likely need to provide the ability to edit context in a page as well |
doesn't that exist already? <script context="module">
import { patch } from 'my-svelte-kit-telemetry-lib';
export const load = patch(({ fetch }) => {
// tada
});
</script> |
The idea is to provide a way to configure it in a single place so that you don't have to update every single template and worry about whether you might miss one |
That's what I understood Rich's first example to be for. Assuming you point to the loader context on each page, you can later change that in just one place and it will affect every component. I think this seems like a reasonable way to handle this. Loader context is a more powerful tool than just being able to override specific preexisting loader functions. I agree that we don't want two ways to do this - and if we are just picking one, then it should be the loader context - which also already exists. |
Loader context doesn't seem like it solves the timing / metrics gathering use case. If you want to instrument how long your If we decide to go the loader context route - I didn't like that in Rich's example that I'd have to change every call to |
My general feeling towards this sort of thing is that if it's possible to satisfy the identified use cases without introducing new API (which as far as I can tell it is, with a combination of context and API surface area is expensive; the bar is high. It might well be the case that we do need to add a means of wrapping |
Going to close this as there isn't a clear course of action to take — we can revisit it if the existing APIs prove insufficient in practice |
Reopening this as @ajbouh is reporting a gap in the APIs: You can't wrap So the minimum needed to "get around this" is to have in the same place:
|
The request headers are available in setup. Is that insufficient? |
@ajbouh I'll leave that question to you (just FYI, if we don't hear anything after awhile we'll probably go ahead and close this) |
I don't fully understand the data flow between setup and load, but maybe? Are there values that the server-side invocation of We can't use |
Fair warning: some of the nomenclature will probably change, because we have too many things called 'context'. You can derive a request context in // src/hooks.js
export function getContext({ headers }) {
return {...};
}
export function getSession({ context }} {
return {...};
} // src/routes/$layout.svelte
export async function load({ fetch, session }) {
const context = {
fetch: wrap(fetch, session)
};
return { context };
} |
I see. This flow seems to make a few assumptions about non-global state used by the
In my experience there are a variety of scenarios where we want to use non-serializable state that differs between server-side and client-side code. Simple counter examples:
|
Still not really sure I understand the problem. The |
My server-side code primarily runs within Lambda@Edge, where environment variables are not available. Instead on boot up my server-side pulls its configuration from AWS SSM Parameter Store. (I believe Cloudflare workers are similarly expected to store their configuration in the Cloudflare Worker KV store.) Unlike environment variables, values pulled from these places are subject to expiration and may need to be periodically updated. In my sapper fork, I initialize a I need this
Not only is the configuration of this It is very straightforward to handle both the lack of environment variables and the differing imports when the server-side "context" and client-side "context" have different initialization code paths. |
this is where the overuse of "context" gets confusing — do you mean component context (as in I'm guessing the latter since component context isn't available in // src/routes/$layout.svelte
import { browser } from '$app/env';
export async function load() {
const context = browser
? await import('$lib/client/context.js')
: await import('$lib/server/context.js');
return { context };
} |
I'm going to close this since it's a long thread and no response has been received. If there's a further need it might be best to file a new issue discussing the specific problem that needs to be solved |
One of the most common requests in Sapper is a way to alter
fetch
or otherwise affect theload
callsIs your feature request related to a problem? Please describe.
fetch
in setup - e.g. to setup Apollo client (Application initialization - making fetch available outside preload sapper#1243 Feature request: Preload argument for context sapper#751)fetch
(Export and cross-fetch don't play nice together sapper#908)Describe the solution you'd like
I originally sent sveltejs/sapper#1336 though it might not solve all requests. E.g. there was a request to time how long
load
takes and I don't think my solution would have made that easy to do, so it should probably be updated a bit to account for that.How important is this feature to you?
This enables a large number of use cases for advanced users, so is fairly important.
The text was updated successfully, but these errors were encountered: