Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gatsby-plugin-jss): use separate SheetsRegistry for each page (#9401
) <!-- Q. Which branch should I use for my pull request? A. Use `master` branch (probably). Q. Which branch if my change is a bug fix for Gatsby v1? A. In this case, you should use the `v1` branch Q. Which branch if I'm still not sure? A. Use `master` branch. Ask in the PR if you're not sure and a Gatsby maintainer will be happy to help :) Note: We will only accept bug fixes for Gatsby v1. New features should be added to Gatsby v2. Learn more about contributing: https://www.gatsbyjs.org/docs/how-to-contribute/ --> Not sure about the branch for this one. Just a small patch based off of cpboyd's solution for this issue: #7716 closes #7716
- Loading branch information
Showing
3 changed files
with
31 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,34 @@ | ||
import React from "react" | ||
import { JssProvider, SheetsRegistry, ThemeProvider } from "react-jss" | ||
|
||
const sheets = new SheetsRegistry() | ||
/** | ||
* Keep track of SheetRegistry for each page | ||
*/ | ||
const sheetsRegistryManager = new Map() | ||
|
||
// eslint-disable-next-line react/prop-types,react/display-name | ||
exports.wrapRootElement = ({ element }, { theme = {} }) => ( | ||
<JssProvider registry={sheets}> | ||
<ThemeProvider theme={theme}>{element}</ThemeProvider> | ||
</JssProvider> | ||
) | ||
exports.wrapRootElement = ({ element, pathname }, { theme = {} }) => { | ||
const sheets = new SheetsRegistry() | ||
sheetsRegistryManager.set(pathname, sheets) | ||
|
||
exports.onRenderBody = ({ setHeadComponents }) => { | ||
setHeadComponents([ | ||
<style | ||
type="text/css" | ||
id="server-side-jss" | ||
key="server-side-jss" | ||
dangerouslySetInnerHTML={{ __html: sheets.toString() }} | ||
/>, | ||
]) | ||
return ( | ||
<JssProvider registry={sheets}> | ||
<ThemeProvider theme={theme}>{element}</ThemeProvider> | ||
</JssProvider> | ||
) | ||
} | ||
|
||
exports.onRenderBody = ({ setHeadComponents, pathname }) => { | ||
const sheets = sheetsRegistryManager.get(pathname) | ||
if (sheets) { | ||
setHeadComponents([ | ||
<style | ||
type="text/css" | ||
id="server-side-jss" | ||
key="server-side-jss" | ||
dangerouslySetInnerHTML={{ __html: sheets.toString() }} | ||
/>, | ||
]) | ||
sheetsRegistryManager.delete(pathname) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters