Next.js: <AppTree /> is incorrect

Created on 7 Nov 2019  路  11Comments  路  Source: vercel/next.js

Bug report

Describe the bug

AppTree is sometimes incorrect in this code:

import BaseApp from "next/app";
import { renderToStaticMarkup } from "react-dom/server";

export default class App extends BaseApp {
  static async getInitialProps(appCtx) {
    const initialProps: any = await BaseApp.getInitialProps(appCtx);

    // Not working
    const { AppTree } = appCtx;
    console.log("Wrong", renderToStaticMarkup(<AppTree {...initialProps} />));

    return initialProps;
  }

  render() {
    return <BaseApp {...this.props} />;
  }
}

Therefore, all the "with-apollo" examples don't work and the "next-with-apollo" package too.

To Reproduce

  1. Open this Code Sand Box : https://codesandbox.io/s/nextjs-apptree-bug-0v2tl
  2. Click on the "About page" link then on the "Home page" link
  3. Click on the console tab. You should see this and observe the current <AppTree /> is sometimes incorrect:
Current <AppTree />: <a href="/about">About page</a> 
Correct <AppTree />: <a href="/">Home page</a> 

Current <AppTree />: <a href="/about">About page</a> 
Correct <AppTree />: <a href="/about">About page</a> 

Current <AppTree />: <a href="/about">About page</a> 
Correct <AppTree />: <a href="/">Home page</a> 

Current workaround

I found a simple way to get the correct AppTree by looking at Next.js source code:

import BaseApp from "next/app";
import { renderToStaticMarkup } from "react-dom/server";

export default class App extends BaseApp {
  static async getInitialProps(appCtx) {
    const initialProps: any = await BaseApp.getInitialProps(appCtx);

    // Working
    const { Component, err, router } = appCtx;
    console.log(
      "Good",
      renderToStaticMarkup(
        <App {...{ Component, err, router }} {...initialProps} />
      )
    );

    return initialProps;
  }

  render() {
    return <BaseApp {...this.props} />;
  }
}
good first issue bug needs investigation

Most helpful comment

Experiencing the same issue, it might be some combination of AppTree and @apollo/react-ssr v3

All 11 comments

The workaround doesn't fully work because the app is not wrapped (in the Next.js source code, app is passed to a wrapApp function that adds router, etc...).

Hence, useRouter returns null.

@lcswillems It seems i have a similar problem on https://github.com/zeit/next.js/issues/9350

Experiencing the same issue, it might be some combination of AppTree and @apollo/react-ssr v3

@FlorinSenoner could you check whether it is still the case with the next 9.1.6 ?

Having this same issue trying to use Apollo getDataFromTree on the client side. AppTree doesn't seem to be updating.

yes!!! experiencing the same issue

I managed to update to next 9.2.0 and it seems to work now. Not sure if it was the same issue though

It is still not working for me on 9.2.0...

@Timer I recently opened a new issue that updates this issue: https://github.com/zeit/next.js/issues/10126

I opened this new issue because the bug worsened and it is now even easier to reproduce it. Maybe should we close this issue in favor of the new one?

Any news on this?

@Timer Any updates?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

olifante picture olifante  路  3Comments

jesselee34 picture jesselee34  路  3Comments

lixiaoyan picture lixiaoyan  路  3Comments

swrdfish picture swrdfish  路  3Comments

havefive picture havefive  路  3Comments