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

feat(meta): merge head at runtime #363

Merged
merged 9 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 5 additions & 3 deletions docs/content/en/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ Add `@nuxtjs/pwa` dependency to your project:
<code-block label="Yarn" active>

```bash
yarn add @nuxtjs/pwa
yarn add --dev @nuxtjs/pwa
```

</code-block>
<code-block label="NPM">

```bash
npm i @nuxtjs/pwa
npm i --dev @nuxtjs/pwa
```

</code-block>
Expand All @@ -32,12 +32,14 @@ Edit your `nuxt.config.js` file to add pwa module::

```js{}[nuxt.config.js]
{
modules: [
buildModules: [
'@nuxtjs/pwa',
]
}
```

**NOTE:** If using `ssr: false` with production mode without `nuxt generate`, you have to use `modules` instead of `buildModules`

### Add Icon

Ensure `static` dir exists and optionally create `static/icon.png`. (Recommended to be square png and >= `512x512px`)
Expand Down
14 changes: 4 additions & 10 deletions lib/icon/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ const { joinUrl, getRouteParams, sizeName } = require('../utils')
const { version } = require('../../package.json')

module.exports = function (pwa) {
this.nuxt.hook('build:before', () => run.call(this, pwa, true))

if (this.options.mode === 'spa' && !this.options.dev) {
return run.call(this, pwa, false) // Fill meta
}
this.nuxt.hook('build:before', () => run.call(this, pwa))
}

async function run (pwa, _emitAssets) {
async function run (pwa) {
const { publicPath } = getRouteParams(this.options)

// Defaults
Expand Down Expand Up @@ -92,9 +88,7 @@ async function run (pwa, _emitAssets) {
}

// Emit assets in background
if (_emitAssets) {
emitAssets.call(this, options)
}
emitAssets.call(this, options)
}

async function findIcon (options) {
Expand All @@ -120,7 +114,7 @@ function addPlugin (options) {
if (options.plugin) {
this.addPlugin({
src: path.resolve(__dirname, './plugin.js'),
fileName: 'nuxt-icons.js',
fileName: 'pwa/icons.js',
options: {
pluginName: options.pluginName,
icons
Expand Down
22 changes: 6 additions & 16 deletions lib/manifest/module.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
const hash = require('hasha')

const { joinUrl, getRouteParams, find } = require('../utils')
const { joinUrl, getRouteParams } = require('../utils')

module.exports = function nuxtManifest (pwa) {
const hook = () => {
addManifest.call(this, pwa)
}

if (this.options.mode === 'spa') {
return hook()
}

this.nuxt.hook('build:before', hook)
this.nuxt.hook('build:before', () => addManifest.call(this, pwa))
}

function addManifest (pwa) {
Expand Down Expand Up @@ -67,11 +59,9 @@ function addManifest (pwa) {
})

// Add manifest meta
if (!find(this.options.head.link, 'rel', 'manifest')) {
const baseAttribute = { rel: 'manifest', href: joinUrl(options.publicPath, manifestFileName) }
const attribute = manifest.crossorigin ? Object.assign({}, baseAttribute, { crossorigin: manifest.crossorigin }) : baseAttribute
this.options.head.link.push(attribute)
} else {
console.warn('Manifest meta already provided!') // eslint-disable-line no-console
const manifestMeta = { rel: 'manifest', href: joinUrl(options.publicPath, manifestFileName), hid: 'manifest' }
if (manifest.crossorigin) {
manifestMeta.crossorigin = manifest.crossorigin
}
pwa._manifestMeta = manifestMeta
}
1 change: 1 addition & 0 deletions lib/meta/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= JSON.stringify(options.head, null, 2) %>
36 changes: 36 additions & 0 deletions lib/meta/meta.merge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
exports.mergeMeta = function mergeMeta (to, from) {
if (typeof to === 'function') {
// eslint-disable-next-line no-console
console.warn('Cannot merge meta. Avoid using head as a function!')
return
}

for (const key in from) {
const value = from[key]
if (Array.isArray(value)) {
to[key] = to[key] || []
for (const item of value) {
// Avoid duplicates
if (
(item.hid && hasMeta(to[key], 'hid', item.hid)) ||
(item.name && hasMeta(to[key], 'name', item.name))
) {
continue
}
// Add meta
to[key].push(item)
}
} else if (typeof value === 'object') {
to[key] = to[key] || {}
for (const attr in value) {
to[key][attr] = value[attr]
}
} else if (to[key] === undefined) {
to[key] = value
}
}
}

function hasMeta (arr, key, val) {
return arr.find(obj => val ? obj[key] === val : obj[key])
}
Loading