diff --git a/example/nuxt.config.js b/example/nuxt.config.js index 543cafeb..7941c45c 100644 --- a/example/nuxt.config.js +++ b/example/nuxt.config.js @@ -10,6 +10,9 @@ module.exports = { modules: [ { handler: require('../') } ], + amp: { + origin: 'http://localhost:3000' + }, router: { middleware: [ 'amp' diff --git a/lib/amp/middleware.js b/lib/amp/middleware.js index 8e409088..89046f9c 100644 --- a/lib/amp/middleware.js +++ b/lib/amp/middleware.js @@ -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 } @@ -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 diff --git a/lib/module.js b/lib/module.js index f720a4c7..a7774033 100644 --- a/lib/module.js +++ b/lib/module.js @@ -15,6 +15,8 @@ const ampBodyPattern = /]*>([.\S\s]*)<\/amp-body>/ module.exports = function (moduleOptions) { const options = { cdnBase: undefined, + origin: '', + ...this.options.amp, ...moduleOptions } @@ -141,7 +143,8 @@ function copyAMP (options) { } this.addTemplate({ src: resolve(coreRoot, file), - fileName: join('amp', file) + fileName: join('amp', file), + options }) } }