Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Render context/data. #20

Open
eirikurn opened this issue Apr 27, 2017 · 0 comments
Open

RFC: Render context/data. #20

eirikurn opened this issue Apr 27, 2017 · 0 comments

Comments

@eirikurn
Copy link
Member

Currently, it's hard to explain what the Session object is for. It may be handling too many use cases.

I think session is a good name considering its lifecycle, i.e. it's a place to store stuff for the duration of a browser session (or server request), and it includes methods related to that (hooks, refresh).

But there is one use case in particular which doesn't quite fit. Passing data between middlewares during a single page render.

The most obvious example is using a middleware for the Layout component and another for the actual page. How can a particular page modify (pass a prop to) the layout component?

Another open question is where document properties live, e.g. page title, meta tags. Is that a property of the session, or a specific render.

My proposal is to pass a new page object as the second argument to each middleware's inner function.

// Not using page data.
const basic = session => next => <Element />

// Using page data.
const layout = session => async (next, page) => {
  const element = await next()
  const theme = page.theme || 'red'

  return <Layout theme={theme}>{element}</Layout>
}

// Adding page data
const page = session => (next, page) => {
  page.theme = 'blue'
  return <Page />
}

If we decide to put document properties there, it can be sent to render hooks as well:

const helmet = session => {
  session.on('server', (render, page) => {
    render()
    page.headProps.helmet = Helmet.rewind()
  }
}

I would like to return this object from renderServer so it can be used to render the Document, but I'm not sure how.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant