<h1 dir="auto">Bug report</h1>
<p dir="auto">I put a React Context Provider in my custom _app.js, and used the context in my pages. This resulted in undefined context in SSR.</p>
<h2 dir="auto">Describe the bug</h2>
<p dir="auto">_app.js</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import React, { Fragment } from 'react';
import { Modal } from '@redq/reuse-modal';
import '@redq/reuse-modal/es/index.css';
import { MyProvider } from '../../common/src/contexts/MyContext';

export default ({ Component, pageProps }) =&gt; {
  return (
    &lt;Fragment&gt;
      &lt;Modal /&gt;
      &lt;MyProvider&gt;
        &lt;Component {...pageProps} /&gt;
      &lt;/MyProvider&gt;
    &lt;/Fragment&gt;
  );
};"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-v">React</span><span class="pl-kos">,</span> <span class="pl-kos">{</span> <span class="pl-v">Fragment</span> <span class="pl-kos">}</span> <span class="pl-k">from</span> <span class="pl-s">'react'</span><span class="pl-kos">;</span>
<span class="pl-k">import</span> <span class="pl-kos">{</span> <span class="pl-v">Modal</span> <span class="pl-kos">}</span> <span class="pl-k">from</span> <span class="pl-s">'@redq/reuse-modal'</span><span class="pl-kos">;</span>
<span class="pl-k">import</span> <span class="pl-s">'@redq/reuse-modal/es/index.css'</span><span class="pl-kos">;</span>
<span class="pl-k">import</span> <span class="pl-kos">{</span> <span class="pl-v">MyProvider</span> <span class="pl-kos">}</span> <span class="pl-k">from</span> <span class="pl-s">'../../common/src/contexts/MyContext'</span><span class="pl-kos">;</span>

