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

styled-jsx is always included in client bundle (even when unused) #30377

Closed
GrantASL19 opened this issue Oct 26, 2021 · 9 comments · Fixed by #32730
Closed

styled-jsx is always included in client bundle (even when unused) #30377

GrantASL19 opened this issue Oct 26, 2021 · 9 comments · Fixed by #32730

Comments

@GrantASL19
Copy link

What version of Next.js are you using?

12.0.0

What version of Node.js are you using?

16.6.2

What browser are you using?

N/A

What operating system are you using?

macOS

How are you deploying your application?

N/A

Describe the Bug

Starting with nextjs 12.0.0 styled-jsx is included in the client bundle even when it’s not used anywhere in the project. e.g. in a fresh create-next-app:

Screen Shot 2021-10-26 at 5 32 27 PM

This wasn’t the case with Next.js 11.

Expected Behavior

styled-jsx should only be included in the bundle if it’s used in the project.

To Reproduce

Here’s a reproduction based on the latest create-next-app (with the typescript preset):

https://github.com/GrantASL19/nextjs-12-unusued-styled-jsx-in-bundle

It’s stock code with WebpackBundleAnalyzer set to run in the next.config.js webpack config.

@GrantASL19 GrantASL19 added the bug Issue was opened via the bug report template. label Oct 26, 2021
@huozhi
Copy link
Member

huozhi commented Oct 26, 2021

@GrantASL19 that's expected. We change the whole architecture to make it support react concurrent mode better. Part of styled-jsx registry and context are inside next.js. The normal part of your actual styles are still in other chunks.

You can checkout the readme for the new API changes.

@huozhi huozhi closed this as completed Oct 26, 2021
@GrantASL19
Copy link
Author

Okay that makes more sense, thanks for the quick reply!

Just to be clear, it’s expected that this code will be bundled with a Next.js 12 site even if the site e.g. uses only Emotion for styling? I found this change because I noticed that each page was ~3kb larger after I upgraded to 12.

@huozhi
Copy link
Member

huozhi commented Oct 27, 2021

It will be bundled into next.js but only a small part of styled-jsx. For the 3kb increasing do you mean on the chunk files?

@GrantASL19
Copy link
Author

It will be bundled into next.js but only a small part of styled-jsx. For the 3kb increasing do you mean on the chunk files?

I mean the styled-jsx files (mostly stylesheet.js and stylesheet-registry.js) in the main chunk pictured above.

Because my previous understanding was that styled-jsx was entirely opt-in I figured this was a bug. If this is expected behaviour even if no stylesheets are loaded I understand, thanks for explaining!

@timneutkens
Copy link
Member

timneutkens commented Oct 29, 2021

Going to re-open this to investigate if it might be optimized 👍 Thanks for bringing this to our attention @GrantASL19 👍

@timneutkens timneutkens reopened this Oct 29, 2021
@timneutkens timneutkens added kind: bug and removed bug Issue was opened via the bug report template. labels Oct 29, 2021
@timneutkens timneutkens added this to the 12.0.x milestone Oct 29, 2021
@styfle styfle modified the milestones: 12.0.x, 12.0.4 Nov 5, 2021
@timneutkens timneutkens removed this from the 12.0.5 milestone Nov 17, 2021
@felixjung
Copy link

Hey, I also noticed this today when looking at our client bundle. styled-jsx is included despite us relying entirely on Emotion. Like @GrantASL19 says, it makes up 3kB, gzipped. Seems rather large for something that isn't being used.

CleanShot 2021-12-08 at 15 11 12@2x

@kodiakhq kodiakhq bot closed this as completed in #32730 Dec 22, 2021
kodiakhq bot pushed a commit that referenced this issue Dec 22, 2021
We introduced the `StyleRegistry` on server side to make it concurrent safe for requests. For client we can have only 1 registry since there's only 1 instance per browser window. Keep consistence with previous usage

Closes #32417
Closes #30377

Revert change in #31357, it's not required anymore
@huozhi
Copy link
Member

huozhi commented Dec 22, 2021

Could you please try next@12.0.8-canary.13 to see if it resolves your issue? Thanks 🙏

@GrantASL19
Copy link
Author

Could you please try next@12.0.8-canary.13 to see if it resolves your issue? Thanks 🙏

next@12.0.8-canary.13 resolves the issue, thanks! 🙏

I’m still seeing a ~4kb bundle size increase vs. 11.1.2, but that’s described by #32417. I’ll try to make a reproduction for it in January.

@balazsorban44
Copy link
Member

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.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 27, 2022
natew pushed a commit to natew/next.js that referenced this issue Feb 16, 2022
We introduced the `StyleRegistry` on server side to make it concurrent safe for requests. For client we can have only 1 registry since there's only 1 instance per browser window. Keep consistence with previous usage

Closes vercel#32417
Closes vercel#30377

Revert change in vercel#31357, it's not required anymore
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants