Unwanted code elimination when using a component in `getStaticProps`
See original GitHub issueWhat 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: {}
};
}
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
- Clone https://github.com/amannn/next-compile-bug
- Follow the reproduction steps in the README
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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)
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