Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: closes #69 and & adds sitemap: false option to pages #70

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
COMPONENT_OPTIONS_BLOCK: 'script',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In any <page>.vue file, it will always be a script element.

Why create a constant?

COMPONENT_OPTIONS_KEY: 'sitemap'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be customisable, you can set that key in the sitemap configuration in nuxt.config.js instead of create a constant.

}
51 changes: 51 additions & 0 deletions lib/extractComponentOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const { readFileSync } = require('fs')

const acorn = require('acorn')
const dynamicImport = require('acorn-dynamic-import')
const inject = require('acorn-dynamic-import/lib/walk')
const walker = inject.default(require('acorn-walk'))
// Must not be an explicit dependency to avoid version mismatch issue.
// See https://github.com/nuxt-community/nuxt-i18n/issues/297
const compiler = require('vue-template-compiler')
// const { COMPONENT_OPTIONS_BLOCK, COMPONENT_OPTIONS_KEY } = require('constants')

function extractComponentOptions (path, blockName, key) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should rename the function and file from extractComponentOptions to extractPageOptions, because it is relative to the *.vue file in the Pages folder.

let extractedData = key ? '' : {}
let Component = compiler.parseComponent(readFileSync(path).toString())
const block =
Component[blockName] ||
Component.customBlocks.find(block => block.type === blockName)
if (!block || block.content.length < 1) {
return extractedData
}
const parsed = acorn.Parser.extend(dynamicImport.default).parse(
block.content,
{
ecmaVersion: 10,
sourceType: 'module'
}
)
walker.simple(
parsed,
{
Property (node) {
const data = block.content.substring(node.start, node.end)
try {
if (key) {
if (node.key.name === key) extractedData = eval(`({${data}})`)[key] // eslint-disable-line no-eval
} else Object.assign(extractedData, eval(`({${data}})`)) // eslint-disable-line no-eval
} catch (e) {}
}
},
walker.base
)

return extractedData
}

module.exports = extractComponentOptions
// .bind(
// this,
// COMPONENT_OPTIONS_BLOCK,
// COMPONENT_OPTIONS_KEY
// )
12 changes: 8 additions & 4 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const AsyncCache = require('async-cache')
const consola = require('consola')
const { promisify } = require('util')
const { hostname } = require('os')

const extractComponentOptions = require('./extractComponentOptions')
const { COMPONENT_OPTIONS_BLOCK, COMPONENT_OPTIONS_KEY } = require('./constants')
const defaultPublicPath = '/_nuxt/'

module.exports = function module (moduleOptions) {
Expand Down Expand Up @@ -54,13 +55,16 @@ module.exports = function module (moduleOptions) {
this.extendRoutes(routes => {
// Get all static routes and ignore dynamic routes
let staticRoutes = flattenRoutes(routes)
.filter(r => !r.includes(':') && !r.includes('*'))
.filter(r => !r.url.includes(':') && !r.url.includes('*'))
.filter(route => {
return extractComponentOptions(route.component, COMPONENT_OPTIONS_BLOCK, COMPONENT_OPTIONS_KEY) !== false
})

// Exclude routes
options.exclude.forEach(pattern => {
const minimatch = new Minimatch(pattern)
minimatch.negate = true
staticRoutes = staticRoutes.filter(route => minimatch.match(route))
staticRoutes = staticRoutes.filter(route => minimatch.match(route.url))
})

// Create a cache for routes
Expand Down Expand Up @@ -232,7 +236,7 @@ function flattenRoutes (router, path = '', routes = []) {
flattenRoutes(r.children, path + r.path + '/', routes)
}
if (r.path !== '') {
routes.push(path + r.path)
routes.push({ ...r, url: path + r.path })
Copy link
Member

@NicoPennec NicoPennec Sep 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This update can create a breaking change in the usage of the filter option of the sitemap module, no ?

if yes, your commit message must respect the conventional commit to bump the release version as "major" update by the release script

}
})
return routes
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
"testEnvironment": "node"
},
"dependencies": {
"acorn": "^6.1.1",
"acorn-dynamic-import": "^4.0.0",
"acorn-walk": "^6.1.1",
"async-cache": "^1.1.0",
"consola": "^2.6.1",
"fs-extra": "^8.0.0",
Expand Down
10 changes: 10 additions & 0 deletions test/fixture/pages/excluded-via-component-options.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<template>
<div>
/excluded-via-component-options
</div>
</template>
<script>
export default {
sitemap:false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why limit the sitemap option to a simple boolean for exclude / include a page from the sitemap?

Maybe you can support the following options in addition of sitemap: true :

<script>
  export default {
    sitemap: {
      changefreq: 'daily',
      priority: 1,
      lastmod: new Date(),
      // ...
   }

}
</script>
3 changes: 3 additions & 0 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ describe('ssr', () => {
// excluded routes
html = await get('/exclude')
expect(html).toContain('/exclude')
html = await get('/excluded-via-component-options')
expect(html).toContain('/excluded-via-component-options')

// filtered routes
html = await get('/filtered')
Expand Down Expand Up @@ -79,6 +81,7 @@ describe('ssr', () => {

// excluded routes
expect(xml).not.toContain('<loc>http://localhost:3000/exclude</loc>')
expect(xml).not.toContain('<loc>http://localhost:3000/excluded-via-component-options</loc>')

// filtered routes
expect(xml).not.toContain('<loc>http://localhost:3000/filtered</loc>')
Expand Down