<span class="pl-k">export</span> <span class="pl-k">default</span> <span class="pl-kos">(</span><span class="pl-kos">{</span> Component<span class="pl-kos">,</span> pageProps <span class="pl-kos">}</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
  <span class="pl-k">return</span> <span class="pl-kos">(</span>
    <span class="pl-c1">&lt;</span><span class="pl-ent">Fragment</span><span class="pl-c1">&gt;</span>
      <span class="pl-c1">&lt;</span><span class="pl-ent">Modal</span> <span class="pl-c1">/</span><span class="pl-c1">&gt;</span>
      <span class="pl-c1">&lt;</span><span class="pl-ent">MyProvider</span><span class="pl-c1">&gt;</span>
        <span class="pl-c1">&lt;</span><span class="pl-ent">Component</span> <span class="pl-kos">{</span>...<span class="pl-s1">pageProps</span><span class="pl-kos">}</span> <span class="pl-c1">/</span><span class="pl-c1">&gt;</span>
      <span class="pl-c1">&lt;</span><span class="pl-c1">/</span><span class="pl-ent">MyProvider</span><span class="pl-c1">&gt;</span>
    <span class="pl-c1">&lt;</span><span class="pl-c1">/</span><span class="pl-ent">Fragment</span><span class="pl-c1">&gt;</span>
  <span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">_document.js</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import Document, { Head, Main, NextScript } from 'next/document';
import { ServerStyleSheet } from 'styled-components';
import FavIcon from 'common/src/assets/image/favicon.png';

export default class CustomDocument extends Document {
  static async getInitialProps(ctx) {
    const sheet = new ServerStyleSheet();
    const originalRenderPage = ctx.renderPage;

    try {
      ctx.renderPage = () =&gt;
        originalRenderPage({
          enhanceApp: App =&gt; props =&gt; sheet.collectStyles(&lt;App {...props} /&gt;),
        });

      const initialProps = await Document.getInitialProps(ctx);
      return {
        ...initialProps,
        styles: (
          &lt;&gt;
            {initialProps.styles}
            {sheet.getStyleElement()}
          &lt;/&gt;
        ),
      };
    } finally {
      sheet.seal();
    }
  }
  render() {
    return (
      &lt;html lang=&quot;en&quot;&gt;
        &lt;Head&gt;
          &lt;link rel=&quot;shortcut icon&quot; type=&quot;image/x-icon&quot; href={FavIcon} /&gt;
        &lt;/Head&gt;
        &lt;body&gt;
          &lt;Main /&gt;
          &lt;NextScript /&gt;
        &lt;/body&gt;
      &lt;/html&gt;
    );
  }
}"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-v">Document</span><span class="pl-kos">,</span> <span class="pl-kos">{</span> <span class="pl-v">Head</span><span class="pl-kos">,</span> <span class="pl-v">Main</span><span class="pl-kos">,</span> <span class="pl-v">NextScript</span> <span class="pl-kos">}</span> <span class="pl-k">from</span> <span class="pl-s">'next/document'</span><span class="pl-kos">;</span>
<span class="pl-k">import</span> <span class="pl-kos">{</span> <span class="pl-v">ServerStyleSheet</span> <span class="pl-kos">}</span> <span class="pl-k">from</span> <span class="pl-s">'styled-components'</span><span class="pl-kos">;</span>
<span class="pl-k">import</span> <span class="pl-v">FavIcon</span> <span class="pl-k">from</span> <span class="pl-s">'common/src/assets/image/favicon.png'</span><span class="pl-kos">;</span>

<span class="pl-k">export</span> <span class="pl-k">default</span> <span class="pl-k">class</span> <span class="pl-v">CustomDocument</span> <span class="pl-k">extends</span> <span class="pl-v">Document</span> <span class="pl-kos">{</span>
  <span class="pl-k">static</span> <span class="pl-k">async</span> <span class="pl-en">getInitialProps</span><span class="pl-kos">(</span><span class="pl-s1">ctx</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">const</span> <span class="pl-s1">sheet</span> <span class="pl-c1">=</span> <span class="pl-k">new</span> <span class="pl-v">ServerStyleSheet</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-k">const</span> <span class="pl-s1">originalRenderPage</span> <span class="pl-c1">=</span> <span class="pl-s1">ctx</span><span class="pl-kos">.</span><span class="pl-c1">renderPage</span><span class="pl-kos">;</span>

    <span class="pl-k">try</span> <span class="pl-kos">{</span>
      <span class="pl-s1">ctx</span><span class="pl-kos">.</span><span class="pl-en">renderPage</span> <span class="pl-c1">=</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span>
        <span class="pl-s1">originalRenderPage</span><span class="pl-kos">(</span><span class="pl-kos">{</span>
          <span class="pl-en">enhanceApp</span>: <span class="pl-v">App</span> <span class="pl-c1">=&gt;</span> <span class="pl-s1">props</span> <span class="pl-c1">=&gt;</span> <span class="pl-s1">sheet</span><span class="pl-kos">.</span><span class="pl-en">collectStyles</span><span class="pl-kos">(</span><span class="pl-c1">&lt;</span><span class="pl-ent">App</span> <span class="pl-kos">{</span>...<span class="pl-s1">props</span><span class="pl-kos">}</span> <span class="pl-c1">/</span><span class="pl-c1">&gt;</span><span class="pl-kos">)</span><span class="pl-kos">,</span>
        <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

      <span class="pl-k">const</span> <span class="pl-s1">initialProps</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-v">Document</span><span class="pl-kos">.</span><span class="pl-en">getInitialProps</span><span class="pl-kos">(</span><span class="pl-s1">ctx</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
      <span class="pl-k">return</span> <span class="pl-kos">{</span>
        ...<span class="pl-s1">initialProps</span><span class="pl-kos">,</span>
        <span class="pl-c1">styles</span>: <span class="pl-kos">(</span>
          <span class="pl-c1">&lt;</span><span class="pl-c1">&gt;</span>
            <span class="pl-kos">{</span><span class="pl-s1">initialProps</span><span class="pl-kos">.</span><span class="pl-c1">styles</span><span class="pl-kos">}</span>
            <span class="pl-kos">{</span><span class="pl-s1">sheet</span><span class="pl-kos">.</span><span class="pl-en">getStyleElement</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">}</span>
          <span class="pl-c1">&lt;</span><span class="pl-c1">/</span><span class="pl-c1">&gt;</span>
        <span class="pl-kos">)</span><span class="pl-kos">,</span>
      <span class="pl-kos">}</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span> <span class="pl-k">finally</span> <span class="pl-kos">{</span>
      <span class="pl-s1">sheet</span><span class="pl-kos">.</span><span class="pl-en">seal</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span>
  <span class="pl-kos">}</span>
  <span class="pl-en">render</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">return</span> <span class="pl-kos">(</span>
      <span class="pl-c1">&lt;</span><span class="pl-ent">html</span> <span class="pl-c1">lang</span><span class="pl-c1">=</span><span class="pl-s">"en"</span><span class="pl-c1">&gt;</span>
        <span class="pl-c1">&lt;</span><span class="pl-ent">Head</span><span class="pl-c1">&gt;</span>
          <span class="pl-c1">&lt;</span><span class="pl-ent">link</span> <span class="pl-c1">rel</span><span class="pl-c1">=</span><span class="pl-s">"shortcut icon"</span> <span class="pl-c1">type</span><span class="pl-c1">=</span><span class="pl-s">"image/x-icon"</span> <span class="pl-c1">href</span><span class="pl-c1">=</span><span class="pl-kos">{</span><span class="pl-v">FavIcon</span><span class="pl-kos">}</span> <span class="pl-c1">/</span><span class="pl-c1">&gt;</span>
        <span class="pl-c1">&lt;</span><span class="pl-c1">/</span><span class="pl-ent">Head</span><span class="pl-c1">&gt;</span>
        <span class="pl-c1">&lt;</span><span class="pl-ent">body</span><span class="pl-c1">&gt;</span>
          <span class="pl-c1">&lt;</span><span class="pl-ent">Main</span> <span class="pl-c1">/</span><span class="pl-c1">&gt;</span>
          <span class="pl-c1">&lt;</span><span class="pl-ent">NextScript</span> <span class="pl-c1">/</span><span class="pl-c1">&gt;</span>
        <span class="pl-c1">&lt;</span><span class="pl-c1">/</span><span class="pl-ent">body</span><span class="pl-c1">&gt;</span>
      <span class="pl-c1">&lt;</span><span class="pl-c1">/</span><span class="pl-ent">html</span><span class="pl-c1">&gt;</span>
    <span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">Error message: TypeError: Cannot read property 'value' of undefined</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="ctx.renderPage
./pages/_document.js:11
   8 |    const originalRenderPage = ctx.renderPage;
   9 | 
  10 |    try {
&gt; 11 |      ctx.renderPage = () =&gt;
     | ^  12 |        originalRenderPage({
  13 |          enhanceApp: App =&gt; props =&gt; sheet.collectStyles(&lt;App {...props} /&gt;),
  14 |        });"><pre class="notranslate"><span class="pl-s1">ctx</span><span class="pl-kos">.</span><span class="pl-c1">renderPage</span>
<span class="pl-kos">.</span><span class="pl-c1">/</span><span class="pl-s1">pages</span><span class="pl-c1">/</span><span class="pl-s1">_document</span><span class="pl-kos">.</span><span class="pl-c1">js</span>:<span class="pl-c1">11</span>
   <span class="pl-c1">8</span> <span class="pl-c1">|</span>    <span class="pl-s1">const</span> <span class="pl-s1">originalRenderPage</span> <span class="pl-c1">=</span> <span class="pl-s1">ctx</span><span class="pl-kos">.</span><span class="pl-c1">renderPage</span><span class="pl-kos">;</span>
   <span class="pl-c1">9</span> <span class="pl-c1">|</span> 
  <span class="pl-c1">10</span> <span class="pl-c1">|</span>    <span class="pl-s1">try</span> <span class="pl-kos">{</span>
<span class="pl-c1">&gt;</span> <span class="pl-c1">11</span> <span class="pl-c1">|</span>      <span class="pl-s1">ctx</span><span class="pl-kos">.</span><span class="pl-en">renderPage</span> <span class="pl-c1">=</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span>
     <span class="pl-c1">|</span> <span class="pl-c1">^</span>  <span class="pl-c1">12</span> <span class="pl-c1">|</span>        <span class="pl-en">originalRenderPage</span><span class="pl-kos">(</span><span class="pl-kos">{</span>
  <span class="pl-c1">13</span> <span class="pl-c1">|</span>          <span class="pl-en">enhanceApp</span>: <span class="pl-v">App</span> <span class="pl-c1">=&gt;</span> <span class="pl-s1">props</span> <span class="pl-c1">=&gt;</span> <span class="pl-s1">sheet</span><span class="pl-kos">.</span><span class="pl-en">collectStyles</span><span class="pl-kos">(</span><span class="pl-c1">&lt;</span><span class="pl-ent">App</span> <span class="pl-kos">{</span>...<span class="pl-s1">props</span><span class="pl-kos">}</span> <span class="pl-c1">/</span><span class="pl-c1">&gt;</span><span class="pl-kos">)</span><span class="pl-kos">,</span>
  <span class="pl-c1">14</span> <span class="pl-c1">|</span>        <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<h1 dir="auto">Bug report</h1>
<h2 dir="auto">Describe the bug</h2>
<p dir="auto">In Next.js 8.1.0 React Context.Consumer receives value of undefined</p>
<p dir="auto">Only on server side</p>
<p dir="auto">UPDATE: Only in production, not in <code class="notranslate">npm run dev</code></p>
<h2 dir="auto">To Reproduce</h2>
<p dir="auto">Reproduce repository - <a href="https://github.com/zpnester/next-consumer-issue">link</a></p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="const MyContext = React.createContext();

function Home() {

    return &lt;MyContext.Provider value={{ x: &quot;123&quot; }}&gt;
        &lt;MyContext.Consumer&gt;
            {value =&gt; value.x}
        &lt;/MyContext.Consumer&gt;
    &lt;/MyContext.Provider&gt;
}

export default Home;"><pre class="notranslate"><span class="pl-k">const</span> <span class="pl-v">MyContext</span> <span class="pl-c1">=</span> <span class="pl-v">React</span><span class="pl-kos">.</span><span class="pl-en">createContext</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-k">function</span> <span class="pl-v">Home</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>

    <span class="pl-k">return</span> <span class="pl-c1">&lt;</span><span class="pl-v">MyContext</span><span class="pl-kos">.</span><span class="pl-v">Provider</span> <span class="pl-c1">value</span><span class="pl-c1">=</span><span class="pl-kos">{</span><span class="pl-kos">{</span> <span class="pl-c1">x</span>: <span class="pl-s">"123"</span> <span class="pl-kos">}</span><span class="pl-kos">}</span><span class="pl-c1">&gt;</span>
        <span class="pl-c1">&lt;</span><span class="pl-v">MyContext</span><span class="pl-kos">.</span><span class="pl-v">Consumer</span><span class="pl-c1">&gt;</span>
            <span class="pl-kos">{</span><span class="pl-s1">value</span> <span class="pl-c1">=&gt;</span> <span class="pl-s1">value</span><span class="pl-kos">.</span><span class="pl-c1">x</span><span class="pl-kos">}</span>
        <span class="pl-c1">&lt;</span><span class="pl-c1">/</span><span class="pl-v">MyContext</span><span class="pl-kos">.</span><span class="pl-v">Consumer</span><span class="pl-c1">&gt;</span>
    <span class="pl-c1">&lt;</span><span class="pl-c1">/</span><span class="pl-v">MyContext</span><span class="pl-kos">.</span><span class="pl-v">Provider</span><span class="pl-c1">&gt;</span>
<span class="pl-kos">}</span>

<span class="pl-k">export</span> <span class="pl-k">default</span> <span class="pl-v">Home</span><span class="pl-kos">;</span></pre></div>
<h2 dir="auto">Expected behavior</h2>
<p dir="auto">MyContext.Consumer value should receive object from MyContext.Provider</p>
<h2 dir="auto">System information</h2>
<ul dir="auto">
<li>Version of Next.js: 8.1.0</li>
</ul>
<h2 dir="auto">Additional context</h2>
<p dir="auto">Also an issue for many Antd components</p>