Skip to content

Commit

Permalink
fix(gatsby-plugin-styled-components): fix global styles pollution (#9922
Browse files Browse the repository at this point in the history
)

Fix #9922

`ServerStyleSheet` should be specific to each pages in order to not pollutes the other. Theses changes removes the "global" stylesheet and use the `pathname` for identify each pages.
  • Loading branch information
LoicMahieu committed Nov 15, 2018
1 parent 6e388bf commit a9f4b23
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions packages/gatsby-plugin-styled-components/src/gatsby-ssr.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import React from "react"
import { ServerStyleSheet, StyleSheetManager } from "styled-components"

const sheet = new ServerStyleSheet()
const sheetByPathname = {}

// eslint-disable-next-line react/prop-types,react/display-name
exports.wrapRootElement = ({ element }) => (
<StyleSheetManager sheet={sheet.instance}>{element}</StyleSheetManager>
)
exports.wrapRootElement = ({ element, pathname }) => {
const sheet = (sheetByPathname[pathname] = new ServerStyleSheet())
return <StyleSheetManager sheet={sheet.instance}>{element}</StyleSheetManager>
}

exports.onRenderBody = ({ setHeadComponents, pathname }) => {
if (!sheetByPathname[pathname]) {
sheetByPathname[pathname] = new ServerStyleSheet()
}

exports.onRenderBody = ({ setHeadComponents }) => {
setHeadComponents([sheet.getStyleElement()])
setHeadComponents([sheetByPathname[pathname].getStyleElement()])
}

0 comments on commit a9f4b23

Please sign in to comment.