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

Client build includes server-only libraries #8

Closed
Pikachews opened this issue Jul 17, 2020 · 4 comments
Closed

Client build includes server-only libraries #8

Pikachews opened this issue Jul 17, 2020 · 4 comments
Labels
enhancement New feature or request released

Comments

@Pikachews
Copy link

Pikachews commented Jul 17, 2020

I noticed that when I build a project including this library, the resulting client build includes a bunch of server libraries that bloat up the size.
For comparison, here's the output of a clean create-next-app output:

Page                                                           Size     First Load JS
┌ λ /                                                          7.08 kB        69.8 kB
├   /_app                                                      4.67 kB        62.7 kB
└ λ /404                                                       3.25 kB          66 kB
+ First Load JS shared by all                                  62.7 kB
  ├ static/pages/_app.js                                       4.67 kB
  ├ chunks/fa0d1538d4ec6ce8974fb6c1dd3701ae265e0f01.b01e96.js  10.5 kB
  ├ chunks/framework.c8d289.js                                 40.3 kB
  ├ runtime/main.71488f.js                                     6.54 kB
  └ runtime/webpack.c21266.js                                  746 B

After adding this library, here's the resulting output size:

Page                                                           Size     First Load JS
┌ λ /                                                          7.08 kB         211 kB
├   /_app                                                      145 kB          203 kB
└ λ /404                                                       3.25 kB         207 kB
+ First Load JS shared by all                                  203 kB
  ├ static/pages/_app.js                                       145 kB
  ├ chunks/e3159650bc6aa89eb4037de31e151c2fac446e71.c2e7b8.js  10.7 kB
  ├ chunks/framework.c8d289.js                                 40.3 kB
  ├ runtime/main.92af30.js                                     6.28 kB
  └ runtime/webpack.c21266.js                                  746 B

Here's an analysis of the client output with this library:
image

It seems all of the extraneous libraries stem from the importing of cookies.
If I dynamically import this library, the size goes back down to something reasonable (the analysis diagram still ends up including the extraneous libraries, although they're not loaded):

ServerCookies = (await import ('cookies')).default as any
cookies = new NodeCookiesWrapper(new ServerCookies(req, res));
Page                                                           Size     First Load JS
┌ λ /                                                          7.08 kB        72.2 kB
├   /_app                                                      6.63 kB        65.1 kB
└ λ /404                                                       3.25 kB        68.3 kB
+ First Load JS shared by all                                  65.1 kB
  ├ static/pages/_app.js                                       6.63 kB
  ├ chunks/971fcca33ddeb824ee6f37f20ba5b5cb1789aea2.c2e7b8.js  10.7 kB
  ├ chunks/framework.c8d289.js                                 40.3 kB
  ├ runtime/main.92af30.js                                     6.28 kB
  └ runtime/webpack.0b6f2e.js                                  1.18 kB

This is probably not the ideal solution, but just throwing that out there.

@Pikachews
Copy link
Author

Looks like the proper solution is one of these methods: https://arunoda.me/blog/ssr-and-server-only-modules (though #2 wouldnt apply in this case).

@bjoluc
Copy link
Owner

bjoluc commented Jul 18, 2020

Definitely, thanks for spotting this! I think the proper fix these days is using the

if (typeof window === "undefined") {
  ServerCookies = require('cookies')
}

trick. Next.js configures webpack to drop that code for client-side builds.

@bjoluc bjoluc added the enhancement New feature or request label Jul 18, 2020
@bjoluc
Copy link
Owner

bjoluc commented Jul 18, 2020

...thought I, not knowing that this does not apply to modules from node_modules. I'll use the evil eval hack then.

@bjoluc bjoluc closed this as completed in 6c3f044 Jul 18, 2020
bjoluc pushed a commit that referenced this issue Jul 18, 2020
### [1.1.3](v1.1.2...v1.1.3) (2020-07-18)

### Bug Fixes

* **Build:** Exclude server-side `cookies` library from Webpack build ([6c3f044](6c3f044)), closes [#8](#8)
@bjoluc
Copy link
Owner

bjoluc commented Jul 18, 2020

🎉 This issue has been resolved in version 1.1.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

bjoluc added a commit that referenced this issue Jan 27, 2021
…` library from client build

The `eval` hack from 6c3f044 (#8) would also exclude the `cookies` library
from server builds when using vercel.
Using the `browser` field is way cleaner anyway.

Hopefully fixes #12
bjoluc pushed a commit that referenced this issue Jan 27, 2021
### [1.1.4](v1.1.3...v1.1.4) (2021-01-27)

### Bug Fixes

* **Build:** Use `browser` field in `package.json` to exclude `cookies` library from client build ([35edd02](35edd02)), closes [#8](#8) [#12](#12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request released
Projects
None yet
Development

No branches or pull requests

2 participants