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

Styling broken on route navigation when using vanilla-extract #53858

Open
1 task done
ssijak opened this issue Aug 10, 2023 · 15 comments
Open
1 task done

Styling broken on route navigation when using vanilla-extract #53858

ssijak opened this issue Aug 10, 2023 · 15 comments
Labels
bug Issue was opened via the bug report template.

Comments

@ssijak
Copy link

ssijak commented Aug 10, 2023

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 22.5.0: Thu Jun  8 22:22:20 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T6000
    Binaries:
      Node: 18.12.1
      npm: 8.19.2
      Yarn: 1.22.19
      pnpm: 7.18.1
    Relevant Packages:
      next: 13.4.14-canary.1
      eslint-config-next: 13.4.5
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 4.9.4
    Next.js Config:
      output: N/A

Which area(s) of Next.js are affected? (leave empty if unsure)

No response

Link to the code that reproduces this issue or a replay of the bug

https://share.cleanshot.com/2HmrBYgs

To Reproduce

Use only pages dir, implement styling via vanilla-extract, navigate between pages using router

Describe the Bug

Styles are broken after navigating to a different page, but on full page loads they are fine
On version "next": "13.4.5", everything works fine, but after that version where big push was made with a lot of optimizations for the app dir, a lot of things started breaking for us, like styling as seen in the video and hot reload not working (any little change reloading the whole page always) so we need to stay on 13.4.5

Expected Behavior

Styles should be good all the time

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

@ssijak ssijak added the bug Issue was opened via the bug report template. label Aug 10, 2023
@MKraust
Copy link

MKraust commented Aug 18, 2023

We have exact same issue on 13.4.18 (it works fine on 13.4.12) in one single case, not for every navigation. Hard navigating to 404 page, then soft navigating to other places seems to not load some (exactly some) of the CSS. We use CSS Modules.

In our project this bug reproduces only on hard navigation to dynamic segment that throws notFound().
Hard navigation to non-existent segment doesn't produce this bug. Soft navigation to 404 doesn't produce this bug either.

I can see these lines, containing needed CSS chunk in the response

3:HL["/_next/static/css/b08a2188dd1c88b2.css",{"as":"style"}]
...
1:["$","$L5",null,{"parallelRouterKey":"children","segmentPath":["children","article","children",["slug","vimeo-video","d"],"children"],"loading":"$undefined","loadingStyles":"$undefined","hasLoading":false,"error":"$undefined","errorStyles":"$undefined","template":["$","$L6",null,{}],"templateStyles":"$undefined","notFound":"$undefined","notFoundStyles":"$undefined","childProp":{"current":["$L7","$L8",null],"segment":"__PAGE__"},"styles":[["$","link","0",{"rel":"stylesheet","href":"/_next/static/css/b08a2188dd1c88b2.css","precedence":"next"}],["$","link","1",{"rel":"stylesheet","href":"/_next/static/css/c226f2ee323fc792.css","precedence":"next"}]]}]

and after navigation I can see this script

<script>
  self.__next_f.push([1,"7:HL[\"/_next/static/css/66482b3fdfca70b9.css\",{\"as\":\"style\"}]\n8:HL[\"/_next/static/css/b08a2188dd1c88b2.css\",{\"as\":\"style\"}]\n9:HL[\"/_next/static/css/c226f2ee323fc792.css\",{\"as\":\"style\"}]\n"])
</script>
<!-- and some other big script that contains missing chunk which I think must insert <link rel="stylesheet"...> -->

But all the CSS in head is

<!-- The first one is from root app/layout.tsx -->
<link rel="stylesheet" href="/_next/static/css/1bf2443249b2433b.css" data-precedence="next">
<link rel="stylesheet" href="/_next/static/css/66482b3fdfca70b9.css" data-precedence="next">
<link rel="stylesheet" href="/_next/static/css/c226f2ee323fc792.css" data-precedence="next">

Note that b08a2188dd1c88b2.css is missing.

@MKraust
Copy link

MKraust commented Aug 18, 2023

@ssijak I've traced this bug to 13.4.14-canary.4. I want to make sure our issues have the same origin. Can you confirm that 13.4.14-canary.2 doesn't produce this bug with missing styles in your app while 13.4.14-canary-4 does? Note that 13.4.14-canary.3 is missing.

If not, I'll just open a new issue :)

@Enngage
Copy link

Enngage commented Aug 30, 2023

We are encountering this issue as well. It happens intermittently - sometimes the module styles load, sometimes they do not. Also started happening after upgrading to 13.4.19 (we went from 13.4.12)

