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.

Unexpected bundle Buffer ployfill into client chunks

See original GitHub issue

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 21.1.0: Wed Oct 13 17:33:24 PDT 2021; root:xnu-8019.41.5~1/RELEASE_ARM64_T8101 Binaries: Node: 16.15.1 npm: 8.11.0 Yarn: 1.22.18 pnpm: N/A Relevant packages: next: 12.2.5 eslint-config-next: 12.2.5 react: 18.2.0 react-dom: 18.2.0

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

A typeof Buffer check, for example:

const isNode = typeof Buffer !== 'undefined';
console.log({ isNode });

It causes a full Buffer ployfill (22.51Kb) to be put into the client bundle.

Expected Behavior

A typeof Buffer check should just return 'undefined' in browsers instead.

Link to reproduction

https://github.com/chentsulin/nextjs-issue-40178-bundle-client-buffer

To Reproduce

Add the following lines to the pages/index.js file:

const isNode = typeof Buffer !== 'undefined';
console.log({ isNode });

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
chentsulincommented, Sep 2, 2022

I believe serialize-error is designed to be used both in browser and server. It handles DOMException as well: https://github.com/sindresorhus/serialize-error/blob/a212a8c3902fa1ff1fdef8f7625dd0cc6d6e89a1/error-constructors.js#L10-L11

If it is designed to be used only on the server-side, it doesn’t need to check Buffer exists or not at the first place.

And the weekly downloads of this package almost hits 5M. A lot of people including us already use it with redux ref: https://github.com/reduxjs/redux-toolkit/issues/2392

Read more comments on GitHub >

github_iconTop Results From Across the Web

concat patch for Buffer in node < 0.8. - GitHub
Node.js 0.8.0 and newer provides this functionality out of the box on the native buffer object ( Buffer.concat ). If you rather this...
Read more >
Can't find variable: Buffer - node.js - Stack Overflow
In TypeScript I had to import Buffer explicitly. import { Buffer } from "buffer";. I would have expected that the compiler would complain ......
Read more >
How to polyfill Buffer with Webpack 5 - viglucci.io
How to fix 'buffer is not defined' error and polyfill the Node.js Buffer API for browsers with Webpack 5.
Read more >
rollup.js
compile to a CommonJS module ('cjs') rollup main.js --file bundle.js ... This is useful for polyfills, or when the primary purpose of the...
Read more >
API - ESBuild
You have to explicitly pass the --bundle flag to enable bundling. If no input files are provided then a single input file is...
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