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 using App dir, Global CSS seems to get bundled incorrectly #48120

Open
1 task done
SamuelQuinones opened this issue Apr 8, 2023 · 8 comments
Open
1 task done
Labels
area: app App directory (appDir: true) bug Issue was opened via the bug report template.

Comments

@SamuelQuinones
Copy link

SamuelQuinones commented Apr 8, 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.3.0: Mon Jan 30 20:38:43 PST 2023; root:xnu-8792.81.3~2/RELEASE_ARM64_T8112
    Binaries:
      Node: 16.18.0
      npm: 8.19.2
      Yarn: N/A
      pnpm: N/A
    Relevant packages:
      next: 13.3.1-canary.3
      eslint-config-next: 13.2.4
      react: 18.2.0
      react-dom: 18.2.0

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

App directory (appDir: true)

Link to the code that reproduces this issue

https://github.com/SamuelQuinones/samtheq.com/tree/main

To Reproduce

Using the above repository

  1. Update src/app/layout.tsx to import @fortawesome/fontawesome-svg-core/styles.css directly, making sure to put it above import "../styles/globals.css";
  2. remove the import "../styles/fa.css"; line
  3. If Possible, seed a database with prisma
  4. if that is not possible, in the src/app/experience/page.tsx file, just have getExperienceItems return the following (so you can see data):
{
  lastUpdated: 2022-10-10T02:54:46.000Z,
  error: false,
  experienceItems: [
    {
      ID: 3,
      description: 'Incognito but reversed',
      start_date: 2022-04-02T00:00:00.000Z,
      end_date: null,
      signifier: 'Super User',
      place: 'Cognito Inc',
      exp_type: 'work',
      additional_info: []
    },
    {
      ID: 2,
      description: 'Created fake data for demos',
      start_date: 2022-01-02T00:00:00.000Z,
      end_date: 2022-04-01T00:00:00.000Z,
      signifier: 'Super User',
      place: 'Not Real Inc.',
      exp_type: 'work',
      additional_info: []
    },
    {
      ID: 5,
      description: 'Made Keyboards',
      start_date: 2021-02-04T00:00:00.000Z,
      end_date: null,
      signifier: 'Bachelors in Design',
      place: 'MKB College',
      exp_type: 'education',
      additional_info: [Array]
    },
    {
      ID: 1,
      description: 'Came up with a fake company',
      start_date: 2021-01-01T00:00:00.000Z,
      end_date: 2020-01-01T00:00:00.000Z,
      signifier: 'CEO',
      place: 'Fake Company LLC',
      exp_type: 'work',
      additional_info: [Array]
    },
    {
      ID: 4,
      description: 'Took latin courses',
      start_date: 2019-01-01T00:00:00.000Z,
      end_date: 2019-12-31T00:00:00.000Z,
      signifier: 'Masters In Latin',
      place: 'Lorem Ipsum University',
      exp_type: 'education',
      additional_info: [Array]
    }
  ]
}
  1. Build the app
  2. Run the app using npm run start

Describe the Bug

When importing styles from node_modules and local styles as global styles, import order is preserved in development.

Given the following imports in my root layout file:

import "@fortawesome/fontawesome-svg-core/styles.css";
import "../styles/globals.css";

Using the provided repo, if I navigate to my experience page, notice the size of the icons:
Screenshot 2023-04-07 at 10 23 55 PM

However if i then build the app, and run it the icons are small:
Screenshot 2023-04-07 at 10 36 34 PM

Looking at the Dev tools, it looks like even though my fontawesome import is first - and should therefore be overwritten by the tailwind rule that sets the height and width as it does in development) for some reason when bundling, the css order gets mixed up (this is the prod screenshot):
Screenshot 2023-04-07 at 10 30 38 PM

Expected Behavior

Css order should be consistent across all environments. It is also worth noting that this does not seem to happen when using the pages folder and an _app.tsx file.

I am able to get around it by using the trick you see in the repo which is to import that node_modules css file into another css file, and then importing that globally as well like so:

import "../styles/fa.css";
import "../styles/globals.css";

Which browser are you using? (if relevant)

Chrome, FireFox, Safari

How are you deploying your application? (if relevant)

next start

@SamuelQuinones SamuelQuinones added the bug Issue was opened via the bug report template. label Apr 8, 2023
@github-actions github-actions bot added the area: app App directory (appDir: true) label Apr 8, 2023
@WoLfulus
Copy link

