Skip to content

Commit

Permalink
fix: support nuxt@2.9 (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
thangman22 authored and farnabaz committed Jul 23, 2019
1 parent 3ef9d89 commit dc40609
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ node_modules
.DS_Store
coverage
dist
package-lock.json
18 changes: 13 additions & 5 deletions lib/amp/middleware.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import VueMeta from 'vue-meta'
import { stringify } from 'qs'
import Middleware from '../middleware'

function ensureKey(obj, key, d) {
const [vueMetaMajor] = VueMeta.version.split('.')

function ensureKey (obj, key, d) {
if (!obj[key]) {
obj[key] = d
}
}

function stringifyQuery(query) {
function stringifyQuery (query) {
if (!Object.keys(query).length) {
return ''
}
Expand All @@ -32,7 +35,7 @@ Middleware.amp = function ({ route, req, app }) {
}
}

const createCustomHead = originalHead => function customHead() {
const createCustomHead = originalHead => function customHead () {
let origin = process.server ? this.$req.url : window.location
if (process.server) {
const schema = 'http://'
Expand Down Expand Up @@ -73,7 +76,12 @@ const createCustomHead = originalHead => function customHead() {
*/
if (this.$isAMP) {
ensureKey(head, 'htmlAttrs', {})
head.htmlAttrs['⚡'] = undefined

if (vueMetaMajor >= 2) {
head.htmlAttrs.amp = true
} else {
head.htmlAttrs.amp = undefined
}

ensureKey(head, 'bodyAttrs', {})
ensureKey(head.bodyAttrs, 'class', '')
Expand All @@ -95,7 +103,7 @@ const createCustomHead = originalHead => function customHead() {
return head
}

const createCustomLayout = originalLayout => function customLayout(ctx) {
const createCustomLayout = originalLayout => function customLayout (ctx) {
let layout

switch (typeof originalLayout) {
Expand Down
4 changes: 2 additions & 2 deletions lib/amp/plugin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Vue from 'vue'
import './middleware'

function pick(...args) {
function pick (...args) {
for (const arg of args) {
if (arg !== undefined) {
return arg
Expand Down Expand Up @@ -46,7 +46,7 @@ export default function ({ route, req }, inject) {
// @vue/component
const AMPMustache = {
name: 'AmpMustache',
render(h) {
render (h) {
return h('template', {
props: {
...this.$props
Expand Down
20 changes: 10 additions & 10 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = function (moduleOptions) {
})
}

function processRoutes(routes) {
function processRoutes (routes) {
for (const route of routes) {
route.meta = route.meta || {}
route.alias = route.alias || []
Expand All @@ -44,11 +44,11 @@ function processRoutes(routes) {
}
}

async function registerValidator(options) {
async function registerValidator (options) {
const amphtmlValidator = require('amphtml-validator')
const validator = await amphtmlValidator.getInstance()
this.nuxt.hook('render:route', (url, { html }) => {
const isAMP = html.includes('')
const isAMP = html.includes('data-n-head="amp"')

if (isAMP) {
const result = validator.validateString(html)
Expand All @@ -73,11 +73,11 @@ async function registerValidator(options) {
})
}

function registerSSRHook(options) {
function registerSSRHook (options) {
const tags = getTags({ cdnBase: options.cdnBase })

this.nuxt.hook('vue-renderer:ssr:templateParams', (params) => {
const isAMP = params.HTML_ATTRS.includes('')
const isAMP = params.HTML_ATTRS.includes('amp')

if (!isAMP) {
return
Expand All @@ -88,7 +88,7 @@ function registerSSRHook(options) {

if (options.removeLayoutTags) {
params.APP = params.APP
.replace('<div data-server-rendered="true" id="__nuxt"><!----><div id="__layout">', '')
.replace('<div data-server-rendered="true" id="__nuxt"><!----><!----><div id="__layout">', '')
.replace(/<\/div><\/div>$/, '')
}

Expand All @@ -108,15 +108,15 @@ function registerSSRHook(options) {
})
}

function registerPlugin(options) {
function registerPlugin (options) {
this.addPlugin({
src: resolve(__dirname, 'amp', 'plugin.js'),
fileName: join('amp', 'plugin.js'),
options
})
}

function copyAMP(options) {
function copyAMP (options) {
const coreRoot = resolve(__dirname, 'amp')

for (const file of readdirSync(coreRoot)) {
Expand All @@ -130,11 +130,11 @@ function copyAMP(options) {
}
}

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

function ensureMeta() {
function ensureMeta () {
if (!this.options.head) {
this.options.head = {}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/tags.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function getTags({
function getTags ({
cdnBase = 'https://cdn.ampproject.org/v0/'
}) {
const tags = [
Expand Down Expand Up @@ -130,7 +130,7 @@ function getTags({
return tags
}

function detectTags(tags, html) {
function detectTags (tags, html) {
return tags.filter(t => t.isTemplate ? html.includes(`type="${t.tag}"`) : html.includes(`<${t.tag}`))
}

Expand Down

0 comments on commit dc40609

Please sign in to comment.