From 59067b380c5d5921e791e19ac0e22f1122367078 Mon Sep 17 00:00:00 2001 From: Alex Moon Date: Mon, 16 Dec 2019 03:04:07 +0000 Subject: [PATCH 1/5] fix: fixes icons not getting asset prefix #18497 --- .../__snapshots__/gatsby-node.js.snap | 21 ++++++++++++++-- .../src/__tests__/gatsby-node.js | 25 +++++++++++++------ .../gatsby-plugin-manifest/src/gatsby-node.js | 9 +++++++ 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/packages/gatsby-plugin-manifest/src/__tests__/__snapshots__/gatsby-node.js.snap b/packages/gatsby-plugin-manifest/src/__tests__/__snapshots__/gatsby-node.js.snap index 783940566560d..3099cf7efce0f 100644 --- a/packages/gatsby-plugin-manifest/src/__tests__/__snapshots__/gatsby-node.js.snap +++ b/packages/gatsby-plugin-manifest/src/__tests__/__snapshots__/gatsby-node.js.snap @@ -1,13 +1,30 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Test plugin manifest options correctly works with default parameters 1`] = `"{\\"name\\":\\"GatsbyJS\\",\\"short_name\\":\\"GatsbyJS\\",\\"start_url\\":\\"/\\",\\"background_color\\":\\"#f7f0eb\\",\\"theme_color\\":\\"#a2466c\\",\\"display\\":\\"standalone\\",\\"icons\\":[{\\"src\\":\\"icons/icon-48x48.png\\",\\"sizes\\":\\"48x48\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"icons/icon-72x72.png\\",\\"sizes\\":\\"72x72\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"icons/icon-96x96.png\\",\\"sizes\\":\\"96x96\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"icons/icon-144x144.png\\",\\"sizes\\":\\"144x144\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"icons/icon-192x192.png\\",\\"sizes\\":\\"192x192\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"icons/icon-256x256.png\\",\\"sizes\\":\\"256x256\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"icons/icon-384x384.png\\",\\"sizes\\":\\"384x384\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"icons/icon-512x512.png\\",\\"sizes\\":\\"512x512\\",\\"type\\":\\"image/png\\"}]}"`; +exports[`Test plugin manifest options correctly works with default parameters 1`] = `"{\\"name\\":\\"GatsbyJS\\",\\"short_name\\":\\"GatsbyJS\\",\\"start_url\\":\\"/\\",\\"background_color\\":\\"#f7f0eb\\",\\"theme_color\\":\\"#a2466c\\",\\"display\\":\\"standalone\\",\\"icons\\":[{\\"src\\":\\"/icons/icon-48x48.png\\",\\"sizes\\":\\"48x48\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"/icons/icon-72x72.png\\",\\"sizes\\":\\"72x72\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"/icons/icon-96x96.png\\",\\"sizes\\":\\"96x96\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"/icons/icon-144x144.png\\",\\"sizes\\":\\"144x144\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"/icons/icon-192x192.png\\",\\"sizes\\":\\"192x192\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"/icons/icon-256x256.png\\",\\"sizes\\":\\"256x256\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"/icons/icon-384x384.png\\",\\"sizes\\":\\"384x384\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"/icons/icon-512x512.png\\",\\"sizes\\":\\"512x512\\",\\"type\\":\\"image/png\\"}]}"`; exports[`Test plugin manifest options does file name based cache busting 1`] = ` [MockFunction] { "calls": Array [ Array [ "public/manifest.webmanifest", - "{\\"name\\":\\"GatsbyJS\\",\\"short_name\\":\\"GatsbyJS\\",\\"start_url\\":\\"/\\",\\"background_color\\":\\"#f7f0eb\\",\\"theme_color\\":\\"#a2466c\\",\\"display\\":\\"standalone\\",\\"icons\\":[{\\"src\\":\\"icons/icon-48x48-contentDigest.png\\",\\"sizes\\":\\"48x48\\",\\"type\\":\\"image/png\\",\\"purpose\\":\\"all\\"},{\\"src\\":\\"icons/icon-128x128-contentDigest.png\\",\\"sizes\\":\\"128x128\\",\\"type\\":\\"image/png\\"}]}", + "{\\"name\\":\\"GatsbyJS\\",\\"short_name\\":\\"GatsbyJS\\",\\"start_url\\":\\"/\\",\\"background_color\\":\\"#f7f0eb\\",\\"theme_color\\":\\"#a2466c\\",\\"display\\":\\"standalone\\",\\"icons\\":[{\\"src\\":\\"/icons/icon-48x48-contentDigest.png\\",\\"sizes\\":\\"48x48\\",\\"type\\":\\"image/png\\",\\"purpose\\":\\"all\\"},{\\"src\\":\\"/icons/icon-128x128-contentDigest.png\\",\\"sizes\\":\\"128x128\\",\\"type\\":\\"image/png\\"}]}", + ], + ], + "results": Array [ + Object { + "type": "return", + "value": undefined, + }, + ], +} +`; + +exports[`Test plugin manifest options properly prefixes icons in manifest with asset or path prefix 1`] = ` +[MockFunction] { + "calls": Array [ + Array [ + "public/manifest.webmanifest", + "{\\"name\\":\\"GatsbyJS\\",\\"short_name\\":\\"GatsbyJS\\",\\"start_url\\":\\"/\\",\\"background_color\\":\\"#f7f0eb\\",\\"theme_color\\":\\"#a2466c\\",\\"display\\":\\"standalone\\",\\"icons\\":[{\\"src\\":\\"/test-prefix/icons/icon-48x48.png\\",\\"sizes\\":\\"48x48\\",\\"type\\":\\"image/png\\",\\"purpose\\":\\"all\\"},{\\"src\\":\\"/test-prefix/icons/icon-128x128.png\\",\\"sizes\\":\\"128x128\\",\\"type\\":\\"image/png\\"}]}", ], ], "results": Array [ diff --git a/packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js index 8cc3e517c3be4..5ff468abcb4b5 100644 --- a/packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js @@ -71,13 +71,13 @@ const manifestOptions = { display: `standalone`, icons: [ { - src: `icons/icon-48x48.png`, + src: `/icons/icon-48x48.png`, sizes: `48x48`, type: `image/png`, purpose: `all`, }, { - src: `icons/icon-128x128.png`, + src: `/icons/icon-128x128.png`, sizes: `128x128`, type: `image/png`, }, @@ -90,6 +90,7 @@ describe(`Test plugin manifest options`, () => { fs.mkdirSync.mockReset() fs.existsSync.mockReset() sharp.mockClear() + global.__PATH_PREFIX__ = `` }) // the require of gatsby-node performs the invoking @@ -123,12 +124,12 @@ describe(`Test plugin manifest options`, () => { const pluginSpecificOptions = { icons: [ { - src: `icons/icon-48x48.png`, + src: `/icons/icon-48x48.png`, sizes: `${size}x${size}`, type: `image/png`, }, { - src: `other-icons/icon-48x48.png`, + src: `/other-icons/icon-48x48.png`, sizes: `${size}x${size}`, type: `image/png`, }, @@ -142,11 +143,11 @@ describe(`Test plugin manifest options`, () => { const firstIconPath = path.join( `public`, - path.dirname(`icons/icon-48x48.png`) + path.dirname(`/icons/icon-48x48.png`) ) const secondIconPath = path.join( `public`, - path.dirname(`other-icons/icon-48x48.png`) + path.dirname(`/other-icons/icon-48x48.png`) ) expect(fs.mkdirSync).toHaveBeenNthCalledWith(1, firstIconPath) @@ -163,7 +164,7 @@ describe(`Test plugin manifest options`, () => { icon: icon, icons: [ { - src: `icons/icon-48x48.png`, + src: `/icons/icon-48x48.png`, sizes: `${size}x${size}`, type: `image/png`, }, @@ -257,6 +258,16 @@ describe(`Test plugin manifest options`, () => { ) }) + it(`properly prefixes icons in manifest with asset or path prefix`, async () => { + global.__PATH_PREFIX__ = `/test-prefix` + + await onPostBootstrap(apiArgs, { + ...manifestOptions, + }) + + expect(fs.writeFileSync).toMatchSnapshot() + }) + it(`icon options iterator adds options and the icon array take precedence`, async () => { fs.statSync.mockReturnValueOnce({ isFile: () => true }) diff --git a/packages/gatsby-plugin-manifest/src/gatsby-node.js b/packages/gatsby-plugin-manifest/src/gatsby-node.js index d9faf2877e38f..d7cd540b7e031 100644 --- a/packages/gatsby-plugin-manifest/src/gatsby-node.js +++ b/packages/gatsby-plugin-manifest/src/gatsby-node.js @@ -3,6 +3,10 @@ import path from "path" import sharp from "./safe-sharp" import { createContentDigest, cpuCoreCount } from "gatsby-core-utils" import { defaultIcons, doesIconExist, addDigestToPath } from "./common" +import { withPrefix as fallbackWithPrefix, withAssetPrefix } from "gatsby" + +// TODO: remove for v3 +const withPrefix = withAssetPrefix || fallbackWithPrefix sharp.simd(true) @@ -119,6 +123,11 @@ const makeManifest = async (cache, reporter, pluginOptions, shouldLocalize) => { manifest.icons = [...defaultIcons] } + //Fix #18497 by prefixing paths + manifest.icons = manifest.icons.map(icon => { + return { ...icon, src: withPrefix(icon.src) } + }) + // Specify extra options for each icon (if requested). if (pluginOptions.icon_options) { manifest.icons = manifest.icons.map(icon => { From 480eefcf8ca18536c9a0cb31010514413bd972c6 Mon Sep 17 00:00:00 2001 From: Alex Moon Date: Fri, 20 Dec 2019 22:10:09 +0800 Subject: [PATCH 2/5] Revert "fix: fixes icons not getting asset prefix #18497" This reverts commit 59067b380c5d5921e791e19ac0e22f1122367078. --- .../__snapshots__/gatsby-node.js.snap | 21 ++-------------- .../src/__tests__/gatsby-node.js | 25 ++++++------------- .../gatsby-plugin-manifest/src/gatsby-node.js | 9 ------- 3 files changed, 9 insertions(+), 46 deletions(-) diff --git a/packages/gatsby-plugin-manifest/src/__tests__/__snapshots__/gatsby-node.js.snap b/packages/gatsby-plugin-manifest/src/__tests__/__snapshots__/gatsby-node.js.snap index 3099cf7efce0f..783940566560d 100644 --- a/packages/gatsby-plugin-manifest/src/__tests__/__snapshots__/gatsby-node.js.snap +++ b/packages/gatsby-plugin-manifest/src/__tests__/__snapshots__/gatsby-node.js.snap @@ -1,30 +1,13 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Test plugin manifest options correctly works with default parameters 1`] = `"{\\"name\\":\\"GatsbyJS\\",\\"short_name\\":\\"GatsbyJS\\",\\"start_url\\":\\"/\\",\\"background_color\\":\\"#f7f0eb\\",\\"theme_color\\":\\"#a2466c\\",\\"display\\":\\"standalone\\",\\"icons\\":[{\\"src\\":\\"/icons/icon-48x48.png\\",\\"sizes\\":\\"48x48\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"/icons/icon-72x72.png\\",\\"sizes\\":\\"72x72\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"/icons/icon-96x96.png\\",\\"sizes\\":\\"96x96\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"/icons/icon-144x144.png\\",\\"sizes\\":\\"144x144\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"/icons/icon-192x192.png\\",\\"sizes\\":\\"192x192\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"/icons/icon-256x256.png\\",\\"sizes\\":\\"256x256\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"/icons/icon-384x384.png\\",\\"sizes\\":\\"384x384\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"/icons/icon-512x512.png\\",\\"sizes\\":\\"512x512\\",\\"type\\":\\"image/png\\"}]}"`; +exports[`Test plugin manifest options correctly works with default parameters 1`] = `"{\\"name\\":\\"GatsbyJS\\",\\"short_name\\":\\"GatsbyJS\\",\\"start_url\\":\\"/\\",\\"background_color\\":\\"#f7f0eb\\",\\"theme_color\\":\\"#a2466c\\",\\"display\\":\\"standalone\\",\\"icons\\":[{\\"src\\":\\"icons/icon-48x48.png\\",\\"sizes\\":\\"48x48\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"icons/icon-72x72.png\\",\\"sizes\\":\\"72x72\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"icons/icon-96x96.png\\",\\"sizes\\":\\"96x96\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"icons/icon-144x144.png\\",\\"sizes\\":\\"144x144\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"icons/icon-192x192.png\\",\\"sizes\\":\\"192x192\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"icons/icon-256x256.png\\",\\"sizes\\":\\"256x256\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"icons/icon-384x384.png\\",\\"sizes\\":\\"384x384\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"icons/icon-512x512.png\\",\\"sizes\\":\\"512x512\\",\\"type\\":\\"image/png\\"}]}"`; exports[`Test plugin manifest options does file name based cache busting 1`] = ` [MockFunction] { "calls": Array [ Array [ "public/manifest.webmanifest", - "{\\"name\\":\\"GatsbyJS\\",\\"short_name\\":\\"GatsbyJS\\",\\"start_url\\":\\"/\\",\\"background_color\\":\\"#f7f0eb\\",\\"theme_color\\":\\"#a2466c\\",\\"display\\":\\"standalone\\",\\"icons\\":[{\\"src\\":\\"/icons/icon-48x48-contentDigest.png\\",\\"sizes\\":\\"48x48\\",\\"type\\":\\"image/png\\",\\"purpose\\":\\"all\\"},{\\"src\\":\\"/icons/icon-128x128-contentDigest.png\\",\\"sizes\\":\\"128x128\\",\\"type\\":\\"image/png\\"}]}", - ], - ], - "results": Array [ - Object { - "type": "return", - "value": undefined, - }, - ], -} -`; - -exports[`Test plugin manifest options properly prefixes icons in manifest with asset or path prefix 1`] = ` -[MockFunction] { - "calls": Array [ - Array [ - "public/manifest.webmanifest", - "{\\"name\\":\\"GatsbyJS\\",\\"short_name\\":\\"GatsbyJS\\",\\"start_url\\":\\"/\\",\\"background_color\\":\\"#f7f0eb\\",\\"theme_color\\":\\"#a2466c\\",\\"display\\":\\"standalone\\",\\"icons\\":[{\\"src\\":\\"/test-prefix/icons/icon-48x48.png\\",\\"sizes\\":\\"48x48\\",\\"type\\":\\"image/png\\",\\"purpose\\":\\"all\\"},{\\"src\\":\\"/test-prefix/icons/icon-128x128.png\\",\\"sizes\\":\\"128x128\\",\\"type\\":\\"image/png\\"}]}", + "{\\"name\\":\\"GatsbyJS\\",\\"short_name\\":\\"GatsbyJS\\",\\"start_url\\":\\"/\\",\\"background_color\\":\\"#f7f0eb\\",\\"theme_color\\":\\"#a2466c\\",\\"display\\":\\"standalone\\",\\"icons\\":[{\\"src\\":\\"icons/icon-48x48-contentDigest.png\\",\\"sizes\\":\\"48x48\\",\\"type\\":\\"image/png\\",\\"purpose\\":\\"all\\"},{\\"src\\":\\"icons/icon-128x128-contentDigest.png\\",\\"sizes\\":\\"128x128\\",\\"type\\":\\"image/png\\"}]}", ], ], "results": Array [ diff --git a/packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js index 5ff468abcb4b5..8cc3e517c3be4 100644 --- a/packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js @@ -71,13 +71,13 @@ const manifestOptions = { display: `standalone`, icons: [ { - src: `/icons/icon-48x48.png`, + src: `icons/icon-48x48.png`, sizes: `48x48`, type: `image/png`, purpose: `all`, }, { - src: `/icons/icon-128x128.png`, + src: `icons/icon-128x128.png`, sizes: `128x128`, type: `image/png`, }, @@ -90,7 +90,6 @@ describe(`Test plugin manifest options`, () => { fs.mkdirSync.mockReset() fs.existsSync.mockReset() sharp.mockClear() - global.__PATH_PREFIX__ = `` }) // the require of gatsby-node performs the invoking @@ -124,12 +123,12 @@ describe(`Test plugin manifest options`, () => { const pluginSpecificOptions = { icons: [ { - src: `/icons/icon-48x48.png`, + src: `icons/icon-48x48.png`, sizes: `${size}x${size}`, type: `image/png`, }, { - src: `/other-icons/icon-48x48.png`, + src: `other-icons/icon-48x48.png`, sizes: `${size}x${size}`, type: `image/png`, }, @@ -143,11 +142,11 @@ describe(`Test plugin manifest options`, () => { const firstIconPath = path.join( `public`, - path.dirname(`/icons/icon-48x48.png`) + path.dirname(`icons/icon-48x48.png`) ) const secondIconPath = path.join( `public`, - path.dirname(`/other-icons/icon-48x48.png`) + path.dirname(`other-icons/icon-48x48.png`) ) expect(fs.mkdirSync).toHaveBeenNthCalledWith(1, firstIconPath) @@ -164,7 +163,7 @@ describe(`Test plugin manifest options`, () => { icon: icon, icons: [ { - src: `/icons/icon-48x48.png`, + src: `icons/icon-48x48.png`, sizes: `${size}x${size}`, type: `image/png`, }, @@ -258,16 +257,6 @@ describe(`Test plugin manifest options`, () => { ) }) - it(`properly prefixes icons in manifest with asset or path prefix`, async () => { - global.__PATH_PREFIX__ = `/test-prefix` - - await onPostBootstrap(apiArgs, { - ...manifestOptions, - }) - - expect(fs.writeFileSync).toMatchSnapshot() - }) - it(`icon options iterator adds options and the icon array take precedence`, async () => { fs.statSync.mockReturnValueOnce({ isFile: () => true }) diff --git a/packages/gatsby-plugin-manifest/src/gatsby-node.js b/packages/gatsby-plugin-manifest/src/gatsby-node.js index d7cd540b7e031..d9faf2877e38f 100644 --- a/packages/gatsby-plugin-manifest/src/gatsby-node.js +++ b/packages/gatsby-plugin-manifest/src/gatsby-node.js @@ -3,10 +3,6 @@ import path from "path" import sharp from "./safe-sharp" import { createContentDigest, cpuCoreCount } from "gatsby-core-utils" import { defaultIcons, doesIconExist, addDigestToPath } from "./common" -import { withPrefix as fallbackWithPrefix, withAssetPrefix } from "gatsby" - -// TODO: remove for v3 -const withPrefix = withAssetPrefix || fallbackWithPrefix sharp.simd(true) @@ -123,11 +119,6 @@ const makeManifest = async (cache, reporter, pluginOptions, shouldLocalize) => { manifest.icons = [...defaultIcons] } - //Fix #18497 by prefixing paths - manifest.icons = manifest.icons.map(icon => { - return { ...icon, src: withPrefix(icon.src) } - }) - // Specify extra options for each icon (if requested). if (pluginOptions.icon_options) { manifest.icons = manifest.icons.map(icon => { From 9c81f3cbbcb3092e3638fc325f95f6914cdd0a88 Mon Sep 17 00:00:00 2001 From: Alex Moon Date: Fri, 20 Dec 2019 14:36:53 +0000 Subject: [PATCH 3/5] fix: prefix manifest paths, fixes #18497 --- .../gatsby-plugin-manifest/src/gatsby-node.js | 40 +++++++++++++++---- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/packages/gatsby-plugin-manifest/src/gatsby-node.js b/packages/gatsby-plugin-manifest/src/gatsby-node.js index d9faf2877e38f..245db2ee6226d 100644 --- a/packages/gatsby-plugin-manifest/src/gatsby-node.js +++ b/packages/gatsby-plugin-manifest/src/gatsby-node.js @@ -58,7 +58,7 @@ async function checkCache(cache, icon, srcIcon, srcIconDigest, callback) { } exports.onPostBootstrap = async ( - { reporter, parentSpan }, + { reporter, parentSpan, basePath }, { localize, ...manifest } ) => { const activity = reporter.activityTimer(`Build manifest and related icons`, { @@ -68,7 +68,7 @@ exports.onPostBootstrap = async ( let cache = new Map() - await makeManifest(cache, reporter, manifest) + await makeManifest({ cache, reporter, pluginOptions: manifest, basePath }) if (Array.isArray(localize)) { const locales = [...localize] @@ -84,23 +84,44 @@ exports.onPostBootstrap = async ( cacheModeOverride = { cache_busting_mode: `name` } } - return makeManifest( + return makeManifest({ cache, reporter, - { + pluginOptions: { ...manifest, ...locale, ...cacheModeOverride, }, - true - ) + shouldLocalize: true, + basePath, + }) }) ) } activity.end() } -const makeManifest = async (cache, reporter, pluginOptions, shouldLocalize) => { +/** + * The complete Triforce, or one or more components of the Triforce. + * @typedef {Object} makeManifestArgs + * @property {Object} cache - from gatsby-node api + * @property {Object} reporter - from gatsby-node api + * @property {Object} pluginOptions - from gatsby-node api/gatsby config + * @property {boolean} shouldLocalize + * @property {string} basePath - string of base path frpvided by gatsby node + */ + +/** + * Build manifest + * @param {makeManifestArgs} + */ +const makeManifest = async ({ + cache, + reporter, + pluginOptions, + shouldLocalize = false, + basePath = ``, +}) => { const { icon, ...manifest } = pluginOptions const suffix = shouldLocalize && pluginOptions.lang ? `_${pluginOptions.lang}` : `` @@ -196,6 +217,11 @@ const makeManifest = async (cache, reporter, pluginOptions, shouldLocalize) => { } } + //Fix #18497 by prefixing paths + manifest.icons = manifest.icons.map(icon => { + return { ...icon, src: path.join(basePath, icon.src) } + }) + //Write manifest fs.writeFileSync( path.join(`public`, `manifest${suffix}.webmanifest`), From 68e4b0af301a96a82a98f8eafd82733c74996073 Mon Sep 17 00:00:00 2001 From: Alex Moon Date: Sat, 21 Dec 2019 08:47:11 +0000 Subject: [PATCH 4/5] feat: prefix start_url as well --- packages/gatsby-plugin-manifest/src/gatsby-node.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/gatsby-plugin-manifest/src/gatsby-node.js b/packages/gatsby-plugin-manifest/src/gatsby-node.js index 245db2ee6226d..4129d6e71208d 100644 --- a/packages/gatsby-plugin-manifest/src/gatsby-node.js +++ b/packages/gatsby-plugin-manifest/src/gatsby-node.js @@ -219,7 +219,11 @@ const makeManifest = async ({ //Fix #18497 by prefixing paths manifest.icons = manifest.icons.map(icon => { - return { ...icon, src: path.join(basePath, icon.src) } + return { + ...icon, + src: path.join(basePath, icon.src), + start_url: path.join(basePath, icon.start_url), + } }) //Write manifest From ccc417fc999231628f1ff837590a547eb4d39062 Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Sat, 25 Jan 2020 01:00:37 +0100 Subject: [PATCH 5/5] fix start-url & add tests --- packages/gatsby-plugin-manifest/package.json | 2 +- .../__snapshots__/gatsby-node.js.snap | 2 + .../src/__tests__/gatsby-node.js | 79 ++++++++++++++++++- .../gatsby-plugin-manifest/src/gatsby-node.js | 10 +-- yarn.lock | 8 ++ 5 files changed, 91 insertions(+), 10 deletions(-) diff --git a/packages/gatsby-plugin-manifest/package.json b/packages/gatsby-plugin-manifest/package.json index 195124a5286ab..d9a1cddabe581 100644 --- a/packages/gatsby-plugin-manifest/package.json +++ b/packages/gatsby-plugin-manifest/package.json @@ -8,7 +8,7 @@ }, "dependencies": { "@babel/runtime": "^7.7.6", - "gatsby-core-utils": "^1.0.24", + "gatsby-core-utils": "^1.0.26", "semver": "^5.7.1", "sharp": "^0.23.4" }, diff --git a/packages/gatsby-plugin-manifest/src/__tests__/__snapshots__/gatsby-node.js.snap b/packages/gatsby-plugin-manifest/src/__tests__/__snapshots__/gatsby-node.js.snap index 783940566560d..1580eba43d9f7 100644 --- a/packages/gatsby-plugin-manifest/src/__tests__/__snapshots__/gatsby-node.js.snap +++ b/packages/gatsby-plugin-manifest/src/__tests__/__snapshots__/gatsby-node.js.snap @@ -2,6 +2,8 @@ exports[`Test plugin manifest options correctly works with default parameters 1`] = `"{\\"name\\":\\"GatsbyJS\\",\\"short_name\\":\\"GatsbyJS\\",\\"start_url\\":\\"/\\",\\"background_color\\":\\"#f7f0eb\\",\\"theme_color\\":\\"#a2466c\\",\\"display\\":\\"standalone\\",\\"icons\\":[{\\"src\\":\\"icons/icon-48x48.png\\",\\"sizes\\":\\"48x48\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"icons/icon-72x72.png\\",\\"sizes\\":\\"72x72\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"icons/icon-96x96.png\\",\\"sizes\\":\\"96x96\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"icons/icon-144x144.png\\",\\"sizes\\":\\"144x144\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"icons/icon-192x192.png\\",\\"sizes\\":\\"192x192\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"icons/icon-256x256.png\\",\\"sizes\\":\\"256x256\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"icons/icon-384x384.png\\",\\"sizes\\":\\"384x384\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"icons/icon-512x512.png\\",\\"sizes\\":\\"512x512\\",\\"type\\":\\"image/png\\"}]}"`; +exports[`Test plugin manifest options correctly works with pathPrefix 1`] = `"{\\"name\\":\\"GatsbyJS\\",\\"short_name\\":\\"GatsbyJS\\",\\"start_url\\":\\"/blog/\\",\\"background_color\\":\\"#f7f0eb\\",\\"theme_color\\":\\"#a2466c\\",\\"display\\":\\"standalone\\",\\"icons\\":[{\\"src\\":\\"/blog/icons/icon-48x48.png\\",\\"sizes\\":\\"48x48\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"/blog/icons/icon-72x72.png\\",\\"sizes\\":\\"72x72\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"/blog/icons/icon-96x96.png\\",\\"sizes\\":\\"96x96\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"/blog/icons/icon-144x144.png\\",\\"sizes\\":\\"144x144\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"/blog/icons/icon-192x192.png\\",\\"sizes\\":\\"192x192\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"/blog/icons/icon-256x256.png\\",\\"sizes\\":\\"256x256\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"/blog/icons/icon-384x384.png\\",\\"sizes\\":\\"384x384\\",\\"type\\":\\"image/png\\"},{\\"src\\":\\"/blog/icons/icon-512x512.png\\",\\"sizes\\":\\"512x512\\",\\"type\\":\\"image/png\\"}]}"`; + exports[`Test plugin manifest options does file name based cache busting 1`] = ` [MockFunction] { "calls": Array [ diff --git a/packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js index 8cc3e517c3be4..efcdd5cd7521a 100644 --- a/packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js @@ -39,7 +39,9 @@ jest.mock(`sharp`, () => { }) jest.mock(`gatsby-core-utils`, () => { + const originalCoreUtils = jest.requireActual(`gatsby-core-utils`) return { + slash: originalCoreUtils.slash, cpuCoreCount: jest.fn(() => `1`), createContentDigest: jest.fn(() => `contentDigest`), } @@ -277,9 +279,31 @@ describe(`Test plugin manifest options`, () => { expect(content.icons[1].purpose).toEqual(`maskable`) }) + it(`correctly works with pathPrefix`, async () => { + await onPostBootstrap( + { ...apiArgs, basePath: `/blog` }, + { + name: `GatsbyJS`, + short_name: `GatsbyJS`, + start_url: `/`, + background_color: `#f7f0eb`, + theme_color: `#a2466c`, + display: `standalone`, + } + ) + const contents = fs.writeFileSync.mock.calls[0][1] + expect(fs.writeFileSync).toHaveBeenCalledWith( + path.join(`public`, `manifest.webmanifest`), + expect.anything() + ) + expect(sharp).toHaveBeenCalledTimes(0) + expect(contents).toMatchSnapshot() + }) + it(`generates all language versions`, async () => { fs.statSync.mockReturnValueOnce({ isFile: () => true }) const pluginSpecificOptions = { + ...manifestOptions, localize: [ { ...manifestOptions, @@ -291,10 +315,6 @@ describe(`Test plugin manifest options`, () => { start_url: `/es/`, lang: `es`, }, - { - ...manifestOptions, - start_url: `/`, - }, ], } const { localize, ...manifest } = pluginSpecificOptions @@ -317,6 +337,57 @@ describe(`Test plugin manifest options`, () => { JSON.stringify(expectedResults[2]) ) }) + it(`generates all language versions with pathPrefix`, async () => { + fs.statSync.mockReturnValueOnce({ isFile: () => true }) + const pluginSpecificOptions = { + ...manifestOptions, + localize: [ + { + ...manifestOptions, + start_url: `/de/`, + lang: `de`, + }, + { + ...manifestOptions, + start_url: `/es/`, + lang: `es`, + }, + ], + } + + const { localize, ...manifest } = pluginSpecificOptions + const expectedResults = [manifest].concat(localize).map(x => { + return { + ...manifest, + ...x, + start_url: path.posix.join(`/blog`, x.start_url), + icons: manifest.icons.map(icon => { + return { + ...icon, + src: path.posix.join(`/blog`, icon.src), + } + }), + } + }) + + await onPostBootstrap( + { ...apiArgs, basePath: `/blog` }, + pluginSpecificOptions + ) + + expect(fs.writeFileSync).toHaveBeenCalledWith( + expect.anything(), + JSON.stringify(expectedResults[0]) + ) + expect(fs.writeFileSync).toHaveBeenCalledWith( + expect.anything(), + JSON.stringify(expectedResults[1]) + ) + expect(fs.writeFileSync).toHaveBeenCalledWith( + expect.anything(), + JSON.stringify(expectedResults[2]) + ) + }) it(`merges default and language options`, async () => { fs.statSync.mockReturnValueOnce({ isFile: () => true }) diff --git a/packages/gatsby-plugin-manifest/src/gatsby-node.js b/packages/gatsby-plugin-manifest/src/gatsby-node.js index 4129d6e71208d..f8c9d98361126 100644 --- a/packages/gatsby-plugin-manifest/src/gatsby-node.js +++ b/packages/gatsby-plugin-manifest/src/gatsby-node.js @@ -1,7 +1,7 @@ import fs from "fs" import path from "path" import sharp from "./safe-sharp" -import { createContentDigest, cpuCoreCount } from "gatsby-core-utils" +import { createContentDigest, cpuCoreCount, slash } from "gatsby-core-utils" import { defaultIcons, doesIconExist, addDigestToPath } from "./common" sharp.simd(true) @@ -107,8 +107,8 @@ exports.onPostBootstrap = async ( * @property {Object} cache - from gatsby-node api * @property {Object} reporter - from gatsby-node api * @property {Object} pluginOptions - from gatsby-node api/gatsby config - * @property {boolean} shouldLocalize - * @property {string} basePath - string of base path frpvided by gatsby node + * @property {boolean?} shouldLocalize + * @property {string?} basePath - string of base path frpvided by gatsby node */ /** @@ -221,10 +221,10 @@ const makeManifest = async ({ manifest.icons = manifest.icons.map(icon => { return { ...icon, - src: path.join(basePath, icon.src), - start_url: path.join(basePath, icon.start_url), + src: slash(path.join(basePath, icon.src)), } }) + manifest.start_url = path.posix.join(basePath, manifest.start_url) //Write manifest fs.writeFileSync( diff --git a/yarn.lock b/yarn.lock index 5eb8d7e76ed8e..94c2294d4e7b3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10314,6 +10314,14 @@ gather-stream@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/gather-stream/-/gather-stream-1.0.0.tgz#b33994af457a8115700d410f317733cbe7a0904b" +gatsby-core-utils@^1.0.26: + version "1.0.26" + resolved "https://registry.yarnpkg.com/gatsby-core-utils/-/gatsby-core-utils-1.0.26.tgz#e1cbdfad498d58d677d9d74f21a1ede661b49d6f" + integrity sha512-NPflmXmyTcg3x2mp6cqp/51QeAHRKepfbf0X4erDsnVlewFJuGTe+25ZJvWkkwU2g1cPAxuwzlPe0jOL92iU4A== + dependencies: + ci-info "2.0.0" + node-object-hash "^2.0.0" + gatsby-node-helpers@^0.3.0: version "0.3.0" resolved "https://registry.yarnpkg.com/gatsby-node-helpers/-/gatsby-node-helpers-0.3.0.tgz#3bdca3b7902a702a5834fef280ad66d51099d57c"