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

When developing with Next.js, css is not generated #1152

Closed
2 tasks done
Karibash opened this issue Jul 26, 2023 · 8 comments
Closed
2 tasks done

When developing with Next.js, css is not generated #1152

Karibash opened this issue Jul 26, 2023 · 8 comments
Labels
nextjs Issue related to NextJS

Comments

@Karibash
Copy link

Describe the bug

When a page transition is made on the Next.js development server, the css of the destination page is not generated.
Reproduced with version 2.2.1 of @vanilla-extract/next-plugin, lowering the version to 2.1.3 makes it work correctly.

Reproduction

https://stackblitz.com/edit/stackblitz-starters-dcvpht

System Info

System:
    OS: macOS 13.4.1
    CPU: (8) x64 Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz
    Memory: 1.16 GB / 32.00 GB
    Shell: 3.6.1 - /usr/local/bin/fish
  Binaries:
    Node: 18.16.0 - ~/.local/share/nvm/v18.16.0/bin/node
    npm: 9.5.1 - ~/.local/share/nvm/v18.16.0/bin/npm
  Browsers:
    Chrome: 114.0.5735.248
    Firefox: 81.0.2
    Safari: 16.5.2

Used Package Manager

pnpm

Logs

No response

Validations

@edenstrom
Copy link

edenstrom commented Aug 24, 2023

I've been looking into this a bit. It only happens between navigation in the pages directory. Navigating in the app directory works perfectly. Also navigating from a route in app to a route in pages and vice versa.

One more thing is that HMR doesn't work either.

  1. Go to home
  2. Change index.css.js (CSS is updated)
  3. Navigate to other
  4. Change other.css.js (CSS is not updated)
  5. Refresh
  6. CSS is loaded and HMR works again

No solution in sight though. Trying to debug further.

Possible culprit? #1105

EDIT 2: This patch works for us for now with only pages. Not well tested at all, and would not recommend merging it. 😅

diff --git a/dist/vanilla-extract-next-plugin.cjs.dev.js b/dist/vanilla-extract-next-plugin.cjs.dev.js
index 0f6ca91fc7cdc332b5274d54993830782366093c..1f61384b80f726b5870bf3a581501a7e99e29c58 100644
--- a/dist/vanilla-extract-next-plugin.cjs.dev.js
+++ b/dist/vanilla-extract-next-plugin.cjs.dev.js
@@ -8,6 +8,7 @@ var css = require('next/dist/build/webpack/config/blocks/css');
 var findPagesDir = require('next/dist/lib/find-pages-dir');
 var fileResolve = require('next/dist/build/webpack/config/blocks/css/loaders/file-resolve');
 var NextMiniCssExtractPluginDefault = require('next/dist/build/webpack/plugins/mini-css-extract-plugin');
+var {getGlobalCssLoader} = require('next/dist/build/webpack/config/blocks/css/loaders');
 
 function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
 
@@ -97,10 +98,23 @@ const createVanillaExtractPlugin = (pluginOptions = {}) => (nextConfig = {}) =>
     const cssRules = config.module.rules.find(rule => Array.isArray(rule.oneOf) && rule.oneOf.some(({
       test
     }) => typeof test === 'object' && typeof test.test === 'function' && test.test('filename.css'))).oneOf;
+    const postcss = () => css.lazyPostCSS(options.dir, getSupportedBrowsers(options.dir, options.dev), undefined);
     cssRules.unshift({
       test: /\.vanilla\.css$/i,
       sideEffects: true,
-      use: getVanillaExtractCssLoaders(options, resolvedNextConfig.assetPrefix)
+      use: dev ? getGlobalCssLoader(
+        {
+          assetPrefix: config.assetPrefix,
+          isClient: !isServer,
+          isServer,
+          isDevelopment: dev,
+          future: resolvedNextConfig.future || {},
+          experimental: resolvedNextConfig.experimental || {},
+          // @ts-ignore -- 'appDir' config is in beta
+          hasAppDir: false,
+        },
+        postcss,
+        []) : getVanillaExtractCssLoaders(options, resolvedNextConfig.assetPrefix)
     });
 
     // vanilla-extract need to emit the css file on both server and client, both during the

@hazzo
Copy link

hazzo commented Sep 14, 2023

Same here, pls fix this :)

@syfxlin
Copy link
Contributor

syfxlin commented Sep 18, 2023

The cause of the problem is that @vanilla-extract/next-plugin removed the next-style-loader from #1105. However, using the next- style-loader may cause inconsistent order between the development and production CSS outputs. I'm not sure if it can be added back.

@IwannabeRealnerD
Copy link

I had exactly same issue. - On page directory, css was not generated on HTML(development server)

I downgraded @vanilla-extract/next-plugin to 2.1.2(it was 2.3.1) and it's working fine.
Build result is also working fine.

NextJS - 13.3.4
@vanilla-extract/css - 1.13.0

@askoufis askoufis added the nextjs Issue related to NextJS label Nov 12, 2023
@huypham50
Copy link

Same issue, hopefully #1203 fixes this

"next": "^14.0.4"
"@vanilla-extract/css": "^1.13.0"
"@vanilla-extract/next-plugin": "^2.1.2"

@askoufis
Copy link
Contributor

I tested @vanilla-extract/next-plugin@2.4.0 in your reproduction and it has fixed the issue.

@takahiro-1227
Copy link

takahiro-1227 commented Mar 29, 2024

I upgraded to 2.4.0, but this issue is still happened.

@askoufis
Copy link
Contributor

@takahiro-1227 Could you please raise a separate issue with a minimal reproduction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
nextjs Issue related to NextJS
Projects
None yet
Development

No branches or pull requests

8 participants