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

Fix first render of with-react-helmet example #6235

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/with-react-helmet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ now

This an minimalistic example of how to combine next.js and [react-helmet](https://github.com/nfl/react-helmet).
The title of the page shall be changed to "Hello next.js!"
If you go to the page `/about`, the title will be overridden to "About | Hello next.js!"
The rest is all up to you.
33 changes: 33 additions & 0 deletions examples/with-react-helmet/pages/_app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react'
import App, { Container } from 'next/app'
import Helmet from 'react-helmet'

export default class MyApp extends App {
static async getInitialProps ({ Component, ctx }) {
let pageProps = {}

if (Component.getInitialProps) {
pageProps = await Component.getInitialProps(ctx)
}

return { pageProps }
}

render () {
const { Component, pageProps } = this.props

return (
<Container>
<Helmet
htmlAttributes={{ lang: 'en' }}
title='Hello next.js!'
meta={[
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
{ property: 'og:title', content: 'Hello next.js!' }
]}
/>
<Component {...pageProps} />
</Container>
)
}
}
14 changes: 0 additions & 14 deletions examples/with-react-helmet/pages/_document.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,10 @@ export default class extends Document {
.map(el => this.props.helmet[el].toComponent())
}

get helmetJsx () {
return (
<Helmet
htmlAttributes={{ lang: 'en' }}
title='Hello next.js!'
meta={[
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
{ property: 'og:title', content: 'Hello next.js!' }
]}
/>
)
}

render () {
return (
<html {...this.helmetHtmlAttrComponents}>
<Head>
{this.helmetJsx}
{this.helmetHeadComponents}
</Head>
<body {...this.helmetBodyAttrComponents}>
Expand Down
30 changes: 10 additions & 20 deletions examples/with-react-helmet/pages/about.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
import React from 'react'
import Helmet from 'react-helmet'

export default class About extends React.Component {
static async getInitialProps ({ req }) {
if (req) {
Helmet.renderStatic()
}
return { title: 'About' }
}

render () {
const { title } = this.props
return (
<div>
<Helmet
title={`${title} | Hello next.js!`}
meta={[{ property: 'og:title', content: title }]}
/>
About the World
</div>
)
}
export default function About () {
return (
<div>
<Helmet
title='About | Hello next.js!'
meta={[{ property: 'og:title', content: 'About' }]}
/>
About the World
</div>
)
}