How to fix broken CSS on Netlify when using Gatsby? - css

I'm not sure where to begin troubleshooting a gatsby/netlify issue. When I run gatsby develop locally, my site looks one way, when I deploy to Netlify, it works another.
Where does one even begin fixing/troubleshooting this sort of thing?
My gatsby-config.js
const dotenv = require("dotenv")
dotenv.config({
path: `.env.${process.env.NODE_ENV}`,
})
module.exports = {
siteMetadata: {
title: `Berlin Music Map`,
description: "Find live music in Berlin",
author: `Kyle Pennell`,
},
plugins: [
'gatsby-plugin-sharp',
'gatsby-transformer-sharp',
`gatsby-plugin-react-helmet`,
// {
// resolve: "gatsby-plugin-remote-images",
// options: {
// nodeType: "item",
// imagePath: "thumbnail",
// name: 'optimized_thumbnail',
// }
// },
{
resolve: "gatsby-plugin-remote-images",
options: {
nodeType: "item",
imagePath: "soundcloud_artwork",
name: 'optimized_soundcloud_artwork',
}
},
{
// keep as first gatsby-source-filesystem plugin for gatsby image support
resolve: 'gatsby-source-filesystem',
options: {
path: `${__dirname}/static/img`,
name: 'uploads',
},
},
{
resolve: 'gatsby-source-filesystem',
options: {
path: `${__dirname}/src/pages`,
name: 'pages',
},
},
{
resolve: `gatsby-plugin-manifest`,
options: {
name: `gatsby-starter-default`,
short_name: `starter`,
start_url: `/`,
background_color: `#663399`,
theme_color: `#663399`,
display: `minimal-ui`,
},
},
{
resolve: `gatsby-plugin-material-ui`,
options: {
theme: {
palette: {
primary: {
lighter: '#47B3C0',
light: '#397AB2',
main: '#005691',
dark: '#202E5F',
},
secondary: {
lighter: '#FFECC4',
light: '#FED969',
main: '#E7A700',
dark: '#C45527',
},
},
typography: {
html:{
fontSize:"14px"
},
h1: {
fontSize: "3.052em"
},
h2: {fontSize: "2.441em"},
h3: {fontSize: "1.953em"},
h4: {fontSize: "1.563em"},
h5: {fontSize: "1.25em"},
h6: {fontSize: "1.05em"},
fontFamily: '"Raleway", "Open Sans", "Arial", sans-serif',
useNextVariants: true,
},
},
},
},
// this (optional) plugin enables Progressive Web App + Offline functionality
// To learn more, visit: https://gatsby.dev/offline
// 'gatsby-plugin-offline',
],
}
package.json
{
"name": "gatsby-starter-default",
"private": true,
"description": "A simple starter to get up and developing quickly with Gatsby",
"version": "0.1.0",
"author": "Kyle Mathews <mathews.kyle#gmail.com>",
"dependencies": {
"#material-ui/core": "^4.5.1",
"#material-ui/icons": "^4.5.1",
"#tippy.js/react": "^3.0.1",
"axios": "^0.19.0",
"dotenv": "^8.0.0",
"gatsby": "^2.17.7",
"gatsby-cli": "^2.8.8",
"gatsby-image": "^2.2.29",
"gatsby-plugin-layout": "^1.1.13",
"gatsby-plugin-manifest": "^2.2.25",
"gatsby-plugin-material-ui": "^2.1.6",
"gatsby-plugin-offline": "^2.0.25",
"gatsby-plugin-react-helmet": "^3.0.10",
"gatsby-plugin-remote-images": "^1.0.7",
"gatsby-plugin-sharp": "^2.2.32",
"gatsby-remark-copy-linked-files": "^2.0.11",
"gatsby-remark-images": "^3.0.1",
"gatsby-remark-relative-images": "^0.2.1",
"gatsby-source-filesystem": "^2.1.35",
"gatsby-transformer-remark": "^2.3.4",
"gatsby-transformer-sharp": "^2.1.9",
"google-map-react": "^1.1.5",
"lodash": "^4.17.15",
"memoize-one": "^4.0.2",
"moment": "^2.24.0",
"prop-types": "^15.7.2",
"react": "^16.8.4",
"react-dom": "^16.8.4",
"react-fa": "^5.0.0",
"react-helmet": "^5.2.0",
"react-player": "^1.11.1",
"react-soundcloud-player": "^1.1.1",
"react-virtualized-auto-sizer": "^1.0.2",
"react-window": "^1.2.2",
"tippy.js": "^5.0.1"
},
"devDependencies": {
"prettier": "^1.16.4"
},
"keywords": [
"gatsby"
],
"license": "MIT",
"scripts": {
"build": "gatsby build",
"develop": "gatsby develop",
"format": "prettier --write src/**/*.{js,jsx}",
"start": "npm run develop",
"serve": "gatsby serve",
"test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\""
},
"repository": {
"type": "git",
"url": "https://github.com/gatsbyjs/gatsby-starter-default"
},
"bugs": {
"url": "https://github.com/gatsbyjs/gatsby/issues"
}
}
https://codesandbox.io/s/github/kpennell/berlinmusicmap/tree/master/?fontsize=14

