vanilla-extract: Unable to render when using with NextJS v13 & React Server Components
Describe the bug
App does not render when I import a .css.ts file in a React Server Component. See component in repro repo.
Workaround is to convert the .css.ts to a default export and use swc-plugin-vanilla-extract instead of @vanilla-extract/next-plugin
// page.css.ts before
export const container = style({
background: 'blue'
})
// page.css.ts workaround
const container = style({
background: 'blue'
})
export default {
container
}
Reproduction
https://github.com/nick-kang/vanilla-extract-repro-001
System Info
System:
OS: Linux 5.15 Ubuntu 22.04.1 LTS 22.04.1 LTS (Jammy Jellyfish)
CPU: (4) x64 Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz
Memory: 29.65 GB / 46.75 GB
Container: Yes
Shell: 5.8.1 - /usr/bin/zsh
Binaries:
Node: 18.7.0 - /run/user/1000/fnm_multishells/553958_1669586582142/bin/node
Yarn: 3.2.2 - /run/user/1000/fnm_multishells/553958_1669586582142/bin/yarn
npm: 8.15.0 - /run/user/1000/fnm_multishells/553958_1669586582142/bin/npm
Watchman: 2022.07.04.00 - /home/linuxbrew/.linuxbrew/bin/watchman
Browsers:
Chrome: 107.0.5304.121
Firefox: 107.0
npmPackages:
@vanilla-extract/css: ^1.9.2 => 1.9.2
@vanilla-extract/next-plugin: ^2.1.1 => 2.1.1
@vanilla-extract/webpack-plugin: ^2.2.0 => 2.2.0
Used Package Manager
pnpm
Logs
TypeError: Cannot read properties of undefined (reading 'chunks')
at getCssInlinedLinkTags (/node_modules/.pnpm/next@13.0.5_biqbaboplfbrettd7655fr4n2y/node_modules/next/dist/server/app-render.js:362:49)
at createComponentTree (/node_modules/.pnpm/next@13.0.5_biqbaboplfbrettd7655fr4n2y/node_modules/next/dist/server/app-render.js:630:52)
at /node_modules/.pnpm/next@13.0.5_biqbaboplfbrettd7655fr4n2y/node_modules/next/dist/server/app-render.js:753:62
at Array.map (<anonymous>)
at createComponentTree (/node_modules/.pnpm/next@13.0.5_biqbaboplfbrettd7655fr4n2y/node_modules/next/dist/server/app-render.js:717:84)
at /node_modules/.pnpm/next@13.0.5_biqbaboplfbrettd7655fr4n2y/node_modules/next/dist/server/app-render.js:753:62
at Array.map (<anonymous>)
at createComponentTree (/node_modules/.pnpm/next@13.0.5_biqbaboplfbrettd7655fr4n2y/node_modules/next/dist/server/app-render.js:717:84)
at /node_modules/.pnpm/next@13.0.5_biqbaboplfbrettd7655fr4n2y/node_modules/next/dist/server/app-render.js:753:62
at Array.map (<anonymous>)
at createComponentTree (/node_modules/.pnpm/next@13.0.5_biqbaboplfbrettd7655fr4n2y/node_modules/next/dist/server/app-render.js:717:84)
at /node_modules/.pnpm/next@13.0.5_biqbaboplfbrettd7655fr4n2y/node_modules/next/dist/server/app-render.js:753:62
at Array.map (<anonymous>)
at createComponentTree (/node_modules/.pnpm/next@13.0.5_biqbaboplfbrettd7655fr4n2y/node_modules/next/dist/server/app-render.js:717:84)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async /node_modules/.pnpm/next@13.0.5_biqbaboplfbrettd7655fr4n2y/node_modules/next/dist/server/app-render.js:753:56
at async Promise.all (index 0)
at async createComponentTree (/node_modules/.pnpm/next@13.0.5_biqbaboplfbrettd7655fr4n2y/node_modules/next/dist/server/app-render.js:717:38)
at async /node_modules/.pnpm/next@13.0.5_biqbaboplfbrettd7655fr4n2y/node_modules/next/dist/server/app-render.js:993:51
### Validations
- [X] Check that there isn't [already an issue](https://github.com/vanilla-extract-css/vanilla-extract/issues) that reports the same bug to avoid creating a duplicate.
- [X] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 33
- Comments: 59 (5 by maintainers)
Commits related to this issue
- tried https://github.com/vanilla-extract-css/vanilla-extract/issues/929#issuecomment-1584340099, but failed. — committed to Sharlottes/ve-missing-style-reproduction by Sharlottes a year ago
Is maintainers of this library planning to support Next.js 13 (appDir) and React Server Component?
This issue should be resolved on
next@13.3.1-canary.6(thanks to @shuding on https://github.com/vercel/next.js/pull/48306)Working example repo here
Any updates here? Looking forward to using VE with RSC š
Alternative workaround: two small patches for NextJS + VE NextJS plugin: https://gist.github.com/bizarre/825cab8224c28e93ff1a3933642f9271 (use with patch-package).
Only caveat Iāve found so far is that
globalStyledoesnāt seem to actually be global for server components, otherwise works pretty well.people still use that?
i thought everyone moved onto yarn 4 when they handed in their rotary dial phones?
Setting up Next.js 13 w/appDir and Vanilla Extract today and ran into this as well.
Updated the above patches based on current versions (Next.js 13.2.4, Vanilla Extract Next Plugin 2.1.1).
If you havenāt used
patch-packagebefore:"postinstall": "patch-package"to thescriptskey inpackage.jsonnpm i patch-packageWhile using the canary builds, I notice styles are coming through correctly, however our sprinkles are not. Classnames are being attached to the markup but the css is not there.
~This is a large application with a lot of sprinkles. While building the project or running in dev mode, I get quite a few serialization warnings due to the sprinkles as well.~
I consolidated our sprinkles to only what was necessary which removed all serialization warnings, however the sprinkles css is still not being applied in the production build.
Iām having the same problem.
gonna sit here and wait on Next.js 13 (+ appDir) supports š
There also seem to be a 3rd issue, unrelated to the platform (Iām on MacOS), with the css being output in the build, but not included in the page, this happens with sprinkles when there are too many output classes, yet I am unsure if this is sprinkles specific, or just that sprinkles tend to increase faster than the rest.
The simultaneous usage of two different sets of sprinkles (defined separately in different
.css.tsfiles) is the only difference, with the faulty outputted css going from 8kb to 16kb and not being included in the page anymoreThis feels similar to what madjam002 described
Happy to open a dedicated issue if needed (edit: someone did, see https://github.com/vanilla-extract-css/vanilla-extract/issues/1085)
It seems there are a few different issues here (1. builds not outputting css on Windows, 2. not working without the experimental.appDir setting). Could someone open new issues for these? I have a hunch that both of those will require help from the Next.js side.
I ran into some problems getting Vanilla Extract to work with Next.js version
13.4.1. It took me a little while to figure out, but Vanilla Extract will only work whenexperimental.appDiris set totrueinnext.config.js. Because Iām using a Next.js version in which the App Router is stable, I didnāt have this option enabled. After I switched it on, Vanilla Extract started working!This is because the
hasAppDircheck here relies on theexperimental.appDiroption being set.@e2goon Youāre using the same Next.js version as me. Maybe this will fix the problem for you.
@airtonix what a way to enrich the GitHub community, way to go! šš¼š
Also running into the same issues. Patches work fine and
next devworks as expected, however when runningnext buildandnext start, none of my styles exist. The class names are there in the server component markup, just no link to the CSS file in the head, thus no style is applied. I can see the CSS files exist in.next/static/cssand they reference class names in the server component, itās just not being linked to in the layout code.I realized that it still doesnāt work for windows operating system and it has flaws, yesterday I tried it on linux operating system and it worked normally⤠I will ask specialists to pay attention to this global problem. Thanks
Weāre waiting for feedback from the Next.js team. Itās not clear how to fix this on the Vanill Extract end. https://github.com/vercel/next.js/discussions/49892
Done
Iād like to know the status of this issue.
I can confirm the same problem:
When I run both https://github.com/SuttonJack/vanilla-extract-app-dir and my own project with a similar setup on my work Mac, everything works fine, but on my Win11 desktop in dev mode, classes are correctly generated, but CSS files are not attached. No errors are reported both in the browser and in the terminal running the dev server.
I will try to gather more details when I have time, as I know what I provided here isnāt much useful, I just wanted to confirm that @KhachGevorgyan94 isnāt alone.
Thanks @SuttonJack! Your example repo works great for me, both dev and build. Export also works if you remove
export const runtime = 'experimental-edge'from page.tsx.Iāll go ahead and close this issue. If anyone encounters any new problems, please open a new issue!
@thiagop90: the steps that @bradcerasani provided are all you need. You should be able to see the patches being applied in the terminal after running
npm installYou may need to update your package versions to match those from the patches
@bizarre I tested your patch and it works for me as well. I donāt see any problem with globalStyle.
Do you want to submit a PR for VE?
Not sure what we should do about the
mod.defaultpart in nextjsā code, maybe submit a PR there too? That seems to make their code more robust, so that should be a welcome change. However, not sure about addingcss.tsto that regex. Maybe we need to find a way to fix that on our end.(Tagging @shuding hoping they might be able to help us)
I think that the linked swc plugin only emulates the previous babel plugin / test transform and doesnāt actually create the css resource.
Also, the CSS not appearing in builds isnāt just a Windows issue. Happening for me on Ubuntu also.
Thanks for this. Works with experimental appDir flag. Curious though why after many weeks thereās still no vanilla-extract update to remove this requirement.
It exposes the class name correctly on Windows 11, but it doesnāt work properly because there is no CSS. Iām having the same issue.
Using styled mdx pages as ServerComponents and
@storybook/nextjsand everything seems to be fine withdevandstorybookbut am experiencing some issues withbuild,globalStyleandcreateGlobalThemelike mentioned by others previously.Currently on
next@13.1.3using an adjusted version of @bizarre 's patch with patch-package and appDir: true in next.config.js.The issue of
globalStyleandcreateGlobalThemenot actually beeing global for server components still persists.@madjam002 Observing the same behaviour still with large chunks from sprinkles. Splitting up the property definitions and sprinkles solved it, except for the color options which seem to be too many classes generated - no matter which way I split it.
@graup There are some changes in app.render.js and checking for mod.default does not do anything for me anymore since it seems to be always defined. I was thinking about submitting a PR to VE, but appDir wonāt work without the regex fix in next and it will crash with
chunks undefinedor sth similar like in OPās post.As of now able to work fine in ādevā and āstorybookā, but would be happy to work on a PR or come back to this issue later when upgrading dependencies.
@bizarre This patch works great for me, although Iāve noticed some strange behaviour where if I have a .css.ts file that uses vanilla-extract/sprinkles and I have lots of properties passed to
defineProperties, no CSS is output when doing anext buildeven though the class names appear in the markup.next devworks fine though.If I reduce the number of properties passed to
defineProperties, it works fine. Iām guessing there is some kind of limit Iām hitting with how many CSS classes are being generated. Planning to dig a little deeper to find out what this could be.EDIT: It looks like an issue triggered by Webpack chunk splitting if the CSS chunks are large and the way that NextJS 13 flight-manifest-plugin picks up CSS chunks.
EDIT 2: I fixed it with this extremely horrible hack, this will probably break other stuff and affect ordering of CSS imports but it seems to work well for me https://gist.github.com/madjam002/55c7d60b247c12e30b65a395e29d5871