next.js: is incorrect
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
- Open this Code Sand Box : https://codesandbox.io/s/nextjs-apptree-bug-0v2tl
- Click on the “About page” link then on the “Home page” link
- 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} />;
}
}
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 14
- Comments: 22 (4 by maintainers)
Commits related to this issue
- Use correct page component in client-side AppTree, fixes #9336 — committed to zenflow/next.js by zenflow 6 years ago
Experiencing the same issue, it might be some combination of
AppTreeand@apollo/react-ssrv3This is still an issue and is needed for proper Apollo client SSR. We really need maintainers on this, I don’t think it’s a “good first issue”. SSR is a selling point of Next.js and this not working kinda kills that.
Fyi I was able to work around this issue by writing this
getInitialAppPropsof my_app, inspired by https://stackoverflow.com/questions/67022296/next-with-apollo-ssr-does-not-work-request-is-done-on-client/67857064#67857064To who it may concern, I put in PR #23721 to fix this.
Really hoping it will be merged so that we can use
AppTreeto preload data for pages on client-side too! IMO half the reason for SSR is so that users don’t see a bunch of initial “loading” indicators when they open a page… But why bother with that optimization if the user will see those initial “loading” indicators when navigating within the site anyways? I guess just for SEO?Any news on this?
The workaround doesn’t fully work because the app is not wrapped (in the Next.js source code,
appis passed to awrapAppfunction that adds router, etc…).Hence,
useRouterreturnsnull.When trying to use Apollo’s getDataFromTree, it crashes with a
TypeError: Cannot convert undefined or null to object, and the error comes from renderToStaticMarkup. Although I can’t figure out what is wrong with the rendering of that AppTree, it should definitely be compatible with React SSR render functions, to populate Apollo cache from Next SSR.Note that I tried to implement https://github.com/shshaw/next-apollo-ssr but it just doesn’t work with my components tree.
@Timer Any updates?