The CSS injection order when doing gatsby build is a place to start. Material UI styles can get messed up when they are not injected first.
How to do it in the gatsby plugin:
https://www.gatsbyjs.org/packages/gatsby-plugin-material-ui/#advanced
From the MUI docs: https://material-ui.com/styles/advanced/#css-injection-order

Can you try to run gatsby build && gatsby serve on your local machine? Then go to localhost:9000 to see if your layout is all messed up.
Generally, it's good to check the built version of your Gatsby site by running this command. Especially since the new build time pricing of Netlify 😱
The issue might be independent of Netlify and related to Material UI.
Check this issue: https://github.com/mui-org/material-ui/issues/18197 you might find some useful information in it.

I (seemingly) solved this by literally copying what I had gatsby-browser.js to gatsby-ssr.js (wrapRootElement + wrapPageElement). I'm still not totally sure why this worked -- I'll need to read more about these in Gatsby's docs.
Dear frustrated/lost person from the future seeing wacky different CSS between gatsby develop and gatsby build ....if you're using React Context, try verbatim copying your wrapRootElement/wrapPageElement between the two files.

Related

Vuetify design is broken when deployed on Heroku

Assumption
I am building a service with docker and Nuxt.js.. I am using Vuetyify for the user interface, but when deployed to Heroku, the CSS design is broken. Specifically, the alert width is not applied, there are blank spaces in the alert area, and other strange behavior. It works fine in the development environment. I have looked into it, but all I found is that the CSS loads differently in the development environment than in the production environment.
Is there any solution to this problem?
What we want to solve
I want to solve Vuetify design collapse on Heroku.
Code
package.json
{
"name": "app",
"version": "1.0.0",
"private": true,
"scripts": {
"dev": "nuxt",
"build": "nuxt build",
"start": "nuxt start",
"export": "nuxt export",
"serve": "nuxt serve",
"generate": "nuxt generate",
"lint:js": "eslint --ext \".js,.vue\" --ignore-path .gitignore .",
"lint": "yarn lint:js",
"test": "jest"
},
"dependencies": {
"#nuxtjs/auth": "^4.9.1",
"#nuxtjs/axios": "^5.13.6",
"#vue/test-utils": "^1.3.0",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^27.4.6",
"core-js": "^3.9.1",
"jest": "^27.4.7",
"moment": "^2.29.1",
"nuxt": "^2.15.3",
"vue-jest": "^3.0.7"
},
"devDependencies": {
"#babel/core": "^7.16.12",
"#babel/preset-env": "^7.16.11",
"#nuxtjs/eslint-config": "^6.0.0",
"#nuxtjs/eslint-module": "^3.0.2",
"#nuxtjs/vuetify": "^1.11.3",
"babel-eslint": "^10.1.0",
"eslint": "^7.22.0",
"eslint-plugin-nuxt": "^2.0.0",
"eslint-plugin-vue": "^7.7.0"
}
}
nuxt.config.js
export default {
// Disable server-side rendering: https://go.nuxtjs.dev/ssr-mode
ssr: false,
publicRuntimeConfig: {
APP_NAME: process.env.APP_NAME,
BOOK_URL: process.env.BOOK_URL,
AXIOS_POST: process.env.AXIOS_POST
},
// Global page headers: https://go.nuxtjs.dev/config-head
head: {
title: 'KOUDOKU',
htmlAttrs: {
lang: 'ja'
},
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
{ hid: 'description', name: 'description', content: '' }
],
link: [
{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
]
},
// Global CSS: https://go.nuxtjs.dev/config-css
css: [
],
// Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
・
・
・
// Auto import components: https://go.nuxtjs.dev/config-components
components: true,
// Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
buildModules: [
// https://go.nuxtjs.dev/eslint
'#nuxtjs/eslint-module',
// Doc: https://www.npmjs.com/package/#nuxtjs/vuetify
'#nuxtjs/vuetify'
],
moment: {
locales: ['ja']
},
// Modules: https://go.nuxtjs.dev/config-modules
modules: [
// https://go.nuxtjs.dev/axios
'#nuxtjs/axios',
'#nuxtjs/auth'
],
// Axios module configuration: https://go.nuxtjs.dev/config-axios
・
・
・
// Added for anti-styling
// Build Configuration: https://go.nuxtjs.dev/config-build
build: {
extractCSS: true,
optimization: {
splitChunks: {
cacheGroups: {
styles: {
name: 'styles',
test: /\.(css|scss)$/,
chunks: 'initial',
enforce: true
},
components: {
name: 'components',
test: /app\/components/,
chunks: 'all',
enforce: true
}
}
}
}
},
router: {
middleware: ['auth']
}
}
heroku.yml
setup:
config:
NODE_ENV: production
NPM_CONFIG_PRODUCTION: false
build:
docker:
web: Dockerfile
config:
WORKDIR: app
API_URL: "https://book-app-api-v1.herokuapp.com"
run:
web: yarn run start

