question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

gatsby-plugin-sass: importing scss file in one page imports stying globally for all pages during development

See original GitHub issue

Description

During development (gatsby develop) css styling should only be applied to pages where I explicitly import .scss file. However, importing .scss file to a single page also applies css styling globally to all other pages that do not import scss file.

Steps to reproduce

  1. gatsby new gatsby-site https://github.com/gatsbyjs/gatsby-starter-hello-world

  2. npm i gatsby-plugin-sass sass

  3. configure gatsby-config.js

module.exports = {
  plugins: [
    {
      resolve: `gatsby-plugin-sass`,
      options: {
        implementation: require("sass"),
        includePaths: ["src/scss"],
      },
    },
  ],
}
  1. add .scss file(s) under src/scss folder
h1 {
  font-family: 'Nunito';
}
  1. create 2 pages (index.js and foo.js), but only import .scss file in one (foo.js)

index.js

import React from "react"

export default function Home() {
  return <h1>Hello world!</h1>
}

foo.js

import React from "react"
import "../scss/style.scss"

export default function Foo() {
  return <h1>Hello foo!</h1>
}

  1. run gatsby develop

Expected result

What should happen?

css styling should only be applied to the page importing .scss (foo.js). css styling should not be applied to the page without the .scss import (index.js)

What happened.

css styling was applied to pages without .scss import (index.js)

index

Environment

System: OS: Windows 10 10.0.18363 CPU: (8) x64 Intel® Core™ i7-6700K CPU @ 4.00GHz Binaries: Node: 14.15.0 - C:\Program Files\nodejs\node.EXE npm: 6.14.8 - C:\Program Files\nodejs\npm.CMD Browsers: Chrome: 86.0.4240.198 Edge: Spartan (44.18362.449.0) npmPackages: gatsby: ^2.25.3 => 2.25.3 gatsby-plugin-sass: ^2.5.1 => 2.5.1 npmGlobalPackages: gatsby-cli: 2.12.117

gatsby-site.zip

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
kimbaudicommented, Nov 16, 2020

we don’t code-split CSS as it will result in unforeseen implications

But this leads to another unforseen implication: importing css/scss files to a single page component will apply styling to other page components during development mode.

This is a problem that many developers will face when they realize that there production site styling is different from their site during development. see this comment

we need some guarantee that our website during development will look the same as it would on production. But currently as it stands, Gatsby might falsely apply css/scss styling to pages during development and give developers the illusion that css styling has been applied to a page in production, when it has not.

this is something that gatsby users need to be aware of. this is a dangerous side-effect and bad developer experience because you get css styling on a page in dev mode when you haven’t even imported css/scss file to that page.

1reaction
kimbaudicommented, Nov 17, 2021

I’m not sure about a proper solution, but I’ve moved a lot of my styling from global scss into styled components using @emotion. I just accept that there is a global scss file and there is no way to split css.

Read more comments on GitHub >

github_iconTop Results From Across the Web

gatsby-plugin-sass
gatsby -plugin-sass Provides drop-in support for Sass/SCSS stylesheets Install ... Write your stylesheets in Sass/SCSS and require or import them as normal.
Read more >
Include sass in gatsby globally - Stack Overflow
This approach is working, but the problem is, as the project grows, the styles.scss is being imported multiple times and seems to be...
Read more >
Styling Gatsby Site with CSS - Ibaslogic
First, we tell Gatsby to process the SCSS file as a CSS module by appending to the file .module.scss . This module is...
Read more >
GatsbyJS Setup with Sass and Markdown Support
I created a new src/styles/main.scss file that we can now import in Gatsby's gatsby-browser.js file to pull it in whenever our app starts...
Read more >
How to use global Sass styles in Gatsby? - Embold Blog
css and layout.js in src/components folder. Rename layout.css to layout.scss and write your global styles in it. Then update the import command ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found