Skip to content

Commit 1a16600

Browse files
yogeshkotadiyasidharthachatterjee
authored andcommitted
feat(gatsby-plugin-manifest): add option for crossorigin in manifest (#11953)
* Add crossorigin option to gatsby-plugin-manifest * Updated docs for add crossorigin option in manifest * Merged requested chages * Changed docs as per request and added a snapshot test * Updated docs and Readme
1 parent 307820a commit 1a16600

File tree

6 files changed

+92
-0
lines changed

6 files changed

+92
-0
lines changed

docs/docs/add-a-manifest-file.md

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ npm install --save gatsby-plugin-manifest
4141
// see https://developers.google.com/web/fundamentals/web-app-manifest/#display
4242
display: "standalone",
4343
icon: "src/images/icon.png", // This path is relative to the root of the site.
44+
// An optional attribute which provides support for CORS check.
45+
// If you do not provide a crossOrigin option, it will skip CORS for manifest.
46+
// Any invalid keyword or empty string defaults to `anonymous`
47+
crossOrigin: `use-credentials`,
4448
},
4549
},
4650
]

packages/gatsby-plugin-manifest/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,33 @@ module.exports = {
257257
],
258258
}
259259
```
260+
261+
## Enable CORS using `crossorigin` attribute
262+
263+
Add a `crossorigin` attribute to the manifest `<link rel="manifest" crossorigin="use-credentials" href="/manifest.webmanifest" />` link tag.
264+
265+
You can set `crossOrigin` plugin option to `'use-credentials'` to enable sharing resources via cookies. Any invalid keyword or empty string will fallback to `'anonymous'`.
266+
267+
You can find more information about `crossorigin` on MDN.
268+
269+
[https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_settings_attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_settings_attributes)
270+
271+
```javascript:title=gatsby-config.js
272+
module.exports = {
273+
plugins: [
274+
{
275+
resolve: `gatsby-plugin-manifest`,
276+
options: {
277+
name: `GatsbyJS`,
278+
short_name: `GatsbyJS`,
279+
start_url: `/`,
280+
background_color: `#f7f0eb`,
281+
theme_color: `#a2466c`,
282+
display: `standalone`,
283+
icon: `src/images/icon.png`, // This path is relative to the root of the site.
284+
crossOrigin: `use-credentials`,
285+
},
286+
},
287+
],
288+
}
289+
```

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

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

166+
exports[`gatsby-plugin-manifest Adds a crossorigin attribute to manifest link tag if provided 1`] = `
167+
Array [
168+
<link
169+
crossOrigin="use-credentials"
170+
href="/manifest.webmanifest"
171+
rel="manifest"
172+
/>,
173+
<link
174+
href="/icons/icon-48x48.png"
175+
rel="apple-touch-icon"
176+
sizes="48x48"
177+
/>,
178+
<link
179+
href="/icons/icon-72x72.png"
180+
rel="apple-touch-icon"
181+
sizes="72x72"
182+
/>,
183+
<link
184+
href="/icons/icon-96x96.png"
185+
rel="apple-touch-icon"
186+
sizes="96x96"
187+
/>,
188+
<link
189+
href="/icons/icon-144x144.png"
190+
rel="apple-touch-icon"
191+
sizes="144x144"
192+
/>,
193+
<link
194+
href="/icons/icon-192x192.png"
195+
rel="apple-touch-icon"
196+
sizes="192x192"
197+
/>,
198+
<link
199+
href="/icons/icon-256x256.png"
200+
rel="apple-touch-icon"
201+
sizes="256x256"
202+
/>,
203+
<link
204+
href="/icons/icon-384x384.png"
205+
rel="apple-touch-icon"
206+
sizes="384x384"
207+
/>,
208+
<link
209+
href="/icons/icon-512x512.png"
210+
rel="apple-touch-icon"
211+
sizes="512x512"
212+
/>,
213+
]
214+
`;
215+
166216
exports[`gatsby-plugin-manifest Adds link favicon if "include_favicon" option is not provided 1`] = `
167217
Array [
168218
<link

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

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ describe(`gatsby-plugin-manifest`, () => {
1313
headComponents = []
1414
})
1515

16+
it(`Adds a crossorigin attribute to manifest link tag if provided`, () => {
17+
onRenderBody(ssrArgs, { crossOrigin: `use-credentials` })
18+
expect(headComponents).toMatchSnapshot()
19+
})
20+
1621
it(`Adds a "theme color" meta tag to head if "theme_color_in_head" is not provided`, () => {
1722
onRenderBody(ssrArgs, { theme_color: `#000000` })
1823
expect(headComponents).toMatchSnapshot()

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

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ exports.onPostBootstrap = (args, pluginOptions) =>
3232
delete manifest.plugins
3333
delete manifest.legacy
3434
delete manifest.theme_color_in_head
35+
delete manifest.crossOrigin
3536

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

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

+2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ exports.onRenderBody = ({ setHeadComponents }, pluginOptions) => {
3636
key={`gatsby-plugin-manifest-link`}
3737
rel="manifest"
3838
href={withPrefix(`/manifest.webmanifest`)}
39+
crossOrigin={pluginOptions.crossOrigin}
3940
/>
4041
)
42+
4143
// The user has an option to opt out of the theme_color meta tag being inserted into the head.
4244
if (pluginOptions.theme_color) {
4345
let insertMetaTag = Object.keys(pluginOptions).includes(

0 commit comments

Comments
 (0)