Tailwind css does not reduce file size after purge

i have a basic html file (index.html), my project structure is like below :
index.html
tailwind.config.js
postcss.js
tailwind.css
dist.css
and here contents for each files
module.exports = {
purge: {
enabled:true,
content:['./*.html', './**/*.html'],
layers: ['components']
},
theme: {
extend: {
fontSize:{
'small' : '.6rem',
// Or with a default line-height as well
'3xl': ['2.5rem', {
lineHeight: '50px',
}],
'6xl': ['3.70rem', {
lineHeight: '60px',
}],
},
colors:{
transparent: 'transparent',
current: 'currentColor',
orange:{
DEFAULT: '#F47521'
}
},
screens: {
'sm': '640px',
'md': '760px',
'custom' : '980px',
'lg': '1024px',
'xl': '1280px',
'2xl': '1536px',
'3xl': '1600px',
'xxl' : '1700px'
}
}
},
variants: {
textColor: ['responsive', 'hover', 'focus', 'visited'],
},
plugins: [
({addUtilities}) => {
const utils = {
'.translate-x-half': {
transform: 'translateX(50%)',
},
};
addUtilities(utils, ['responsive'])
}
]
};
the postcss file
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
}
}
and my package.json
{
"name": "myproject",
"version": "1.0.0",
"description": "my theme",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "NODE_ENV=production npx tailwindcss-cli#latest build tailwind.css -o dist.css",
"build:css": "postcss tailwind.css -o dist.css"
},
"author": "",
"license": "ISC",
"devDependencies": {
"autoprefixer": "^10.2.5",
"postcss": "^8.2.8",
"tailwindcss": "^2.0.4"
},
"dependencies": {
"cssnano": "^4.1.10",
"postcss-cli": "^8.3.1"
}
}
when building with : npm run build, tailwind build the project but the dist.css size remains 5,7MB
What i'm doing wrong here?
thank you
You have purge configured to apply to the 'components' layer.
Tailwind has three layers: 'base', 'components', and 'utilities'. Components is the smallest of the three so its impact on the resulting filesize will be fairly minimal. You're hitting 5.7MB because by far the largest layer, 'utilities', is being ignored.
Update your purge configuration to apply to utilities too. Unless there's a good reason to be selective with layers, you probably want to drop any specificity and allow it to apply to all layers.
Furthermore, if you leave out enabled, it will be handled automatically based on your NODE_ENV setting.
https://tailwindcss.com/docs/optimizing-for-production#purging-specific-layers

