Skip to content
This repository has been archived by the owner on Dec 5, 2021. It is now read-only.

Commit

Permalink
Merge pull request #97 from abernh/fix-96_target-static--duplicate-me…
Browse files Browse the repository at this point in the history
…ta-tags

Fix 96 target static  duplicate meta tags
  • Loading branch information
TiagoDanin authored May 20, 2021
2 parents 52a9a45 + 0dd8d6e commit 11712b8
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 46 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,5 @@ typings/

# Nuxt
example/.nuxt
example/.nuxtStatic
dist/
13 changes: 13 additions & 0 deletions example/nuxt.config.static.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const path = require('path')
const baseConfig = require('./nuxt.config')

const buildDir = path.resolve(__dirname, '.nuxtStatic')

module.exports = {
...baseConfig,
generate: {
dir: `${buildDir}/_static`
},
buildDir,
target: 'static'
}
50 changes: 12 additions & 38 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ const createMeta = (options = {}, inputMeta = [], template = {}) => {

const generate = (metas, optionsGenerate, id = '', index = false) => {
metas = {...metas}
Object.keys(metas).map(k => {
const meta = {...metas[k]}
Object.keys(metas).forEach(k => {
const meta = {ids: false, ...metas[k]}
let metaIds
if (meta.fullId) {
meta.id = meta.fullId
} else if (!meta.id) { // eslint-disable-line no-negated-condition
Expand Down Expand Up @@ -189,60 +190,33 @@ const createMeta = (options = {}, inputMeta = [], template = {}) => {
})
} else if (typeof meta.value === 'object' && !Array.isArray(meta.value)) {
return generate(meta, meta.value, meta.id + ':')
} else if (meta.content) {
} else {
findAndRemove(meta.id)

meta.value = parserValue(meta.value)
if (!meta.value) {
return
}

if (meta.ids) {
meta.ids.map(id => {
meta.id = id
metaIds = meta.ids || [meta.id]
metaIds.forEach(id => {
meta.id = id

if (meta.content) {
outputMeta.push({
hid: meta.id,
key: typeof index === 'number' ? `${meta.id}:0${index}` : meta.id,
property: meta.id,
name: meta.id,
content: meta.value
})
})
} else {
outputMeta.push({
hid: meta.id,
key: typeof index === 'number' ? `${meta.id}:0${index}` : meta.id,
property: meta.id,
name: meta.id,
content: meta.value
})
}
} else if (meta.id) {
if (meta.ids) {
meta.ids.map(id => {
findAndRemove(id)

meta.value = parserValue(meta.value)
if (!meta.value) {
return
}

} else {
outputMeta.push({
hid: id,
[id]: meta.value
})
})
} else {
findAndRemove(meta.id)

meta.value = parserValue(meta.value)
if (!meta.value) {
return
}

outputMeta.push({
[meta.id]: meta.value
})
}
})
}
})
}
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/all.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
module.exports = [{
hid: 'charset',
charset: 'utf-8'
}, {
hid: 'lang',
lang: 'en'
}, {
hid: 'language',
language: 'English'
}, {
hid: 'copyright',
Expand Down
52 changes: 44 additions & 8 deletions test/nuxt.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,82 @@
const test = require('ava')
const {Nuxt, Builder} = require('nuxt')
const {Nuxt, Builder, Generator} = require('nuxt')
const fsPromises = require('fs').promises
const got = require('got')
const config = require('../example/nuxt.config')
const configStatic = require('../example/nuxt.config.static')

const url = path => `http://localhost:4000${path}`
const get = async path => {
const {body} = await got(url(path))
return body
}

const staticPath = path => `${configStatic.generate.dir}${path}index.html`
const getStatic = async path => {
return fsPromises.readFile(staticPath(path), {encoding: 'utf8'})
}

let nuxt = null

test.before('Init Nuxt.js', async () => {
// Init SSR
config.dev = false
config.seo.baseUrl = 'http://localhost:4000'
nuxt = new Nuxt(config)
await new Builder(nuxt).build()
nuxt.listen(4000, 'localhost')
await nuxt.listen(4000, 'localhost')

// Build STATIC
configStatic.dev = false
const nuxtStatic = new Nuxt(configStatic)
const builder = await new Builder(nuxtStatic)
const generator = await new Generator(nuxtStatic, builder)
await nuxtStatic.listen(4001, 'localhost')
const {errors} = await generator.generate({build: false})
if (errors.length > 0) {
throw new Error('Error generating pages, exiting with non-zero code')
}

await nuxtStatic.close()
})

test.after('Closing server', () => {
nuxt.close()
})

test('Static route /', async t => {
const html = await getStatic('/')
t.true(html.includes('<title>Home Page</title>'))
t.true(html.match('<meta data-n-head="ssr" data-hid="charset" charset="utf-8">').length === 1)
})

test('Static route /news', async t => {
const html = await getStatic('/news/')
t.true(html.includes('<title>Nuxt is the best</title>'))
t.true(html.match('<meta data-n-head="ssr" data-hid="charset" charset="utf-8">').length === 1)
})

test('Route / and render HTML', async t => {
const html = await get('/')

t.true(html.includes('<title>Home Page</title>'))
t.true(html.includes('<meta data-n-head="ssr" charset="utf-8"'))
t.true(html.includes('<meta data-n-head="ssr" lang="en">'))
t.true(html.includes('<meta data-n-head="ssr" language="English">'))
t.true(html.match('<meta data-n-head="ssr" data-hid="charset" charset="utf-8">').length === 1)
t.true(html.includes('<meta data-n-head="ssr" data-hid="charset" charset="utf-8"'))
t.true(html.includes('<meta data-n-head="ssr" data-hid="lang" lang="en">'))
t.true(html.includes('<meta data-n-head="ssr" data-hid="language" language="English">'))
t.true(html.includes('<meta data-n-head="ssr" data-hid="name" key="name" property="name" name="name" content="App name">'))
t.true(html.includes('<meta data-n-head="ssr" data-hid="description" key="description" property="description" name="description" content="Example app with Nuxt Seo">'))
t.true(html.includes('<link data-n-head="ssr" rel="canonical" href="http://localhost:4000/">'))
})

test('Route /news and render HTML', async t => {
await get('/news')
await get('/')
const html = await get('/news')
t.true(html.includes('<title>Nuxt is the best</title>'))
t.true(html.includes('<meta data-n-head="ssr" charset="utf-8">'))
t.true(html.includes('<meta data-n-head="ssr" lang="en">'))
t.true(html.includes('<meta data-n-head="ssr" language="English">'))
t.true(html.match('<meta data-n-head="ssr" data-hid="charset" charset="utf-8">').length === 1)
t.true(html.includes('<meta data-n-head="ssr" data-hid="lang" lang="en">'))
t.true(html.includes('<meta data-n-head="ssr" data-hid="language" language="English">'))
t.true(html.includes('<meta data-n-head="ssr" data-hid="name" key="name" property="name" name="name" content="App name">'))
t.true(html.includes('<meta data-n-head="ssr" data-hid="description" key="description" property="description" name="description" content="Hello World page in blog">'))
t.true(html.includes('<meta data-n-head="ssr" data-hid="og:site_name" key="og:site_name" property="og:site_name" name="og:site_name" content="App name">'))
Expand Down

0 comments on commit 11712b8

Please sign in to comment.