question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Context provided in _app.js can't be consumed in pages in SSR

See original GitHub issue

React v16.3 context provided in pages/_app.js can be consumed and rendered in pages on the client, but is undefined in SSR. This causes React SSR markup mismatch errors.

Note that context can be universally provided/consumed within pages/_app.js, the issue is specifically when providing context in pages/_app.js and consuming it in a page such as pages/index.js.

  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

Context provided in pages/_app.js should be consumable in pages both on the server for SSR and when browser rendering.

Current Behavior

Context provided in pages/_app.js is undefined when consumed in pages for SSR. It can only be consumed for client rendering.

Steps to Reproduce (for bugs)

In pages/_app.js:

import App, { Container } from 'next/app'
import React from 'react'
import TestContext from '../context'

export default class MyApp extends App {
  render () {
    const { Component, pageProps } = this.props
    return (
      <Container>
        <TestContext.Provider value="Test value.">
          <Component {...pageProps} />
        </TestContext.Provider>
      </Container>
    )
  }
}

In pages/index.js:

import TestContext from '../context'

export default () => (
  <TestContext.Consumer>
    {value => value}
  </TestContext.Consumer>
)

In context.js:

import React from 'react'

export default React.createContext()

Will result in:

screen shot 2018-04-23 at 2 29 29 pm

Context

A large motivation for the pages/_app.js feature is to be able to provide context persistently available across pages. It’s unfortunate the current implementation does not support this basic use case.

I’m attempting to isomorphically provide the cookie in context so that graphql-react <Query /> components can get the user’s access token to make GraphQL API requests. This approach used to work with separately decorated pages.

Your Environment

Tech Version
next v6.0.0-canary.5
node v9.11.1

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:53
  • Comments:56 (24 by maintainers)

github_iconTop GitHub Comments

37reactions
timneutkenscommented, Jul 21, 2018
24reactions
timneutkenscommented, Apr 23, 2018

Unfortunately it doesn’t work as expected with commonschunk on the server. I have some ideas to fix this once and for all, but haven’t had time to implement it yet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Next.js + SSR not updating the Context API - Stack Overflow
On a page we collect some data from an API by using getServerSideProps; We send the data to the page through the props;...
Read more >
How to implement server-side rendering (SSR) in your React ...
This is the step where the NodeJS app setup the React app to use the current root, and other settings. Server sending Ready...
Read more >
Make User State Globally Accessible in Next.js with React ...
In order to consume our user context, we need to wrap our root component in our Context Provider. This can be done in...
Read more >
Server-Side Rendering (SSR) - Vue.js
Compared to a client-side Single-Page Application (SPA), the advantage of SSR ... The keyword here is static: SSG can only be applied to...
Read more >
Client API - NextAuth.js
Make sure that <SessionProvider> is added to pages/_app.js . ... check if the session is valid and then generate the requested page (SSR)....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found