Skip to content

Commit

Permalink
fix(nuxt-generate): generate canonical link using module option
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Sep 12, 2019
1 parent 9b3a168 commit 0f5e5ad
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
3 changes: 3 additions & 0 deletions example/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module.exports = {
modules: [
{ handler: require('../') }
],
amp: {
origin: 'http://localhost:3000'
},
router: {
middleware: [
'amp'
Expand Down
7 changes: 3 additions & 4 deletions lib/amp/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ Middleware.amp = function ({ route, req, app }) {
}

const createCustomHead = originalHead => function customHead () {
let origin = process.server ? this.$req.url : window.location
let origin
if (process.server) {
const schema = 'http://'
origin = schema + this.$req.headers.host
origin = '<%= options.origin %>'
} else {
origin = window.location.origin
}
Expand All @@ -63,7 +62,7 @@ const createCustomHead = originalHead => function customHead () {

const { amp, ...query } = this.$route.query

if (!head.link.find(l => l.rel === 'canonical' || l.hid === 'canonical')) {
if (origin && !head.link.find(l => l.rel === 'canonical' || l.hid === 'canonical')) {
const path = this.$isAMP && this.$ampMode !== 'only'
? this.$route.path.replace(/^\/amp(\/.*)?/, '$1')
: this.$route.path
Expand Down
5 changes: 4 additions & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const ampBodyPattern = /<amp-body[^>]*>([.\S\s]*)<\/amp-body>/
module.exports = function (moduleOptions) {
const options = {
cdnBase: undefined,
origin: '',
...this.options.amp,
...moduleOptions
}

Expand Down Expand Up @@ -141,7 +143,8 @@ function copyAMP (options) {
}
this.addTemplate({
src: resolve(coreRoot, file),
fileName: join('amp', file)
fileName: join('amp', file),
options
})
}
}
Expand Down

0 comments on commit 0f5e5ad

Please # to comment.