Skip to content

Commit

Permalink
feat: add og:image support (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
ishitatsuyuki authored and pi0 committed Mar 1, 2018
1 parent b17bbd0 commit afebd96
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ Using Nuxt PWA you can supercharge your current or next Nuxt project with a heav

## License

MIT - Nuxt Community - Pooya Parsa
MIT - Nuxt Community - Pooya Parsa
50 changes: 49 additions & 1 deletion packages/meta/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ function generateMeta (_options) {
lang: 'en',
ogType: 'website',
ogTitle: true,
ogDescription: true
ogDescription: true,
ogImage: true
}

// Combine sources
Expand Down Expand Up @@ -140,6 +141,53 @@ function generateMeta (_options) {
if (options.ogDescription && !find(this.options.head.meta, 'property', 'og:description') && !find(this.options.head.meta, 'name', 'og:description')) {
this.options.head.meta.push({ hid: 'og:description', name: 'og:description', property: 'og:description', content: options.ogDescription })
}

// og:image
if (options.ogImage === true) {
if (options.icons && options.icons.length > 0) {
const iconBig = options.icons[options.icons.length - 1]
const [width, height] = iconBig.sizes.split('x').map(x => parseInt(x))
options.ogImage = {path: iconBig.src, width, height, type: iconBig.type}
} else {
options.ogImage = false
}
} else if (typeof options.ogImage === 'string') {
options.ogImage = {src: options.ogImage}
}
if (options.ogImage && !find(this.options.head.meta, 'property', 'og:image') && !find(this.options.head.meta, 'name', 'og:image')) {
if (options.ogHost) {
this.options.head.meta.push({
hid: 'og:image',
name: 'og:image',
property: 'og:image',
content: options.ogHost + options.ogImage.path
})
if (options.ogImage.width && options.ogImage.height) {
this.options.head.meta.push({
hid: 'og:image:width',
name: 'og:image:width',
property: 'og:image:width',
content: options.ogImage.width
})
this.options.head.meta.push({
hid: 'og:image:height',
name: 'og:image:height',
property: 'og:image:height',
content: options.ogImage.height
})
}
if (options.ogImage.type) {
this.options.head.meta.push({
hid: 'og:image:type',
name: 'og:image:type',
property: 'og:image:type',
content: options.ogImage.type
})
}
} else {
debug('No host specified, skipping og:image')
}
}
}

module.exports.meta = require('./package.json')

0 comments on commit afebd96

Please sign in to comment.