@hazzo
Copy link

hazzo commented Sep 14, 2023

I have the same issue, in all nextjs versions... I'm starting to suspect that this might be a vanilla-extract issue. What version are you using?

@hazzo
Copy link

hazzo commented Sep 14, 2023

Definitely vanilla-extract issue vanilla-extract-css/vanilla-extract#1152

@ssijak
Copy link
Author

ssijak commented Sep 14, 2023

@hazzo It is NextJS issue in newer versions, any styling solution that does not use one big CSS file which is imported at the top level suffer from this, which is I would wager large percentage of projects

@hazzo
Copy link

hazzo commented Sep 14, 2023

@hazzo It is NextJS issue in newer versions, any styling solution that does not use one big CSS file which is imported at the top level suffer from this, which is I would wager large percentage of projects

But since which version? I tested with 13.3.4 and it still was not working...

@6mint
Copy link

6mint commented Jan 26, 2024

Can confirm the issue for 14.1.0 with App Router.
Enforcing the the css output to a single chunk as a band-aid solution is working:

// next.config.js

const { createVanillaExtractPlugin } = require("@vanilla-extract/next-plugin");

const withVanillaExtract = createVanillaExtractPlugin();

const nextConfig = {
  webpack(config, { nextRuntime }) {
    if (!nextRuntime) {
      config.optimization.splitChunks.cacheGroups = {
        ...config.optimization.splitChunks.cacheGroups,
        styles: {
          name: "styles",
          type: "css/mini-extract",
          chunks: "all",
          enforce: true,
        },
      };
    }
    return config;
  },
};

module.exports = withVanillaExtract(nextConfig);

@amar1795
Copy link

Can confirm the issue for 14.1.0 with App Router. Enforcing the the css output to a single chunk as a band-aid solution is working:

// next.config.js

const { createVanillaExtractPlugin } = require("@vanilla-extract/next-plugin");

const withVanillaExtract = createVanillaExtractPlugin();

const nextConfig = {
  webpack(config, { nextRuntime }) {
    if (!nextRuntime) {
      config.optimization.splitChunks.cacheGroups = {
        ...config.optimization.splitChunks.cacheGroups,
        styles: {
          name: "styles",
          type: "css/mini-extract",
          chunks: "all",
          enforce: true,
        },
      };
    }
    return config;
  },
};

module.exports = withVanillaExtract(nextConfig);

I have applied this but i am still facing the issue , on hard reload the styling works fine but on navigation the style breaks . I am on nextjs 14.2.3

@6mint
Copy link

6mint commented May 21, 2024

Are you using parallel routes? Next has a css loading issue for client navigation when using parallel routes. You could try updating to 14.3.0-canary.48 and see if the problem persists.

@amar1795
Copy link

amar1795 commented May 21, 2024

no, i just switched to tailwind instead and everything works fine . it's just the external css files are not working properly .
Sure will try to update and see , Thanks .

@RisingGeek
Copy link

 webpack(config, { nextRuntime }) {
    if (!nextRuntime) {
      config.optimization.splitChunks.cacheGroups = {
        ...config.optimization.splitChunks.cacheGroups,
        styles: {
          name: "styles",
          type: "css/mini-extract",
          chunks: "all",
          enforce: true,
        },
      };
    }
    return config;
  },

this works. I'm using pages router with Nextjs 14.2.4
But loading css in a single chunk is not a good idea

@amar1795
Copy link

 webpack(config, { nextRuntime }) {
    if (!nextRuntime) {
      config.optimization.splitChunks.cacheGroups = {
        ...config.optimization.splitChunks.cacheGroups,
        styles: {
          name: "styles",
          type: "css/mini-extract",
          chunks: "all",
          enforce: true,
        },
      };
    }
    return config;
  },

this works. I'm using pages router with Nextjs 14.2.4 But loading css in a single chunk is not a good idea

thanks just figured out the fix, it was my mistake all working good now

@colehart
Copy link

thanks just figured out the fix, it was my mistake all working good now

What was your fix @amar1795 ?

@amar1795
Copy link

thanks just figured out the fix, it was my mistake all working good now

What was your fix @amar1795 ?

I am not sure but I guess I was loading CSS in a single Chunk hence the issue was happening .

this happened to fix it :

webpack(config, { nextRuntime }) {
if (!nextRuntime) {
config.optimization.splitChunks.cacheGroups = {
...config.optimization.splitChunks.cacheGroups,
styles: {
name: "styles",
type: "css/mini-extract",
chunks: "all",
enforce: true,
},
};
}
return config;
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

8 participants