-
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-manifest): Update manifest on navigation (#17426)
With the addition of the localize option in gatsby-plugin-manifest (#13471), multiple manifests can be generated and served depending on the path of the URL. While the correct manifest is served on initial page load, the link to the manifest is never updated even if the user navigates to a page that should include a different manifest. Co-authored-by: Ward Peeters <[email protected]>
- Loading branch information
Showing
6 changed files
with
142 additions
and
24 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
packages/gatsby-plugin-manifest/src/__tests__/gatsby-browser.js
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,73 @@ | ||
describe(`gatsby-plugin-manifest`, () => { | ||
const pluginOptions = { | ||
name: `My Website`, | ||
start_url: `/`, | ||
localize: [ | ||
{ | ||
start_url: `/es/`, | ||
lang: `es`, | ||
}, | ||
], | ||
} | ||
let onRouteUpdate | ||
|
||
beforeEach(() => { | ||
global.__PATH_PREFIX__ = `` | ||
global.__MANIFEST_PLUGIN_HAS_LOCALISATION__ = true | ||
onRouteUpdate = require(`../gatsby-browser`).onRouteUpdate | ||
document.head.innerHTML = `<link rel="manifest" href="/manifest.webmanifest">` | ||
}) | ||
|
||
afterAll(() => { | ||
delete global.__MANIFEST_PLUGIN_HAS_LOCALISATION__ | ||
}) | ||
|
||
test(`has manifest in head`, () => { | ||
const location = { | ||
pathname: `/`, | ||
} | ||
onRouteUpdate({ location }, pluginOptions) | ||
expect(document.head).toMatchInlineSnapshot(` | ||
<head> | ||
<link | ||
href="/manifest.webmanifest" | ||
rel="manifest" | ||
/> | ||
</head> | ||
`) | ||
}) | ||
|
||
test(`changes href of manifest if navigating to a localized app`, () => { | ||
const location = { | ||
pathname: `/es/`, | ||
} | ||
// add default lang | ||
pluginOptions.lang = `en` | ||
onRouteUpdate({ location }, pluginOptions) | ||
expect(document.head).toMatchInlineSnapshot(` | ||
<head> | ||
<link | ||
href="/manifest_es.webmanifest" | ||
rel="manifest" | ||
/> | ||
</head> | ||
`) | ||
}) | ||
|
||
test(`keeps default manifest if not navigating to a localized app`, () => { | ||
const location = { | ||
pathname: `/random-path/`, | ||
} | ||
// add default lang | ||
pluginOptions.lang = `en` | ||
onRouteUpdate({ location }, pluginOptions) | ||
expect(document.head).toMatchInlineSnapshot(` | ||
<head> | ||
<link | ||
href="/manifest.webmanifest" | ||
rel="manifest" | ||
/> | ||
</head> | ||
`) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* global __MANIFEST_PLUGIN_HAS_LOCALISATION__ */ | ||
import { withPrefix as fallbackWithPrefix, withAssetPrefix } from "gatsby" | ||
import getManifestForPathname from "./get-manifest-pathname" | ||
|
||
// when we don't have localisation in our manifest, we tree shake everything away | ||
if (__MANIFEST_PLUGIN_HAS_LOCALISATION__) { | ||
const withPrefix = withAssetPrefix || fallbackWithPrefix | ||
|
||
exports.onRouteUpdate = function({ location }, pluginOptions) { | ||
const { localize } = pluginOptions | ||
const manifestFilename = getManifestForPathname(location.pathname, localize) | ||
|
||
const manifestEl = document.head.querySelector(`link[rel="manifest"]`) | ||
if (manifestEl) { | ||
manifestEl.setAttribute(`href`, withPrefix(manifestFilename)) | ||
} | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
packages/gatsby-plugin-manifest/src/get-manifest-pathname.js
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,23 @@ | ||
/** | ||
* Get a manifest filename depending on localized pathname | ||
* | ||
* @param {string} pathname | ||
* @param {Array<{start_url: string, lang: string}>} localizedManifests | ||
* @return string | ||
*/ | ||
export default (pathname, localizedManifests) => { | ||
const defaultFilename = `manifest.webmanifest` | ||
if (!Array.isArray(localizedManifests)) { | ||
return defaultFilename | ||
} | ||
|
||
const localizedManifest = localizedManifests.find(app => | ||
pathname.startsWith(app.start_url) | ||
) | ||
|
||
if (!localizedManifest) { | ||
return defaultFilename | ||
} | ||
|
||
return `manifest_${localizedManifest.lang}.webmanifest` | ||
} |