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

Pass an HOC to renderPage() #2010

Merged
merged 3 commits into from
Aug 21, 2017
Merged

Pass an HOC to renderPage() #2010

merged 3 commits into from
Aug 21, 2017

Conversation

jcheroske
Copy link
Contributor

There has been some discussion at #1751 and #1776 about changing the renderPage() signature to better support SSR and allow wrapping of all pages in the application. My suggestion is to simply allow an HOC to be passed into renderPage().

@mxstbr
Copy link
Contributor

mxstbr commented May 27, 2017

This would be amazing to merge as it'd make SSR with styled-components and Next.js much nicer!

Copy link
Member

@timneutkens timneutkens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any harm in adding this 👌

@jcheroske
Copy link
Contributor Author

jcheroske commented May 27, 2017 via email

@mxstbr
Copy link
Contributor

mxstbr commented Jun 13, 2017

Can we land this maybe? 😊 @arunoda @rauchg

Folks have been asking us about the best way to do Next + styled-components, this would make it so much better!

@jcheroske
Copy link
Contributor Author

jcheroske commented Jun 13, 2017 via email

@timneutkens
Copy link
Member

@jcheroske no it's us 😉 You did a great job.

@jcheroske
Copy link
Contributor Author

jcheroske commented Jun 13, 2017

Whew! For once, it's not my fault.

Oh, and sorry about the .gitignore change. I realized too late that is bad form.

@timneutkens
Copy link
Member

@arunoda lets merge this if you agree 👌

Copy link

@iamstarkov iamstarkov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nice to have feature

@iamstarkov
Copy link

@arunoda @timneutkens can we make it to be merged and released? it makes cssinjs's ssr so much easier

@iamstarkov
Copy link

iamstarkov commented Jul 17, 2017

@jcheroske @mxstbr @kof @timneutkens just fyi https://twitter.com/arunoda/status/885645093436141568

On it. After 3.0

— Arunoda Susiripala (@arunoda) July 13, 2017

@timneutkens
Copy link
Member

👌 thanks @iamstarkov I saw the tweet 😄

.gitignore Outdated
@@ -13,3 +13,4 @@ npm-debug.log
coverage

.DS_Store
.idea
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jcheroske this is unrelated, sometimes PRs are not merged because of things like this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed it ❤️

@kof
Copy link

kof commented Jul 23, 2017

@jcheroske can you please remove .gitgnore change and add a test? Otherwise it seems to be fine.

@jcheroske
Copy link
Contributor Author

jcheroske commented Jul 23, 2017 via email

@iamstarkov
Copy link

next 3 is released, so i hope we can expect this to be merged soon =)

@jcheroske
Copy link
Contributor Author

jcheroske commented Aug 9, 2017 via email

@timneutkens
Copy link
Member

@arunoda this is ready to be merged, I guess tests should be added for it though 🤔

@mxstbr
Copy link
Contributor

mxstbr commented Aug 21, 2017

Can we merge this? 😊 It's been a while and it would make styled-components + Next soooo much better!

Copy link
Member

@timneutkens timneutkens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 👌

@timneutkens timneutkens merged commit 9c18c54 into vercel:master Aug 21, 2017
@timneutkens
Copy link
Member

timneutkens commented Aug 21, 2017

@mxstbr done 👍, will be included in the Next release.

@mxstbr
Copy link
Contributor

mxstbr commented Aug 22, 2017

AWESOME thank you! I'll send a PR to update the official example and we'll get our docs in sync.

@mxstbr
Copy link
Contributor

mxstbr commented Aug 22, 2017

Hmm, I'm trying to update the example locally but I'm getting "The default export is not a React Component in page: "/""

This is the _document.js:

export default class MyDocument extends Document {
  static getInitialProps ({ renderPage, req }) {
    const isServer = !!req;
    if (isServer) {
      const sheet = new ServerStyleSheet()
      // I don't know why I have to .bind here, but otherwise this is undefined in sheet.collectStyles
      const page = renderPage(sheet.collectStyles.bind(sheet))
      const styleTags = sheet.getStyleTags()
      return { ...page, styleTags }
    }
  }

  render () {
    return (
      <html>
       <Head>
         <title>Next with styled-components</title>
         {this.props.styleTags}
       </Head>
       <body>
         <Main />
         <NextScript />
       </body>
     </html>
    )
  }
}

Any ideas why the component could not be a function?

@mxstbr
Copy link
Contributor

mxstbr commented Aug 22, 2017

I tried only importing the ServerStyleSheet on the server but that didn't work either:

  static getInitialProps ({ renderPage, req }) {
    const isServer = !!req;
    if (isServer) {
      const ServerStyleSheet = eval("require('styled-components').ServerStyleSheet")
      const sheet = new ServerStyleSheet()
      const page = renderPage(sheet.collectStyles.bind(sheet))
      const styleTags = sheet.getStyleTags()
      return { ...page, styleTags }
    }
    return {};
  }

mxstbr added a commit to mxstbr/next.js that referenced this pull request Aug 22, 2017
Based on vercel#2010 this updates the styled-components to use the new
renderPage() signature.

Note that this doesn't work yet, it throws an error.
@mxstbr
Copy link
Contributor

mxstbr commented Aug 22, 2017

Moving discussion to: #2831

@iamstarkov
Copy link

yay 🎉

@fresh5447
Copy link

Awesome!

@arunoda
Copy link
Contributor

arunoda commented Aug 30, 2017

This is awesome.

@jcheroske I'm gonna do a release now.
Could you add a section about this on the README? I think it'll be useful.

@arunoda
Copy link
Contributor

arunoda commented Aug 30, 2017

Check next@3.1.0

@jcheroske
Copy link
Contributor Author

jcheroske commented Aug 30, 2017 via email

@NikitaVlaznev
Copy link
Contributor

NikitaVlaznev commented Jun 27, 2018

As i can see, renderPage enhancer runs only on the server side, so if it will wrap the page with any tag then there will be a react tree mismatch after browser rendering with unpredictable behavior and some warning from react like "expected server HTML to contain a matching ...", isn't it?

@timneutkens
Copy link
Member

This feature (wrapping the App) was introduced to support css-in-js libraries like styled-components. You shouldn't use it to wrap extra layout, that's what _app.js is for.

@NikitaVlaznev
Copy link
Contributor

NikitaVlaznev commented Jun 27, 2018

Tricky code. It is worth to mention that actually the App is wrapping this enhancer, and the enhancer is wrapping the Page, but only during server side rendering.

JSS example for material-ui still offers to wrap every page manually to handle the styles.
Here is a PR of how it can be done using the App: mui/material-ui#11989.

It is based on passing pageContext from the App down to the enhancer and then to the Document.
Still looks tricky but should work.

@jcheroske
Copy link
Contributor Author

Sorry, I never did get around to writing docs for this. I'm no longer using next.js, or doing client-side work atm. I hope the project is still healthy.

@kof
Copy link

kof commented Jul 5, 2018

I don't use next.js yet either, thats the problem, waiting for someone who uses both.

@lock lock bot locked as resolved and limited conversation to collaborators Jul 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants