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.

Referencing Component inside getServerSideProps breaks build (Next.js 12)

See original GitHub issue

What version of Next.js are you using?

12.0.3

What version of Node.js are you using?

16.6.1

What browser are you using?

Chrome, Firefox

What operating system are you using?

macOS

How are you deploying your application?

Doesn’t matter

Describe the Bug

After upgrading my project to Next.js 12 the generated builds are not correct. I’ve identified the following issue:

If a component is referenced inside getServerSideProps as shown below the generated build is incorrect and gives client side errors. This issue didn’t happen on Next 11.

import { ComponentFoo } from "../components/ComponentFoo";

export default function Home() {
  return (
    <div>
      <ComponentFoo />
    </div>
  );
}

export function getServerSideProps() {
  return {
    props: { foo: ComponentFoo.fragments.foo },
  };
}

I’ve created a minimal reproduction here which is deployed on vercel here.

Basically the error is that the generated build for the client is not including the component definition. See comparison . Referencing the component from inside getServerSideProps: Screenshot 2021-10-29 at 11 14 25

Not referencing the component from inside getServerSideProps: Screenshot 2021-10-29 at 11 19 10

This is generating a ComponentFoo is not defined error when rehydrating on the client.

Reason why I’m referencing the components: I attach GraphQL fragments to the components specifying the data they need to render. When calling getServerSideProps I need to reference those fragments in order to fetch the necessary data.

Expected Behavior

I would it expect to work. The same way it worked on Next 11.

To Reproduce

Minimal reproduction: https://github.com/santialbo/test-next-build Vercel deployment: https://test-next-build.vercel.app/

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
balazsorban44commented, Jan 14, 2022

Hi, this has been fixed and is working now, just tested with 12.0.8. Please upgrade.

1reaction
benknightcommented, Nov 22, 2021

Thank you for reporting this issue. I am experiencing exactly the same problem after updating to version 12.

I am using the same pattern as OP where I’m defining GraphQL fragments as fields on my exported components.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Data Fetching: getServerSideProps - Next.js
Fetch data on each request with `getServerSideProps`. ... Instead, directly import the logic used inside your API Route into getServerSideProps .
Read more >
Next.js , pass getServerSideProps and children props in the ...
You have two options: 1) fetch the data server-side in _app inside getInitialProps then pass it down to the Layout component; 2) fetch...
Read more >
Server-side rendered styled-components with Nextjs
styled-components supports concurrent server side rendering, with stylesheet rehydration. The basic idea is that everytime you render your app ...
Read more >
Best practices to increase the speed for Next.js apps
Use server-side rendering ; async function getServerSideProps({context}) ; const data = await ; // Returning the fetched data return ; function ...
Read more >
What's New in Next.js 13 — and Why it Matters
js is a development framework which builds on React by enabling server-side rendering, additional server-side functionality and an improved ...
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