mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
9c4eefcdbf
* Add prettier for examples directory * Fix files * Fix linting * Add prettier script in case it has to be ran again
59 lines
1.4 KiB
JavaScript
59 lines
1.4 KiB
JavaScript
import React from 'react'
|
|
import { QueryRenderer, fetchQuery } from 'react-relay'
|
|
import NextApp, { Container } from 'next/app'
|
|
|
|
import { initEnvironment, createEnvironment } from '../lib/createEnvironment'
|
|
|
|
export default class App extends NextApp {
|
|
static getInitialProps = async ({ Component, router, ctx }) => {
|
|
const { variables } = Component.getInitialProps
|
|
? await Component.getInitialProps(ctx)
|
|
: {}
|
|
|
|
try {
|
|
if (initEnvironment) {
|
|
const { environment, relaySSR } = initEnvironment()
|
|
|
|
await fetchQuery(environment, Component.query, variables)
|
|
|
|
return {
|
|
variables,
|
|
relayData: await relaySSR.getCache()
|
|
}
|
|
}
|
|
} catch (e) {
|
|
console.log(e)
|
|
}
|
|
|
|
return {
|
|
variables
|
|
}
|
|
}
|
|
|
|
render () {
|
|
const { Component, variables = {}, relayData } = this.props
|
|
const environment = createEnvironment(
|
|
relayData,
|
|
JSON.stringify({
|
|
queryID: Component.query ? Component.query().name : undefined,
|
|
variables
|
|
})
|
|
)
|
|
|
|
return (
|
|
<Container>
|
|
<QueryRenderer
|
|
environment={environment}
|
|
query={Component.query}
|
|
variables={variables}
|
|
render={({ error, props }) => {
|
|
if (error) return <div>{error.message}</div>
|
|
else if (props) return <Component {...props} />
|
|
return <div>Loading</div>
|
|
}}
|
|
/>
|
|
</Container>
|
|
)
|
|
}
|
|
}
|