Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-plugin-manifest): fixes icons not getting asset prefix #20142

Merged
merged 7 commits into from
Jan 27, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix start-url & add tests
wardpeet committed Jan 25, 2020
commit ccc417fc999231628f1ff837590a547eb4d39062
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-manifest/package.json
Original file line number Diff line number Diff line change
@@ -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"
},
Original file line number Diff line number Diff line change
@@ -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 [
79 changes: 75 additions & 4 deletions packages/gatsby-plugin-manifest/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -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 })
10 changes: 5 additions & 5 deletions packages/gatsby-plugin-manifest/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -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(
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -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"