Gatsby Issue - on load and on refresh I am seeing a flicker of unstyled components

Issue - When I first load the page or I refresh the page, I get a flash of ugly un-styled features such as massive images, links go back to the custom styling of blue text with an underline, etc) before my own CSS applies.
Here is my package.json file -
{
"name": "gatsby-starter-hello-world",
"private": true,
"description": "A simplified bare-bones starter for Gatsby",
"version": "0.1.0",
"license": "0BSD",
"scripts": {
"build": "gatsby build",
"develop": "gatsby develop",
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,md}\"",
"start": "npm run develop",
"serve": "gatsby serve",
"clean": "gatsby clean",
"test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\" && exit 1"
},
"dependencies": {
"#mdx-js/mdx": "^1.6.22",
"#mdx-js/react": "^1.6.22",
"babel-plugin-styled-components": "^1.12.0",
"gatsby": "^2.26.1",
"gatsby-image": "^2.7.0",
"gatsby-plugin-google-fonts": "^1.0.1",
"gatsby-plugin-manifest": "^2.9.0",
"gatsby-plugin-mdx": "^1.5.0",
"gatsby-plugin-sharp": "^2.9.1",
"gatsby-plugin-styled-components": "^3.5.0",
"gatsby-remark-images": "^3.6.0",
"gatsby-source-filesystem": "^2.6.1",
"gatsby-transformer-sharp": "^2.7.0",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-helmet": "^6.1.0",
"styled-components": "^5.2.1"
},
"devDependencies": {
"prettier": "2.1.2"
},
"repository": {
"type": "git",
"url": "https://github.com/gatsbyjs/gatsby-starter-hello-world"
},
"bugs": {
"url": "https://github.com/gatsbyjs/gatsby/issues"
}
}
Here is my gatsby-config file -
module.exports = {
siteMetadata: {
title: "Gatsby Project",
description: "",
image: "/logo.png",
},
plugins: [
{
resolve: `gatsby-source-filesystem`,
options: {
name: `pages`,
path: `${__dirname}/src/pages`,
},
},
{
resolve: `gatsby-plugin-styled-components`,
},
{
resolve: `gatsby-source-filesystem`,
options: {
name: `posts`,
path: `${__dirname}/src/posts`,
},
},
{
resolve: `gatsby-source-filesystem`,
options: {
name: `images`,
path: `${__dirname}/src/images`,
},
},
`gatsby-plugin-sharp`,
`gatsby-transformer-sharp`,
{
resolve: `gatsby-plugin-mdx`,
options: {
extensions: [`.mdx`, `.md`],
gatsbyRemarkPlugins: [
{
resolve: `gatsby-remark-images`,
options: { maxWidth: 1200 },
},
],
},
},
{
resolve: `gatsby-plugin-google-fonts`,
options: {
fonts: [`roboto-mono`, `muli\:400,400i,700,700i`],
display: "swap",
},
},
{
resolve: `gatsby-plugin-manifest`,
options: {
icon: `src/images/logo.png`,
},
},
],
}
Am I missing a dependancy or is there a mistake in my config? Not sure..
Any suggestions would be much appreciated!
Usually, this kind of issue related to styled-components (known as FOUT, Flash of Unstyled Text) is solved by checking missing configurations but in your case, it seems that you have everything properly set in your gatsby-config.js and you have all packages installed.
One thing you can try is removing the gatsby-plugin-google-fonts since usually when dealing with font-face blocks the rendering while loading the JavaScript. According to https://github.com/typekit/webfontloader:
Using the Web Font Loader asynchronously avoids blocking your page
while loading the JavaScript. Be aware that if the script is used
asynchronously, the rest of the page might render before the Web Font
Loader is loaded and executed, which can cause a Flash of Unstyled
Text (FOUT).
If the issue is solved by removing this, you may need to find another way to add the fonts globally.
Credits: #LekoArts in this Spectrum topic

