-
Notifications
You must be signed in to change notification settings - Fork 27.4k
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
Unused Server-Only Dynamic Import Inside typeof window === 'undefined'
Causes Error
#36514
Comments
WorkaroundA (very weird) workaround is to omit the export async function unusedServerExport() {
if (typeof window === "undefined") {
- await import("fs");
+ import("fs").then((m) => {
+ console.log("imported", m);
+ });
}
} Sandbox: https://codesandbox.io/s/empty-https-y9ws41?file=/util/shared.js:0-163 |
typeof window
checktypeof window
check
typeof window
checktypeof window
check Bundled in Client
typeof window
check Bundled in Clienttypeof window === 'undefined'
Causes Error
Seems the main difference between the Babel/SWC approach is that the Babel transform we used to transform // util/shared.js
export function unusedServerExport() {
if (typeof window === "undefined") {
import("fs");
}
}
export function usedClientExport() {
console.log("yay");
} Result with SWC: // util/shared.js
export function unusedServerExport() {
if ("object" === "undefined") {
import("fs");
}
}
export function usedClientExport() {
console.log("yay");
} Result with next/babel: // util/shared.js
export function unusedServerExport() {
if (false) {
import("fs");
}
}
export function usedClientExport() {
console.log("yay");
} |
Ok, but two things:
Maybe there are two different bugs here... |
Note that Next.js does not claim to even tree-shake these, we only tree shake get(Static/ServerSide)Props and getStaticPaths. For |
So am I understanding correctly that it's impossible to write a shared util file like this with Next.js? By "shared util" I mean a file including:
That would be too bad! It's nice to group functions for both server and client in the same util file... |
Or is it the current recommendation of the Next.js team to use the Seems a bit unusual to have to do this, especially when the code is not being used. module.exports = {
webpack: (config, { isServer }) => {
if (!isServer) {
config.node = {
fs: 'empty'
};
}
return config;
}
}; |
Correct, it's a compilation limitation. With Server Components there will be a file-level convention to mark something as server-only. Related: #16153 (comment) |
Ok, maybe the We'll also try out the hacky-feeling workaround to see if this is any more ergonomic. |
@karlhorky I also hit this problem when trying to create an isomorphic utility and am trying to work around it. So far I have managed by dynamically importing the module (which contains server-only code and imports) within a block which only executes in the server context (dependent on a |
Also we need to ensure tree shaking so all the isomorphic server specific code does not get included in the client bundle |
Verify canary release
Provide environment information
$ npx --no-install next info Operating System: Platform: linux Arch: x64 Version: #112-Ubuntu SMP Thu Feb 3 13:50:55 UTC 2022 Binaries: Node: 14.18.1 npm: 6.14.15 Yarn: 1.22.17 pnpm: N/A Relevant packages: next: 12.1.6-canary.9 react: 18.0.0 react-dom: 18.0.0
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
Hi there! First of all, thanks for your continued effort on Next.js! 🙌 Really amazing framework.
Importing a client-only function in a Next.js page causes bundling of the server-only code contained within the
typeof window === 'undefined'
check:Error:
Sandbox: https://codesandbox.io/s/recursing-lalande-k3w986?file=/util/shared.js
cc @balazsorban44 @sokra
Expected Behavior
unusedServerExport
) are removed using tree shaking or dead code elimination and not included in the client bundletypeof window === 'undefined'
is not included in the client bundleTo Reproduce
https://codesandbox.io/s/recursing-lalande-k3w986?file=/util/shared.js
https://codesandbox.io/s/unruffled-fire-jrkz7x?file=/pages/index.js (also here on StackBlitz: https://stackblitz.com/edit/nextjs-wmguir?file=pages%2Findex.js)
Potentially Related Issues
if (typeof window === 'undefined')
is not removed from the browser bundle. #30892The text was updated successfully, but these errors were encountered: