airbnb / hypernova

A service for server-side rendering your JavaScript views
MIT License
5.82k stars 207 forks source link

Guidance needed - how to store request context? #111

Open magicmark opened 6 years ago

magicmark commented 6 years ago

Hi @goatslacker!

So I have a logger set up inside an express middleware, logging various things about the request.

One bit of information I'd like to add to the logs is - did the request hit a cold cache or not?

I have access to this information inside of my application code - inside of getComponent. However there's no great way that I can currently think of to thread this information along to other parts of the express app.

I'm thinking one way to do this would be to expose the express request object to the context object passed into getComponent https://github.com/airbnb/hypernova/blob/477257daad82e04f24ce851a9b3d21625354c783/src/utils/BatchManager.js#L90

Or maybe there's just a better way to architect this?

Thanks!

magicmark commented 6 years ago

(Shoving arbitrary things on the request object is clearly not ideal. I still need to do some more thinking here, but that's probably an orthogonal problem)

ljharb commented 6 years ago

I usually stick things on the response object (and never mutate the request)

magicmark commented 6 years ago

Oh that sounds better, yea! @ljharb++

ok so same question, s/request/response/ ?

goatslacker commented 6 years ago

Why don't we pass a reference to res.locals?

magicmark commented 6 years ago

Ah yeah, even better I suppose.

I'll update my PR. Thanks for the quick responses!