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.

Tree shaking for `getStaticProps` not working

See original GitHub issue

What version of Next.js are you using?

10.1.2

What version of Node.js are you using?

12.18.3

What browser are you using?

Firefox

What operating system are you using?

Ubuntu 20.04 LTS (Pop_OS! 20.04 LTS)

How are you deploying your application?

Vercel

Describe the Bug

Tree shaking for getStaticProps doesn’t work properly when re-exporting reusable props functions:

// lib/page.ts
import { GetStaticPathsResult } from 'next';

import { OrgJSON } from 'lib/model/org';
import { getOrgs } from 'lib/api/db/org';

export interface PageProps {
  orgs?: OrgJSON[];
}

export async function getPageProps(): Promise<{ props: PageProps }> {
  const orgs = (await getOrgs()).map((o) => o.toJSON());
  return { props: { orgs } };
}

export async function getPagePaths(): Promise<GetStaticPathsResult> {
  return { paths: [], fallback: true };
}

I then re-export those getPageProps and getPagePaths from that file in my pages:

// pages/[org]/settings/index.tsx
import { getPagePaths, getPageProps } from 'lib/page';
...
export const getStaticProps = getPageProps;
export const getStaticPaths = getPagePaths;

And the getPagePaths and getPageProps aren’t properly tree shaken. They still try to appear in the client-side bundle; I know this because my build fails with the error:

12:15:38.782 | Failed to compile.
12:15:38.783 | ModuleNotFoundError: Module not found: Error: Can't resolve 'fs' in '/vercel/path0/node_modules/winston/dist/winston/transports'
12:15:38.783 | > Build error occurred
12:15:38.784 | Error: > Build failed because of webpack errors
12:15:38.784 | at /vercel/path0/node_modules/next/dist/build/index.js:17:924
12:15:38.784 | at async Span.traceAsyncFn (/vercel/path0/node_modules/next/dist/telemetry/trace/trace.js:5:584)
12:15:38.880 | Error: Command "yarn run build" exited with 1

That ☝️ error occurs because my API logger (Winston which uses fs which is only available in server-side environments) is being imported into lib/api/db/org.ts which is imported by lib/page.ts which is (incorrectly) not tree shaken by Next.js even though it is only used in getStaticProps.

Expected Behavior

Next.js should tree shake imports that are re-exported as getStaticProps or getStaticPaths (and not include them in the client-side bundle).

To Reproduce

See issue description.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
timneutkenscommented, Aug 4, 2021

Can you share a full reproduction? You can check the tree shaking here: https://next-code-elimination.vercel.app/. Your example of:

import { getPagePaths, getPageProps } from 'lib/page';

export const getStaticProps = getPageProps;
export const getStaticPaths = getPagePaths;

Is correctly shaken from the page file, so it’s likely something else.

A separate module can’t contain both client-side JS and server-side JS as the tree shaking of getStaticProps, getStaticPaths, and getServerSideProps only runs on the page file itself, if you end up doing something like:

import { getPagePaths, getPageProps, SomeComponent } from 'lib/page';

export const getStaticProps = getPageProps;
export const getStaticPaths = getPagePaths;

export default function Home() {
  // Note that SomeComponent is imported from `lib/page` which imports server-only modules like `fs`
  return <SomeComponent />
}

It will not work because the tree shaking only removes the getPagePaths and getPageProps import and does not actually change lib/page.

0reactions
balazsorban44commented, Jan 27, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tree shaking at build time with the help of get static props
Imagine there is a component, huge in size and an api at build time decides whether to show it or not like so....
Read more >
Tree shaking not working for node package - Stack Overflow
Tree shaking not working for node package · I'm pretty certain it doesn't like var color = colorExport; . Try using export {...
Read more >
Have a JavaScript Module Not Found Error ... - Airbrake Blog
Ensure that modules have been imported correctly by tree shaking. Tree shaking allows you to automatically remove dead code when bundling ...
Read more >
Dev is painfully slow : r/nextjs - Reddit
Since we've switched to next, running the app in dev mode has been extremely slow. ... My guess is you're not tree-shaking your...
Read more >
Tim on Twitter: "Over the past few years, we've been building a ...
In order to make this happen, we implemented all built-in Next.js transforms like styled-jsx, tree shaking of getStaticProps, getStaticPaths, ...
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