Skip to content

Commit

Permalink
fix: fixed route processing, added condition on meta tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Faggin committed Sep 20, 2019
1 parent 135b260 commit 492d547
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
25 changes: 14 additions & 11 deletions lib/amp/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,19 @@ const createCustomHead = originalHead => function customHead () {
*/
ensureKey(head, 'link', [])

if (origin && !head.link.find(l => l.rel === 'canonical' || l.hid === 'canonical')) {
const path = this.$isAMP && this.$ampMode !== 'only'
? this.$route.fullPath.replace(/^\/amp(\/.*)?/, '$1')
: this.$route.fullPath

head.link.push({
rel: 'canonical',
hid: 'canonical',
href: origin + path
})
// Add canonical meta only if page is served as AMP
if (this.$ampMode !== false && this.$isAMP === true) {
if (origin && !head.link.find(l => l.rel === 'canonical' || l.hid === 'canonical')) {
const path = this.$isAMP && this.$ampMode !== 'only'
? this.$route.fullPath.replace(/^\/amp(\/.*)?/, '$1')
: this.$route.fullPath

head.link.push({
rel: 'canonical',
hid: 'canonical',
href: origin + path
})
}
}

/**
Expand All @@ -123,7 +126,7 @@ const createCustomHead = originalHead => function customHead () {
head.bodyAttrs.class += ' __amp'
}

// check if amp is disabled for this page
// Add amphtml meta only if page has amp counterpart
if (this.$ampMode !== false && this.$isAMP === false) {
if (!head.link.find(l => l.rel === 'amphtml' || l.hid === 'amphtml')) {
const ampPrefix = this.$ampMode === 'only' ? '' : '/amp'
Expand Down
25 changes: 13 additions & 12 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,22 @@ module.exports = function (moduleOptions) {
registerValidator.call(this, options)
}

this.nuxt.hook('build:extendRoutes', (routes) => {
processRoutes(routes)
})
processRoutes()
}

function processRoutes (routes) {
for (const route of routes) {
route.meta = route.meta || {}
route.alias = route.alias || []
if (route.path === '/amp' || route.path.indexOf('/amp/') === 0) {
route.meta.amp = true
} else {
route.alias = '/amp' + route.path
function processRoutes () {
this.extendRoutes((routes) => {
for (const route of routes) {
route.meta = route.meta || {}
route.alias = route.alias || []

if (route.path === '/amp' || route.path.indexOf('/amp/') === 0) {
route.meta.amp = true
} else {
route.alias = '/amp' + route.path
}
}
}
})
}

async function registerValidator (options) {
Expand Down

0 comments on commit 492d547

Please # to comment.