-
Notifications
You must be signed in to change notification settings - Fork 27.6k
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
Functional _app.js causes page getInitialProps to not run #9284
Comments
I have exactly the same issue today. Can fix this issue, please? |
I solved my problem now using this preconfiguration, I hope help you.
|
As you can see there are tests to ensure it works correctly: https://github.com/zeit/next.js/pull/9287/files#diff-a6943712c01ef0dec9f061ad7e0fe8f9 Make sure you're on the latest version of Next.js. |
I confirm this bug |
@esseswann how can you confirm a bug that has tests for the exact case outlined in the initial issue template. Please create a new issue following the issue template and provide a complete reproduction |
Please provide your _app.js and the page you're trying to access. |
I think I know what the problem is, it's ApolloClient _app.js import { createElement as $ } from 'react'
import withApolloClient from '../lib/with-apollo-client'
const MyApp = ({
Component,
pageProps // this is empty
}) =>
$(Component, pageProps)
export default withApolloClient(MyApp) index.js import { createElement as $ } from 'react'
const Index = ({ host }) =>
$('div', null, 'host is: ', host)
Index.getInitialProps = async ctx => {
console.log(ctx.req) // this does not fire
return { host:ctx.req.headers.host.split('.') }
}
export default Index with-apollo-client.js import React from 'react'
import initApollo from './init-apollo'
import Head from 'next/head'
import { getDataFromTree } from '@apollo/react-ssr'
export default App =>
class Apollo extends React.Component {
static displayName = 'withApollo(App)'
static async getInitialProps (ctx) {
const { AppTree } = ctx
let headers
let appProps = {}
if (App.getInitialProps) {
appProps = await App.getInitialProps(ctx)
}
// Run all GraphQL queries in the component tree
// and extract the resulting data
const apollo = initApollo({ headers })
if (typeof window === 'undefined') {
try {
// Run all GraphQL queries
await getDataFromTree(<AppTree {...appProps} apolloClient={apollo} />)
} catch (error) {
// Prevent Apollo Client GraphQL errors from crashing SSR.
// Handle them in components via the data.error prop:
// https://www.apollographql.com/docs/react/api/react-apollo.html#graphql-query-data-error
console.error('Error while running `getDataFromTree`', error)
}
// getDataFromTree does not call componentWillUnmount
// head side effect therefore need to be cleared manually
Head.rewind()
}
// Extract query data from the Apollo store
const apolloState = apollo.cache.extract()
return {
...appProps,
apolloState
}
}
constructor (props) {
super(props)
this.apolloClient = initApollo(props.apolloState)
}
render () {
return <App apolloClient={this.apolloClient} {...this.props} />
}
} |
+1, May be adding more docs around it'll help! |
@ksorv this bug was fixed in Next.js 9.1.3 and newer. |
@Timer Indeed. That's why I said more docs[or better] will certainly help! For anyone else coming around here:
But if you have to use them anyways, then: You've to call Page's class CustomApp extends App{
...
// Docs[of _app] does say that you can use Component in _app.js but in `getInitialProps` it doesn't say so/
static async getInitialProps({Component, ctx}){
let pageProps,
if(Component.getInitialProps) {
// Pass it ctx if you have to, idk
pageProps = await Component.getInitialProps()
}
...
return {...pageProps, ...}
}
render(){
return (
<Provider>
<Component {...pageProps}></Component>
</Provider>
}
} And now do everything normally as you would! P.S. Don't try to use them in components it wont work. Unless you do something like: const HOC = (PassedComponent)=> {
return class Some extends Component{
static async getInitialProps()...
...
// Oh and dont forget to check if you have to call PassedComponent's getInitialProps
// Also, only use this HOC on pages, You know why?
}
} |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Bug report
Describe the bug
Exporting a function
App
inpages/_app.js
as documented in #9268 causes thegetInitialProps
of pages to stop working.To Reproduce
Create a custom app as documented here: https://github.com/zeit/next.js/tree/v9.1.3-canary.1#custom-app
In a page, try using
getInitialProps
:Expected behavior
The page's
getInitialProps
should run when the page is loaded, in this example resulting inWorks!
being logged to the console.System information
Additional context
Original issue for the functional app feature: #7515
The text was updated successfully, but these errors were encountered: