Skip to content

Commit 1af42bc

Browse files
gurpreet-hanjrawardpeet
authored andcommitted
feat(gatsby-plugin-manifest): make favicon link tag optional (#11414)
Related issue - #11308
1 parent b158332 commit 1af42bc

File tree

4 files changed

+207
-4
lines changed

4 files changed

+207
-4
lines changed

packages/gatsby-plugin-manifest/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ module.exports = {
5050
// see https://developers.google.com/web/fundamentals/web-app-manifest/#display
5151
display: `standalone`,
5252
icon: `src/images/icon.png`, // This path is relative to the root of the site.
53+
include_favicon: true, // Include favicon
5354
},
5455
},
5556
],
@@ -231,3 +232,28 @@ module.exports = {
231232
],
232233
}
233234
```
235+
236+
## Exclude `favicon` link tag
237+
238+
Excludes `<link rel="shortcut icon" href="/favicon.png" />` link tag to html output. You can set `include_favicon` plugin option to `false` to opt-out of this behaviour.
239+
240+
```javascript:title=gatsby-config.js
241+
module.exports = {
242+
plugins: [
243+
{
244+
resolve: `gatsby-plugin-manifest`,
245+
options: {
246+
name: `GatsbyJS`,
247+
short_name: `GatsbyJS`,
248+
start_url: `/`,
249+
background_color: `#f7f0eb`,
250+
theme_color: `#a2466c`,
251+
display: `standalone`,
252+
icon: `src/images/icon.png`, // This path is relative to the root of the site.
253+
theme_color_in_head: false, // This will avoid adding theme-color meta tag.
254+
include_favicon: false, // This will exclude favicon link tag
255+
},
256+
},
257+
],
258+
}
259+
```

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

+155
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,112 @@ Array [
163163
]
164164
`;
165165

166+
exports[`gatsby-plugin-manifest Adds link favicon if "include_favicon" option is not provided 1`] = `
167+
Array [
168+
<link
169+
href="/icons/icon-48x48.png"
170+
rel="shortcut icon"
171+
/>,
172+
<link
173+
href="/manifest.webmanifest"
174+
rel="manifest"
175+
/>,
176+
<link
177+
href="/icons/icon-48x48.png"
178+
rel="apple-touch-icon"
179+
sizes="48x48"
180+
/>,
181+
<link
182+
href="/icons/icon-72x72.png"
183+
rel="apple-touch-icon"
184+
sizes="72x72"
185+
/>,
186+
<link
187+
href="/icons/icon-96x96.png"
188+
rel="apple-touch-icon"
189+
sizes="96x96"
190+
/>,
191+
<link
192+
href="/icons/icon-144x144.png"
193+
rel="apple-touch-icon"
194+
sizes="144x144"
195+
/>,
196+
<link
197+
href="/icons/icon-192x192.png"
198+
rel="apple-touch-icon"
199+
sizes="192x192"
200+
/>,
201+
<link
202+
href="/icons/icon-256x256.png"
203+
rel="apple-touch-icon"
204+
sizes="256x256"
205+
/>,
206+
<link
207+
href="/icons/icon-384x384.png"
208+
rel="apple-touch-icon"
209+
sizes="384x384"
210+
/>,
211+
<link
212+
href="/icons/icon-512x512.png"
213+
rel="apple-touch-icon"
214+
sizes="512x512"
215+
/>,
216+
]
217+
`;
218+
219+
exports[`gatsby-plugin-manifest Adds link favicon tag if "include_favicon" is set to true 1`] = `
220+
Array [
221+
<link
222+
href="/icons/icon-48x48.png"
223+
rel="shortcut icon"
224+
/>,
225+
<link
226+
href="/manifest.webmanifest"
227+
rel="manifest"
228+
/>,
229+
<link
230+
href="/icons/icon-48x48.png"
231+
rel="apple-touch-icon"
232+
sizes="48x48"
233+
/>,
234+
<link
235+
href="/icons/icon-72x72.png"
236+
rel="apple-touch-icon"
237+
sizes="72x72"
238+
/>,
239+
<link
240+
href="/icons/icon-96x96.png"
241+
rel="apple-touch-icon"
242+
sizes="96x96"
243+
/>,
244+
<link
245+
href="/icons/icon-144x144.png"
246+
rel="apple-touch-icon"
247+
sizes="144x144"
248+
/>,
249+
<link
250+
href="/icons/icon-192x192.png"
251+
rel="apple-touch-icon"
252+
sizes="192x192"
253+
/>,
254+
<link
255+
href="/icons/icon-256x256.png"
256+
rel="apple-touch-icon"
257+
sizes="256x256"
258+
/>,
259+
<link
260+
href="/icons/icon-384x384.png"
261+
rel="apple-touch-icon"
262+
sizes="384x384"
263+
/>,
264+
<link
265+
href="/icons/icon-512x512.png"
266+
rel="apple-touch-icon"
267+
sizes="512x512"
268+
/>,
269+
]
270+
`;
271+
166272
exports[`gatsby-plugin-manifest Creates legacy apple touch links Using default set of icons 1`] = `
167273
Array [
168274
<link
@@ -349,6 +455,55 @@ Array [
349455
]
350456
`;
351457

