-
Notifications
You must be signed in to change notification settings - Fork 26.8k
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
Tree shaking for getStaticProps
not working
#27741
Comments
Temporary fix for vercel/next.js#27741
// pages/[org]/settings/index.tsx
import { getPagePaths, getPageProps } from 'lib/page';
//...
export { getPageProps as getStaticProps};
export { getPagePaths as getStaticPaths}; |
I also encountered a similar problem. I used the refactoring function of vscode to extract all the code that only runs on the server to a new file, and the problem was solved. |
Can you share a full reproduction? You can check the tree shaking here: https://next-code-elimination.vercel.app/. Your example of:
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
It will not work because the tree shaking only removes the |
@timneutkens I get a similar error on build if I don't do this on my next.config.js: module.exports = {
// ...
webpack5: true,
webpack: (config, { webpack, isServer}) => {
config.externals = config.externals.concat(['mssql', 'mysql2', 'oracle', 'oracledb', 'postgres',
'redshift', 'sqlite3', 'pg', 'pg-query-stream', 'tedious', { knex: 'commonjs knex' } ]);
if (!isServer) {
config.resolve.fallback.fs = false;
}
return config;
},
} Which looks kind of of ugly to me. Basically because I don't understand it Is there a nicer, DRY way to do that than copying all that logic on each |
@glantucan you can import external files, you just have to make sure the files with server code are not also exporting client-side code. Going to close this issue as my previous comment got a 👍 |
This seems to possibly be a duplicate of #16153. |
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. |
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:I then re-export those
getPageProps
andgetPagePaths
from that file in my pages:And the
getPagePaths
andgetPageProps
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:That ☝️ error occurs because my API logger (Winston which uses
fs
which is only available in server-side environments) is being imported intolib/api/db/org.ts
which is imported bylib/page.ts
which is (incorrectly) not tree shaken by Next.js even though it is only used ingetStaticProps
.Expected Behavior
Next.js should tree shake imports that are re-exported as
getStaticProps
orgetStaticPaths
(and not include them in the client-side bundle).To Reproduce
See issue description.
The text was updated successfully, but these errors were encountered: