-
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.
fix(gatsby-plugin-manifest): remove fs from ssr (#24097)
- Loading branch information
Showing
3 changed files
with
88 additions
and
88 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,91 @@ | ||
import React from "react" | ||
import * as React from "react" | ||
import { withPrefix as fallbackWithPrefix, withAssetPrefix } from "gatsby" | ||
import fs from "fs" | ||
import { createContentDigest } from "gatsby-core-utils" | ||
import { defaultIcons, addDigestToPath, favicons } from "./common.js" | ||
import getManifestForPathname from "./get-manifest-pathname" | ||
|
||
// TODO: remove for v3 | ||
const withPrefix = withAssetPrefix || fallbackWithPrefix | ||
|
||
let iconDigest = null | ||
|
||
exports.onRenderBody = ( | ||
{ setHeadComponents, pathname = `/` }, | ||
{ localize, ...pluginOptions } | ||
{ | ||
localize, | ||
legacy, | ||
cache_busting_mode: cacheBusting, | ||
cacheDigest, | ||
icon, | ||
icons: pluginIcons, | ||
include_favicon: insertFaviconLinkTag, | ||
theme_color_in_head: insertMetaTag, | ||
theme_color, | ||
crossOrigin, | ||
} | ||
) => { | ||
// We use this to build a final array to pass as the argument to setHeadComponents at the end of onRenderBody. | ||
let headComponents = [] | ||
|
||
const srcIconExists = !!pluginOptions.icon | ||
const headComponents = [] | ||
|
||
const icons = pluginOptions.icons || defaultIcons | ||
const legacy = | ||
typeof pluginOptions.legacy !== `undefined` ? pluginOptions.legacy : true | ||
|
||
const cacheBusting = | ||
typeof pluginOptions.cache_busting_mode !== `undefined` | ||
? pluginOptions.cache_busting_mode | ||
: `query` | ||
const srcIconExists = !!icon | ||
const icons = pluginIcons || defaultIcons | ||
const manifestFileName = getManifestForPathname(pathname, localize) | ||
|
||
// If icons were generated, also add a favicon link. | ||
if (srcIconExists) { | ||
if (cacheBusting !== `none`) { | ||
iconDigest = createContentDigest(fs.readFileSync(pluginOptions.icon)) | ||
} | ||
|
||
const insertFaviconLinkTag = | ||
typeof pluginOptions.include_favicon !== `undefined` | ||
? pluginOptions.include_favicon | ||
: true | ||
|
||
if (insertFaviconLinkTag) { | ||
favicons.forEach(favicon => { | ||
headComponents.push( | ||
<link | ||
key={`gatsby-plugin-manifest-icon-link`} | ||
rel="icon" | ||
href={withPrefix( | ||
addDigestToPath(favicon.src, iconDigest, cacheBusting) | ||
addDigestToPath(favicon.src, cacheDigest, cacheBusting) | ||
)} | ||
/> | ||
) | ||
}) | ||
} | ||
} | ||
|
||
const manifestFileName = getManifestForPathname(pathname, localize) | ||
|
||
// Add manifest link tag. | ||
headComponents.push( | ||
<link | ||
key={`gatsby-plugin-manifest-link`} | ||
rel="manifest" | ||
href={withPrefix(`/${manifestFileName}`)} | ||
crossOrigin={pluginOptions.crossOrigin} | ||
crossOrigin={crossOrigin} | ||
/> | ||
) | ||
|
||
// The user has an option to opt out of the theme_color meta tag being inserted into the head. | ||
if (pluginOptions.theme_color) { | ||
const insertMetaTag = | ||
typeof pluginOptions.theme_color_in_head !== `undefined` | ||
? pluginOptions.theme_color_in_head | ||
: true | ||
if (theme_color && insertMetaTag) { | ||
headComponents.push( | ||
<meta | ||
key={`gatsby-plugin-manifest-meta`} | ||
name="theme-color" | ||
content={theme_color} | ||
/> | ||
) | ||
} | ||
|
||
if (insertMetaTag) { | ||
if (legacy) { | ||
icons.forEach(icon => { | ||
headComponents.push( | ||
<meta | ||
key={`gatsby-plugin-manifest-meta`} | ||
name="theme-color" | ||
content={pluginOptions.theme_color} | ||
<link | ||
key={`gatsby-plugin-manifest-apple-touch-icon-${icon.sizes}`} | ||
rel="apple-touch-icon" | ||
sizes={icon.sizes} | ||
href={withPrefix( | ||
addDigestToPath( | ||
icon.src, | ||
cacheDigest, | ||
srcIconExists ? cacheBusting : `none` | ||
) | ||
)} | ||
/> | ||
) | ||
} | ||
} | ||
|
||
if (legacy) { | ||
const iconLinkTags = icons.map(icon => ( | ||
<link | ||
key={`gatsby-plugin-manifest-apple-touch-icon-${icon.sizes}`} | ||
rel="apple-touch-icon" | ||
sizes={icon.sizes} | ||
href={withPrefix( | ||
addDigestToPath( | ||
icon.src, | ||
iconDigest, | ||
srcIconExists ? cacheBusting : `none` | ||
) | ||
)} | ||
/> | ||
)) | ||
|
||
headComponents = [...headComponents, ...iconLinkTags] | ||
}) | ||
} | ||
|
||
setHeadComponents(headComponents) | ||
|
||
return true | ||
} |