458+
exports[`gatsby-plugin-manifest Does not add a link favicon if "include_favicon" option is set to false 1`] = `
459+
Array [
460+
<link
461+
href="/manifest.webmanifest"
462+
rel="manifest"
463+
/>,
464+
<link
465+
href="/icons/icon-48x48.png"
466+
rel="apple-touch-icon"
467+
sizes="48x48"
468+
/>,
469+
<link
470+
href="/icons/icon-72x72.png"
471+
rel="apple-touch-icon"
472+
sizes="72x72"
473+
/>,
474+
<link
475+
href="/icons/icon-96x96.png"
476+
rel="apple-touch-icon"
477+
sizes="96x96"
478+
/>,
479+
<link
480+
href="/icons/icon-144x144.png"
481+
rel="apple-touch-icon"
482+
sizes="144x144"
483+
/>,
484+
<link
485+
href="/icons/icon-192x192.png"
486+
rel="apple-touch-icon"
487+
sizes="192x192"
488+
/>,
489+
<link
490+
href="/icons/icon-256x256.png"
491+
rel="apple-touch-icon"
492+
sizes="256x256"
493+
/>,
494+
<link
495+
href="/icons/icon-384x384.png"
496+
rel="apple-touch-icon"
497+
sizes="384x384"
498+
/>,
499+
<link
500+
href="/icons/icon-512x512.png"
501+
rel="apple-touch-icon"
502+
sizes="512x512"
503+
/>,
504+
]
505+
`;
506+
352507
exports[`gatsby-plugin-manifest Does not create legacy apple touch links If "legacy" options is false and using default set of icons 1`] = `
353508
Array [
354509
<link

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

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// const { shallow } = require(`enzyme`)
21
const { onRenderBody } = require(`../gatsby-ssr`)
32

43
let headComponents
@@ -36,7 +35,25 @@ describe(`gatsby-plugin-manifest`, () => {
3635
})
3736

3837
it(`Adds "shortcut icon" and "manifest" links and "theme_color" meta tag to head`, () => {
39-
onRenderBody(ssrArgs, { icon: true, theme_color: `#000000` })
38+
onRenderBody(ssrArgs, {
39+
icon: true,
40+
theme_color: `#000000`,
41+
})
42+
expect(headComponents).toMatchSnapshot()
43+
})
44+
45+
it(`Adds link favicon tag if "include_favicon" is set to true`, () => {
46+
onRenderBody(ssrArgs, { icon: true, include_favicon: true })
47+
expect(headComponents).toMatchSnapshot()
48+
})
49+
50+
it(`Adds link favicon if "include_favicon" option is not provided`, () => {
51+
onRenderBody(ssrArgs, { icon: true })
52+
expect(headComponents).toMatchSnapshot()
53+
})
54+
55+
it(`Does not add a link favicon if "include_favicon" option is set to false`, () => {
56+
onRenderBody(ssrArgs, { icon: true, include_favicon: false })
4057
expect(headComponents).toMatchSnapshot()
4158
})
4259

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ exports.onRenderBody = ({ setHeadComponents }, pluginOptions) => {
1010
const legacy =
1111
typeof pluginOptions.legacy !== `undefined` ? pluginOptions.legacy : true
1212

13-
// If icons were generated, also add a favicon link.
13+
// The user has an option to opt out of the favicon link tag being inserted into the head.
1414
if (pluginOptions.icon) {
1515
let favicon = icons && icons.length ? icons[0].src : null
1616

17-
if (favicon) {
17+
const insertFaviconLinkTag =
18+
typeof pluginOptions.include_favicon !== `undefined`
19+
? pluginOptions.include_favicon
20+
: true
21+
22+
if (favicon && insertFaviconLinkTag) {
1823
headComponents.push(
1924
<link
2025
key={`gatsby-plugin-manifest-icon-link`}

0 commit comments

Comments
 (0)