gatsby: CSS styles do not load after deploying to Surge or Netlify

Description

CSS styles are loading when running ‘gatsby develop’ however, when deploying to Netlify or Surge, the styles do not load. Here are some of the solutions I tried:

  • setting gatsby-plugin-sass to 1.0.19
  • re-deploying to Netlify with build cache cleared
  • removing the yarn.lock file to force netlify to use npm
  • removing the netlify site altogether and creating a new one.

These solutions don’t seem to correct the issue of the styles not loading. Also I tried deploying to surge.sh and I’m still getting no-styles deployment.

I’m pretty befuddled at this point even after looking at similar issues

Steps to reproduce

Netlify

  1. Fork https://github.com/DanStockham/tootles-bar
  2. Add site to Netlify
  3. Deploy site

Surge

  1. Clone https://github.com/DanStockham/tootles-bar
  2. gatsby build
  3. surge public/
  4. Navigate to site

Expected result

image

Actual result

What happened. image

Environment

  • Gatsby version 1.9.253:
  • gatsby-cli version 1.1.50:
  • Node.js version: v8.9.4
  • Operating System: Windows 10

File contents (if changed):

gatsby-config.js:

require('dotenv').config();

module.exports = {
  siteMetadata: {
    title: `Tootle's Pumpkin Inn`,
  },
  plugins: [
    `gatsby-plugin-react-helmet`,
    `gatsby-plugin-glamor`,
    `gatsby-transformer-remark`,
    'gatsby-plugin-sass',
    {
      resolve: `gatsby-source-contentful`,
      options: {
        spaceId: process.env.CONTENTFUL_SPACE_ID,
        accessToken: process.env.CONTENTFUL_ACCESS_TOKEN
      }
    },
    {
      resolve: 'gatsby-source-filesystem',
      options: {
        name: 'src',
        path: `${__dirname}/src/`
      },
    },
    {
      resolve: `gatsby-plugin-typography`,
      options: {
        pathToConfigModule: `src/utils/typography`,
      },
    }],
}

package.json:

{
  "name": "gatsby-starter-default",
  "description": "Gatsby default starter",
  "version": "1.0.0",
  "author": "Kyle Mathews <mathews.kyle@gmail.com>",
  "dependencies": {
    "bootstrap": "^4.1.0",
    "dot-env": "0.0.1",
    "gatsby": "^1.9.253",
    "gatsby-image": "^1.0.47",
    "gatsby-link": "^1.6.41",
    "gatsby-paginate": "^1.0.14",
    "gatsby-plugin-glamor": "^1.6.13",
    "gatsby-plugin-react-helmet": "^2.0.11",
    "gatsby-plugin-sass": "1.0.19",
    "gatsby-plugin-typography": "^1.7.18",
    "gatsby-source-contentful": "^1.3.47",
    "gatsby-source-filesystem": "^1.5.32",
    "gatsby-transformer-remark": "^1.7.40",
    "gatsby-transformer-sharp": "^1.6.23",
    "react-fontawesome": "^1.6.1",
    "react-helmet": "^5.2.0",
    "react-transition-group": "^2.3.1",
    "reactstrap": "^5.0.0",
    "typeface-lobster": "0.0.54",
    "typography-theme-kirkham": "^0.16.3"
  },
  "keywords": [
    "gatsby"
  ],
  "license": "MIT",
  "main": "n/a",
  "scripts": {
    "build": "gatsby build",
    "develop": "gatsby develop",
    "format": "prettier --trailing-comma es5 --no-semi --single-quote --write \"src/**/*.js\"",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "devDependencies": {
    "prettier": "^1.12.1"
  }
}

gatsby-node.js: gatsby-browser.js: gatsby-ssr.js:

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 39 (13 by maintainers)

Most upvoted comments

@rishichawda @osogrizz

I’m also using styled components. This was fixed for me as soon as I added the Gatsby styled-components plugin to my build, using the steps outlined here:

https://www.gatsbyjs.org/docs/styled-components/

Before fixing this, my styles wouldn’t load on my first page load, until I navigated to another route. Now everything loads the first time.

I just ran into a similar problem as @gzhihao mentioned. In production, if I navigated to a specific page, it would only load CSS I had required in gatsby-browsers.js, but would fail to load CSS I had imported in src/pages/index.js. However, it would work if I started on the homepage, and navigated to said page. The site is hosted on netlify.

I’m testing moving my main styles.css I imported in src/pages/index.js to gatsby-browsers.js to see if that resolves it.

Edit: This seemed to resolve the issue for me.

For those that might be using Material UI and running into this problem, it was simply a matter of installing gatsby-plugin-material-ui for me! 😄

I also had the same issue running Styled Components on Netlify. Installing gatsby-plugin-styled-components and adding it to my config file did the trick.

Note: I had to clear my browser cache to see the changes.

I have the same problem as @maxmatthews. I found several issues related to this with no definitive fix.

I checked those 3 you mentioned & it looks good so I think you are definitely missing something. Can’t help it unless there is something specific.

I remember now that my error was I wasn’t using v2 of emotion gatsby plugin & I think I had to clear Netlify cache since I was using https://github.com/axe312ger/gatsby-plugin-netlify-cache which uses previous cache to speed up deployment. So those 2 were my problems.

I think you will find answer if there is something relevant you can post or else you checkout example sites made using rebase & styled-system. I did it that way. Sorry I don’t have a solution for you 😦