-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Allow to pass custom headers on load responses #909
Comments
This comment has been minimized.
This comment has been minimized.
What would happen to the You can also use https://kit.svelte.dev/docs#hooks-handle to add or modify response headers for any request, whether it be for a page or an endpoint. edit: Oh, I see you mention doing this in the issue, sorry. I still don't know what I would expect to happen with these header values when |
Don't we already have that though with |
Yeah, technically the same thing happens when passing regular |
We talked about this in today's maintainer's meeting. Folks thought it made sense to support it on individual pages. Won't add it to layouts yet since you can use handle, other options are configured only at leaf level, and there's not a clear use case for adding it to layouts at the moment. |
I'd like to work on this issue.
|
To clarify, |
Thanks for the discussion and (@nhe23) the PR. Clearly this is an issue worth solving more elegantly (though it seems to me that the real solution is for hashed assets to be treated as immutable; this is something that adapters should probably do automatically). Having said that, I have a nagging feeling that allowing arbitrary headers to be set in Besides, looking through the list of response headers on MDN, it's not clear to me that apart from All of which leads me to the conclusion that we probably just need to refine our caching logic. Perhaps if we had something like this instead? export async function load({ params }) {
const props = await get_props(params);
return {
- maxage: 3600,
+ cache: {
+ public: true, // if unset, inferred using logic described in https://kit.svelte.dev/docs#loading-output-maxage
+ maxage: null,
+ smaxage: 3600
+ },
props
};
} This needs some thinking through/bikeshedding (are there other things we could usefully put there?) but does this feel like a promising direction? |
It admittedly felt kind of wrong to add a new return value to the load function that is only considered server-side. |
Yep, looks good to me and I agree. I'm not able to think of a scenario that would need extra headers. We can already handle redirection and caching (this new approach gives even more control, which is needed). Probably the |
After some thought I think it would be a good idea if the cache-control header could be optionally set directly in the load function. As there are many possible caching directives it would make sense to set them all at once and not through a bunch of properties in the returned cache object. If no value is set for export async function load({ params }) {
const props = await get_props(params);
return {
- maxage: 3600,
+ cache: {
+ maxage: null,
+ header: "public, s-maxage=3600"
+ },
props
};
} The |
Going to close this in favour of #4549 |
Is your feature request related to a problem? Please describe.
This can be related to my previous issue, which is really just some details missing in the docs #793
Despite
svelte-kit
having a way to definemaxage
to control caching for HTML pages, we really don't have a way of setting any other headers. The more common use cases are there: redirect and basic caching. But I feel like we need more control. On Next.js you can use thecontext
object and usesetHeader
. Here onsvelte-kit
you can't even use thecontext
because that's for layout only, so I need to resort to "hacky" solutions to get what I need.My basic use case is to use
s-maxage
instead ofmaxage
. I don't want to cache HTML pages on the client because JS and CSS hashes change between deploys, and if a user with an HTML page from a previous deploy refreshes the page (without clearing cache), the user gets an unstyled and unscripted page. Browsers make a request even if the assets are cached, but the 404 produced by the CDN (in this case, Vercel), results in those unstyled and unscripted pages. The only way around this is to probably use service workers and cache assets with it, but I want to avoid that.I feel like a
headers
prop is just something that sooner or later will become handy, the same way it is on endpoints.Describe the solution you'd like
Just allow us to specify the
headers
on aload
response, just like on endpoints:Describe alternatives you've considered
I've read about hooks and managed to work around this by doing:
Not that pretty honestly, but works for me. This replaces all
maxage
usages withs-maxage
which is something I'm 100% ok with, but just for this project. But this just fixes the issue for this project and this scenario, if someone wants to pass another random header, no dice.I also tried using the
context
object to pass in headers during theload
function and then apply them onhandle
, but you can't modify thecontext
from pageload
functions. So that was a no-go from the start.How important is this feature to you?
Very. Working with these recent CDN providers like Vercel, Netlify, and even Cloudflare is amazing, and being able to use
s-maxage
to avoid running cloud functions over and over again is just what I need, especially as I do a ton of work with local non-profits that really want to keep costs to a minimum.The text was updated successfully, but these errors were encountered: