Skip to content

Commit 129c5d8

Browse files
ivorpadpieh
authored andcommitted
feat(gatsby-plugin-manifest): add option to remove the "theme color" meta tag (#10440)
1 parent 16b7026 commit 129c5d8

File tree

6 files changed

+97
-7
lines changed

6 files changed

+97
-7
lines changed

packages/gatsby-plugin-manifest/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,27 @@ module.exports = {
207207
],
208208
}
209209
```
210+
211+
## Removing `theme-color` meta tag
212+
213+
By default `gatsby-plugin-manifest` inserts `<meta content={theme_color} name="theme-color" />` tag to html output. This can be problematic if you want to programatically control that tag - for example when implementing light/dark themes in your project. You can set `theme_color_in_head` plugin option to `false` to opt-out of this behaviour.
214+
215+
```javascript:title=gatsby-config.js
216+
module.exports = {
217+
plugins: [
218+
{
219+
resolve: `gatsby-plugin-manifest`,
220+
options: {
221+
name: `GatsbyJS`,
222+
short_name: `GatsbyJS`,
223+
start_url: `/`,
224+
background_color: `#f7f0eb`,
225+
theme_color: `#a2466c`,
226+
display: `standalone`,
227+
icon: `src/images/icon.png`, // This path is relative to the root of the site.
228+
theme_color_in_head: false, // This will avoid adding theme-color meta tag.
229+
},
230+
},
231+
],
232+
}
233+
```

packages/gatsby-plugin-manifest/src/__tests__/__snapshots__/gatsby-ssr.js.snap

+35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`gatsby-plugin-manifest Add a "theme color" meta tag if "theme_color_in_head" is set to true 1`] = `
4+
Array [
5+
<link
6+
href="/manifest.webmanifest"
7+
rel="manifest"
8+
/>,
9+
<meta
10+
content="#000000"
11+
name="theme-color"
12+
/>,
13+
]
14+
`;
15+
316
exports[`gatsby-plugin-manifest Adds "shortcut icon" and "manifest" links and "theme_color" meta tag to head 1`] = `
417
Array [
518
<link
@@ -17,6 +30,19 @@ Array [
1730
]
1831
`;
1932

33+
exports[`gatsby-plugin-manifest Adds a "theme color" meta tag to head if "theme_color_in_head" is not provided 1`] = `
34+
Array [
35+
<link
36+
href="/manifest.webmanifest"
37+
rel="manifest"
38+
/>,
39+
<meta
40+
content="#000000"
41+
name="theme-color"
42+
/>,
43+
]
44+
`;
45+
2046
exports[`gatsby-plugin-manifest Creates legacy apple touch links if opted in Using default set of icons 1`] = `
2147
Array [
2248
<link
@@ -101,6 +127,15 @@ Array [
101127
]
102128
`;
103129

130+
exports[`gatsby-plugin-manifest Does not add a "theme color" meta tag if "theme_color_in_head" is set to false 1`] = `
131+
Array [
132+
<link
133+
href="/manifest.webmanifest"
134+
rel="manifest"
135+
/>,
136+
]
137+
`;
138+
104139
exports[`gatsby-plugin-manifest Does not add a "theme_color" meta tag to head if "theme_color" option is not provided or is an empty string 1`] = `
105140
Array [
106141
<link

packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ describe(`Test plugin manifest options`, () => {
7171
icon: undefined,
7272
legacy: true,
7373
plugins: [],
74+
theme_color_in_head: false,
7475
}
7576
await onPostBootstrap([], {
7677
...manifestOptions,

packages/gatsby-plugin-manifest/src/__tests__/gatsby-ssr.js

+21
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,27 @@ describe(`gatsby-plugin-manifest`, () => {
1414
headComponents = []
1515
})
1616

17+
it(`Adds a "theme color" meta tag to head if "theme_color_in_head" is not provided`, () => {
18+
onRenderBody(ssrArgs, { theme_color: `#000000` })
19+
expect(headComponents).toMatchSnapshot()
20+
})
21+
22+
it(`Does not add a "theme color" meta tag if "theme_color_in_head" is set to false`, () => {
23+
onRenderBody(ssrArgs, {
24+
theme_color: `#000000`,
25+
theme_color_in_head: false,
26+
})
27+
expect(headComponents).toMatchSnapshot()
28+
})
29+
30+
it(`Add a "theme color" meta tag if "theme_color_in_head" is set to true`, () => {
31+
onRenderBody(ssrArgs, {
32+
theme_color: `#000000`,
33+
theme_color_in_head: true,
34+
})
35+
expect(headComponents).toMatchSnapshot()
36+
})
37+
1738
it(`Adds "shortcut icon" and "manifest" links and "theme_color" meta tag to head`, () => {
1839
onRenderBody(ssrArgs, { icon: true, theme_color: `#000000` })
1940
expect(headComponents).toMatchSnapshot()

packages/gatsby-plugin-manifest/src/gatsby-node.js

+2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ exports.onPostBootstrap = (args, pluginOptions) =>
2323
const { icon, ...manifest } = pluginOptions
2424

2525
// Delete options we won't pass to the manifest.webmanifest.
26+
2627
delete manifest.plugins
2728
delete manifest.legacy
29+
delete manifest.theme_color_in_head
2830

2931
// If icons are not manually defined, use the default icon set.
3032
if (!manifest.icons) {

packages/gatsby-plugin-manifest/src/gatsby-ssr.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,23 @@ exports.onRenderBody = ({ setHeadComponents }, pluginOptions) => {
3131
href={withPrefix(`/manifest.webmanifest`)}
3232
/>
3333
)
34-
3534
// The user has an option to opt out of the theme_color meta tag being inserted into the head.
3635
if (pluginOptions.theme_color) {
37-
headComponents.push(
38-
<meta
39-
key={`gatsby-plugin-manifest-meta`}
40-
name="theme-color"
41-
content={pluginOptions.theme_color}
42-
/>
36+
let insertMetaTag = Object.keys(pluginOptions).includes(
37+
`theme_color_in_head`
4338
)
39+
? pluginOptions.theme_color_in_head
40+
: true
41+
42+
if (insertMetaTag) {
43+
headComponents.push(
44+
<meta
45+
key={`gatsby-plugin-manifest-meta`}
46+
name="theme-color"
47+
content={pluginOptions.theme_color}
48+
/>
49+
)
50+
}
4451
}
4552

4653
if (pluginOptions.legacy) {

0 commit comments

Comments
 (0)