Chrome is not communicating changes in my browser to my local files

The Issue:
I'm trying to use chrome workspaces / source maps to update my sass partials as I edit CSS in chrome dev tools. Chrome is detecting my sass partials from source maps I setup in webpack, but giving me the error "Changes to this file were not saved to the file system." when I try to save the changes from the sources tab in chrome dev tools.
Moreover, in the styles tab of chrome dev tools, despite chrome knowing which properties are connected to which sass partials, changing the styles there doesn't update the partial files in the sources tab.
Chrome is not communicating changes in my browser to my local files, despite the sass partials showing up as source mapped in the chrome dev tools, and despite me setting up a workstation.
So far:
I set up a workspace in chrome and added the folder for the project
Disabled all my chrome extensions
Made sure 'enable css source maps' was true in chrome settings
I believe the problem might be my webpack config, since this is the first time I've made a custom webpack config, and I basically fiddled around with it until it worked. It could (probably not) be a problem with with a dependency, so I'll also add my package.json file.
Sass partial detected in chrome inspector
Sass partials showing up in sources tab:
Error Message
Webpack config file:
const path = require('path')
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: ['#babel/polyfill', './src/index.js'],
output: {
path: path.resolve(__dirname, './dist'),
filename: 'bundle.js',
publicPath: '/'
},
devServer: {
contentBase: 'dist',
historyApiFallback: true
},
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
template: './src/public/index.html'
}),
new MiniCssExtractPlugin({
filename: "[name].css",
chunkFilename: "[id].css"
})
],
module: {
rules: [
{
test: /\.sass$/,
use: [
"style-loader",
{ loader: 'css-loader', options: { sourceMap: true} },
{ loader: 'sass-loader', options: { sourceMap: true } }
],
},
{
test: /\.css$/,
use: [
"style-loader",
"css-loader"
]
},
{
test: /\.js$/,
exclude: /node_modules/,
use: [
"babel-loader"
]
}
]
}
}
Package.json:
{
"name": "css",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "webpack --mode development",
"build": "webpack --mode production",
"start": "webpack-dev-server --mode development --open"
},
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.6.4",
"#babel/plugin-proposal-class-properties": "^7.5.5",
"#babel/preset-env": "^7.6.3",
"#babel/preset-react": "^7.6.3",
"babel-eslint": "^10.0.3",
"babel-loader": "^8.0.6",
"css-loader": "^3.2.0",
"eslint": "^6.5.1",
"eslint-plugin-react": "^7.16.0",
"html-webpack-plugin": "^3.2.0",
"mini-css-extract-plugin": "^0.8.0",
"node-sass": "^4.12.0",
"sass-loader": "^8.0.0",
"style-loader": "^1.0.0",
"webpack": "^4.41.1",
"webpack-cli": "^3.3.9",
"webpack-dev-server": "^3.8.2"
},
"dependencies": {
"#babel/polyfill": "^7.6.0",
"react": "^16.10.2",
"react-dom": "^16.10.2",
"react-router-dom": "^5.1.2"
},
"eslintConfig": {
"extends": [
"eslint:recommended",
"plugin:react/recommended"
],
"env": {
"browser": true,
"node": true
}
},
"eslintIgnore": [
"webpack.config.js"
]
}
This is no longer supported in Chrome :( Relevant issue is marked as "won't fix".
https://bugs.chromium.org/p/chromium/issues/detail?id=996226

GatsbyJS + Google Analytics = "No HTTP Response" Error?

I've poured my time almost 4-5 hours to figure it out, but I couldn't have done with it... but fortunately, succeeded my Gatsby APP acknowledges the Google Analytics.
However, The problem occurs "No HTTP response Detected" from Google Tag Assistant Chrome Extension.
I've pasted the Google Analytics Snippets many times into HTML.js, Index.html and updated the latest version of GatsbyJs as well.
Additionally, I've tried to turn off my Adblock extension as well! but all failed...
URL
The error occurred blog
gatsby-config.js
module.exports = {
// Note: it must *not* have a trailing slash.
siteMetadata: {
title: 'SJ personal Blog',
siteUrl: `https://ollagada.github.io`
},
plugins: [
{
resolve: `gatsby-plugin-google-analytics`,
options: {
trackingId: "MY TRACKING ID",
// Puts tracking script in the head instead of the body
head: true,
// Setting this parameter is optional
anonymize: true,
// Setting this parameter is also optional
respectDNT: true,
// Avoids sending pageview hits from custom paths
exclude: ["/preview/**", "/do-not-track/me/too/"],
// Enables Google Optimize using your container Id
optimizeId: "YOUR_GOOGLE_OPTIMIZE_TRACKING_ID",
// Any additional create only fields (optional)
sampleRate: 5,
siteSpeedSampleRate: 10,
},
},
{
resolve: `gatsby-plugin-gtag`,
options: {
// your google analytics tracking id
trackingId: `MY TRACKING ID`,
// Puts tracking script in the head instead of the body
head: true,
// enable ip anonymization
anonymize: true,
},
},
'gatsby-transformer-remark', {
resolve: `gatsby-source-filesystem`,
options: {
name: `posts`,
path: `${__dirname}/src/pages/posts`
}
},
'gatsby-plugin-react-helmet', {
resolve: `gatsby-plugin-manifest`,
options: {
name: 'gatsby-starter-default',
short_name: 'starter',
start_url: '/',
background_color: '#663399',
theme_color: '#663399',
display: 'minimal-ui',
icon: 'src/img/moi.jpg', // This path is relative to the root of the site.
}
},
]
Package.json
{
"name": "gatsby-starter-default",
"description": "Gatsby default starter",
"version": "1.0.0",
"author": "Kyle Mathews ",
"dependencies": {
"aos": "^2.3.4",
"child_process": "^1.0.2",
"fs": "0.0.1-security",
"gatsby": "^2.0.75",
"gatsby-plugin-google-analytics": "^2.0.8",
"gatsby-plugin-gtag": "^1.0.4",
"gatsby-plugin-manifest": "^2.0.2",
"gatsby-plugin-offline": "^2.0.5",
"gatsby-plugin-react-helmet": "^3.0.0",
"gatsby-plugin-sharp": "^2.0.5",
"gatsby-source-filesystem": "^2.0.1",
"gatsby-transformer-remark": "^2.1.17",
"gatsby-transformer-sharp": "^2.1.1",
"instafeed.js": "^1.4.1",
"net": "^1.0.2",
"prop-types": "^15.6.2",
"react": "^16.4.2",
"react-dom": "^16.4.2",
"react-helmet": "^5.2.0",
"react-instafeed": "^0.4.0",
"react-lines-ellipsis": "^0.13.2",
"react-mdl": "^1.11.0",
"react-spinkit": "^3.0.0",
"tls": "0.0.1",
"typed.js": "^2.0.9",
"window": "^4.2.5"
},
"keywords": [
"gatsby"
],
"license": "MIT",
"scripts": {
"build": "gatsby build",
"develop": "gatsby develop",
"format": "prettier --write '**/*.js'",
"test": "echo \"Error: no test specified\" && exit 1",
"deploy": "gatsby build --prefix-paths && gh-pages -b master -d public"
},
"devDependencies": {
"gh-pages": "^2.0.0",
"prettier": "^1.14.2"
},
"repository": {
"type": "git",
"url": "https://github.com/gatsbyjs/gatsby-starter-default"
}
}
Thank you! Lovely Day!

Resources