clay / amphora

Middleware for Express that composes components into renderable pages
https://claycms.gitbooks.io/amphora/
MIT License
31 stars 23 forks source link

Expose response object (res) on locals #716

Open elgreg opened 1 month ago

elgreg commented 1 month ago

Is your feature request related to a problem? Please describe.

We'd like to be able to add response headers to things as they're rendered. Specifically, we'd like to be able to add a Surrogate-Key header of a page's author, section, tags, etc when we're rendering the article object. Right now, we can't add to the response based on its content because we don't have access to the response object.

Describe the solution you'd like

Assign locals.res to res in amphora somewhere like this: https://github.com/clay/amphora/blob/master/lib/render.js#L119

locals.res = res

Now model renders and other functions that have access to locals can also do something like locals.res.set("Surrogate-Key",author:${data.author.slug})

Of course, when setting it, one would have to check if it already exists, etc, etc., but this ask is more about getting it on the locals object so it's passed around.

james-owen commented 1 month ago

This sounds good. I wonder if we should expose the response object in only one spot to keep it simple at first. We could pass res to render hooks https://github.com/clay/amphora-html/blob/master/lib/render.js#L148 that can be declared by calling renderer.addPlugins([{render: my-cool-plugin}]) https://github.com/clay/amphora-html/blob/master/lib/setup.js#L149 That plugin will get the full composed page json after it's run through all of the component model files.