I'm seeing this while in development. I'm not sure, but seems like the hot reload reorders the css somehow, causing styles to break randomly.

@philwolstenholme
Copy link
Contributor

philwolstenholme commented Apr 29, 2023

Might be the same issue as #48807 or #47585

@SamuelQuinones
Copy link
Author

Might be the same issue as #48807 or #47585

I don't believe it is the same issue as #48807 as hot reloading itself works fine, and things work as expected in development.

I can't say for certain if its the same as #47585 as I am not using link tags for my css, I'm importing them directly into the file.

The big issue here is that for me things work completely fine in dev, and issues occur in prod

@Frikki
Copy link

Frikki commented May 4, 2023

I believe this is a #47585 issue.

@SamuelQuinones
Copy link
Author

An update: This is still happening as of the most recent release

timneutkens pushed a commit that referenced this issue May 30, 2023
This PR fixes a couple of categories of CSS issues in App Router, that
come from the same root cause.

### 1. Duplicated styles being loaded in different layers

This issue has been described in
vanilla-extract-css/vanilla-extract#1088 (comment).
If a CSS module (or a global CSS) is referenced in multiple layers (e.g.
a layout and a page), it will be bundled into multiple CSS assets
because each layer is considered as a separate entry.

<img width="1141" alt="CleanShot-2023-05-26-GoB9Rhcs@2x"
src="https://github.com/vercel/next.js/assets/3676859/8e0f5346-ee64-4553-950a-7fb44f769efc">

As explained in that issue, we have to bundle all CSS modules into one
chunk to avoid a big number of requests.

### 2. CSS ordering issues (conflicts)

This is likely causing #48120.
When the layer-based bundling and ordering logic applies to CSS, it can
potentially cause non-deterministic order. In this example, button A in
the layout should be in blue. However when button B is imported by the
page, button A becomes red. This is an inconsistent experience and can
be hard to debug and fix.

<img width="1090" alt="CleanShot-2023-05-26-Ar4MN5rP@2x"
src="https://github.com/vercel/next.js/assets/3676859/4328d5d7-23af-4c42-bedf-30f8f062d96a">
@switz
Copy link

switz commented Jun 8, 2023

I'm still seeing this even on 13.4.5-canary.8

yup, I have the following css imports in my root app layout

import 'npm_module/dist/index.css';
import '../styles/local.css';
import '../styles/local2.css';
import 'npm_module2/dist/other.css';

the two npm modules are combined and moved after the 2 local css files, which are combined themselves.

I'm using pnpm if that affects anything, did not see this issue when using yarn.

hydRAnger pushed a commit to hydRAnger/next.js that referenced this issue Jun 12, 2023
This PR fixes a couple of categories of CSS issues in App Router, that
come from the same root cause.

### 1. Duplicated styles being loaded in different layers

This issue has been described in
vanilla-extract-css/vanilla-extract#1088 (comment).
If a CSS module (or a global CSS) is referenced in multiple layers (e.g.
a layout and a page), it will be bundled into multiple CSS assets
because each layer is considered as a separate entry.

<img width="1141" alt="CleanShot-2023-05-26-GoB9Rhcs@2x"
src="https://github.com/vercel/next.js/assets/3676859/8e0f5346-ee64-4553-950a-7fb44f769efc">

As explained in that issue, we have to bundle all CSS modules into one
chunk to avoid a big number of requests.

### 2. CSS ordering issues (conflicts)

This is likely causing vercel#48120.
When the layer-based bundling and ordering logic applies to CSS, it can
potentially cause non-deterministic order. In this example, button A in
the layout should be in blue. However when button B is imported by the
page, button A becomes red. This is an inconsistent experience and can
be hard to debug and fix.

<img width="1090" alt="CleanShot-2023-05-26-Ar4MN5rP@2x"
src="https://github.com/vercel/next.js/assets/3676859/4328d5d7-23af-4c42-bedf-30f8f062d96a">
@jwalcher
Copy link

I'm experiencing this kind of random ordering of global CSS in 13.5.2. Is this still an issue for anyone, or is there another thread with up-to-date discussion?

@avarayr
Copy link

avarayr commented Jan 3, 2024

I am experiencing this issue which causes random full-page reloads because HMR (incorrectly) thinks CSS module hashes change between unrelated file changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: app App directory (appDir: true) bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

7 participants