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.

Unwanted code elimination when using a component in `getStaticProps`

See original GitHub issue

What version of Next.js are you using?

12.0.4

What version of Node.js are you using?

v14.15.0

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

Describe the Bug

I’m encountering a runtime error, since Next.js seems to be eliminating a component from a function where it is needed.

It seems to occur when a component is used both on the page and in getStaticProps.

function PageLayout({children}) {
  return <div style={{background: 'gray'}}>{children}</div>;
}

export default function Index() {
  // ReferenceError: PageLayout is not defined
  return <PageLayout>Index</PageLayout>
}

export function getStaticProps() {
  // When this line is removed, the page no longer breaks
  console.log(PageLayout.name);
  
  return {
    props: {}
  };
}
Screenshot 2021-11-17 at 16 05 44

Note that the example is intended to be minimal, in my application the usage of the layout component in getStaticProps serves a meaningful purpose. In Next.js 11 the above code works flawlessly.

Expected Behavior

No runtime error

To Reproduce

  1. Clone https://github.com/amannn/next-compile-bug
  2. Follow the reproduction steps in the README

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
pveyescommented, Dec 21, 2021

I’m not confident with my rust skill, so I just created a draft PR as a proof of concept. The test is very basic (only covers JSXElement AST)

1reaction
pveyescommented, Dec 21, 2021

Upon investigating further, this is a regression in the swc-based compiler. If you add babel.config.js to disable SWC, the code works even in latest next.js version (12.0.7)

Here’s a repro with swc disabled https://github.com/pveyes/next-swc-ssg-code-elimination

Read more comments on GitHub >

github_iconTop Results From Across the Web

getStaticProps on _app · Discussion #10949 · vercel/next.js
If I had a shared component in _app.js , like a header with a menu, ... That approach still works with our browser-side...
Read more >
Data Fetching: getStaticProps - Next.js
Fetch data and generate static pages with `getStaticProps`. ... Next.js eliminates from the client-side bundle, you can use the next-code-elimination tool.
Read more >
Fetching and hydrating a Next.JS app using ... - Medium
JS project. How to properly use `getStaticProps`, `getServerSideProps` and `getInitialProps`. Photo by ...
Read more >
use getStaticProps in component - Stack Overflow
In the example you're using getServerSideProps instead of getStaticProps btw. Also I tried this code and it worked, I had to refresh page....
Read more >
getstaticprops not passing props - You.com | The AI Search ...
But when I read the props array inside the component it has unwanted ... with using Context and not even need Apollo Client...
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