sapper icon indicating copy to clipboard operation
sapper copied to clipboard

Add ability to set response attributes on a per-route basis

Open benmccann opened this issue 5 years ago • 4 comments
trafficstars

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:

benmccann avatar Aug 02 '20 19:08 benmccann

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.

Conduitry avatar Aug 02 '20 21:08 Conduitry

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.

ajbouh avatar Aug 17 '20 19:08 ajbouh

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?

ehrencrona avatar Sep 15 '20 08:09 ehrencrona

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

benmccann avatar Sep 15 '20 16:09 benmccann