Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unwanted code elimination when using a component in getStaticProps #31536

Closed
amannn opened this issue Nov 17, 2021 · 8 comments · Fixed by #32709
Closed

Unwanted code elimination when using a component in getStaticProps #31536

amannn opened this issue Nov 17, 2021 · 8 comments · Fixed by #32709
Labels
kind: bug Confirmed bug that is on the backlog SWC Related to minification/transpilation in Next.js.

Comments

@amannn
Copy link
Contributor

amannn commented Nov 17, 2021

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
@amannn amannn added the bug Issue was opened via the bug report template. label Nov 17, 2021
@amannn amannn changed the title Unwanted code elimination with getLayout pattern and using the layout component in getStaticProps Unwanted code elimination when using a component in getStaticProps Nov 17, 2021
@ljosberinn
Copy link
Contributor

iirc getStaticProps etc. are destructured from the page during build and then executed outside of it, not ran directly within the file and thus lose context to anything thats not an import thats exclusively used within the fn. I believe this is intended.

@amannn
Copy link
Contributor Author

amannn commented Nov 18, 2021

Thanks for the reply! I also think Next.js has some compile-time features to get rid of imports that are only used for data fetching functions and the page entry component when building the respective bundles.

However since PageLayout is used both on the page and in getStaticProps it should be available to both, right? In this case it is missing in the page bundle.

I guess the compiler has a feature to mark local variables as used for a given bundle but has a bug in detecting this case?

@ljosberinn
Copy link
Contributor

Possibly, I thought this would be already covered somewhere here.

@balazsorban44
Copy link
Member

Could you please explain the meaningful purpose?

@amannn
Copy link
Contributor Author

amannn commented Dec 10, 2021

Sure: I'm using this approach to build up a recursive dependency list of i18n message namespaces that can be used to only include the minimum of messages per page: https://github.com/amannn/next-intl/blob/1571b78688fa2735011b2170e7811e83fd574204/packages/example/src/pages/index.tsx#L24-L40.

The linked code works in Next.js 12, because I introduced the assignment to Index.messages. If L29 is removed and you'd pass the array directly to to pick in L36, the bug would occur.

A very similar use case which I haven't yet tried out with Next.js 12 is the composition of GraphQL fragments that are attached to a static property on a component.

Does that help?

@pveyes
Copy link
Contributor

pveyes commented 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

@pveyes
Copy link
Contributor

pveyes commented 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)

@timneutkens timneutkens added SWC Related to minification/transpilation in Next.js. kind: bug Confirmed bug that is on the backlog and removed bug Issue was opened via the bug report template. labels Dec 22, 2021
@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Feb 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind: bug Confirmed bug that is on the backlog SWC Related to minification/transpilation in Next.js.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants