Skip to content

Custom document support - #405

Merged
rauchg merged 1 commit into
masterfrom
add/custom-document
Dec 16, 2016
Merged

rauchg merged 1 commit into
masterfrom
add/custom-document

Conversation

@nkzawa

@nkzawa nkzawa commented Dec 16, 2016 •

Copy link
Copy Markdown
Contributor

Closes #251
See https://github.com/zeit/next.js/wiki/Roadmap#custom-css-solutions-custom-document

This PR supports:

  • replace next/css with any CSS library
  • customize server rendered HTML template
  • hot-code reloading of pages/_document.js
  • base Document component and some useful components (next/document)

Usage

Create a file on pages/_document.js

import Document, { Head, Main, NextScript } from `next/document`

export default class MyDocument extends Document {
  static async getInitialProps (ctx) {
    const props = await Document.getInitialProps(ctx)
    // append value to props
    return { ...props, value: 'hi' }
  }

  render () {
    return (
     <html>
       <Head>
         {/* custom style */}
         <style>{`body { margin: 0 }`}</style>
       </Head>
        {/* set className to body */}
       <body className="hi">
         {this.props.value}
         <Main />
         <NextScript />
       </body>
     </html>
    )
  }
}

WIP

Add khan/aphrodite example
Add tests

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+3.2%) to 37.438% when pulling 7d0b180 on add/custom-document into 43b0e6f on master.

1 similar comment
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+3.2%) to 37.438% when pulling 7d0b180 on add/custom-document into 43b0e6f on master.

Comment thread server/build/index.js

await Promise.all([
runCompiler(compiler),
babel(dir)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We have to do a specific babel compilation instead of using webpack and babel-loader for pages/_document.js since the file is used only on server side.

Comment thread server/document.js
}

render () {
const { head, nextCSS } = this.context._documentProps

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Head, Main and NextScript receive props of Document via context so that you don't have to manually set props to these components. This should be convenient but can be too obscured ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No. I think context is the right thing to use here.

@rauchg

rauchg commented Dec 16, 2016

Copy link
Copy Markdown
Member

Amazing

@rdewolff

Copy link
Copy Markdown

Great!

Question, in my custom , the comments says the following :

// The document (which is SSR-only) needs to be customized to expose the locale
// data for the user's locale for React Intl to work in the browser.

Is that true, the is SSR only?

@lock lock Bot locked as resolved and limited conversation to collaborators Jan 18, 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.

4 participants