-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby-plugin-typography): hot reloading for styles and Google F…
…onts (#10545)
- Loading branch information
Showing
2 changed files
with
47 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Hot reload gatsby-plugin-typography in development | ||
|
||
let ReactDOMServer = null | ||
let React = null | ||
let GoogleFont = null | ||
let typography = null | ||
|
||
if (process.env.BUILD_STAGE === `develop`) { | ||
ReactDOMServer = require(`react-dom/server`) | ||
React = require(`react`) | ||
GoogleFont = require(`react-typography`).GoogleFont | ||
// typography links to the file set in "pathToConfigModule" | ||
typography = require(`./.cache/typography.js`).default | ||
|
||
exports.onClientEntry = () => { | ||
// Inject the CSS Styles | ||
typography.injectStyles() | ||
|
||
// Hot reload Google CDN links | ||
if (typography.options.googleFonts.length > 0) { | ||
if (typeof document !== `undefined`) { | ||
// Construct the <link /> tag | ||
const googleFonts = ReactDOMServer.renderToStaticMarkup( | ||
<GoogleFont key={`GoogleFont`} typography={typography} /> | ||
) | ||
// Parse the tag | ||
let doc = new DOMParser().parseFromString(googleFonts, `text/html`) | ||
let linkElement = doc.head.firstChild | ||
// Add custom identifier | ||
linkElement.setAttribute(`data-gatsby-typography`, `true`) | ||
const head = document.getElementsByTagName(`head`)[0] | ||
const elem = document.querySelector(`[data-gatsby-typography]`) | ||
// Remove old <link /> tag | ||
if (elem) { | ||
elem.remove() | ||
} | ||
head.appendChild(linkElement) | ||
} | ||
} | ||
} | ||
} |
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