sapper
sapper copied to clipboard
Add ability to set response attributes on a per-route basis
There's a desire to set certain response attributes on a per-route basis. E.g. the Cache-Control header, other headers, response type, etc.
A few ideas:
- Handle it with middleware
- Export some function or setting in a page or layout or call some function there
- Do it in a config file
- Expose the router and allow configuring it
My knee-jerk reaction here would be something along the lines of the second option above, where we have a new module-level export in the component, which is passed the req/res objects when doing a server rendering of that route.
Being able to affect the request and response before/after render would be a powerful tool.
Might be good to provide a single "around" hook to this effect, or maybe just two different hooks: "before" and "after". Though I think "around" would be better.
How about allowing this in preload? So either exposing a this.setHeader, as in
export function preload(page, session) {
this.setHeader('Cache-Control', 'max-age=3600');
}
or potentially just exposing the entire response, though I think that might be exposing a bit much.
What is the use case for doing something to request/response after the request?
preload might not be the right place to do it because it runs on the server and client
One example @ajbouh gave elsewhere was timing how long preload took. He wanted to instrument that call and so having a before/after hook would be useful for that. I imagine he might be wanting to do the same thing here of instrumenting how long it takes the entire request to be handled