marko-js-archive / marko-starter

MIT License
30 stars 8 forks source link

What's the right way to set custom HTTP headers for my routes? #18

Open callumlocke opened 7 years ago

callumlocke commented 7 years ago

I want to set a Cache-Control header, but it's unclear from the docs how to do this.

austinkelleher commented 7 years ago

@callumlocke You can set a header on your out in a route.js. For example:

const template = require('./index.marko');

exports.handler = (input, out) => {
  out.setHeader('Cache-Control', 'no-cache')
  template.render(input, out);
}
philidem commented 7 years ago

NOTE: out.setHeader(...) works when out is an HTTP response stream. out.setHeader(...) will throw error when building statically because a file output stream does not have a setHeader(...) method.

One of the goals of marko-starter was to normalize building statically (writing to file system) and building for the web (writing to HTTP response). We might need to make it a little more clear when handler is being invoked for static build versus HTTP response.

philidem commented 7 years ago

When building statically we might want to create a sidecar file with the headers. For example, index.html.headers could be produced. This would allow the headers to not be lost when writing to filesystem. Anyways, just a proposal right now so maybe we could come up with something better.