Skip to content

Commit

Permalink
fix: automatically create sitemap in dist (#42)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: usage of hook that require Nuxt >= 1.0
  • Loading branch information
ricardogobbosouza authored and Nicolas Pennec committed Apr 15, 2019
1 parent 8cefe1d commit 2767ccb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1,322 deletions.
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ Module based on the awesome [sitemap](https://github.com/ekalinin/sitemap.js) pa
hostname: 'https://example.com',
cacheTime: 1000 * 60 * 15,
gzip: true,
generate: false, // Enable me when using nuxt generate
exclude: [
'/secret',
'/admin/**'
Expand Down Expand Up @@ -70,16 +69,10 @@ Where serve/generate sitemap file

This values is **mandatory** for generation sitemap file, and you should explicitly provide it for generate mode.

### `generate`
- Default: `false`

Generates static sitemap file during build/generate instead of serving using middleware.

### `cacheTime`
- Default: `1000 * 60 * 15` (15 Minutes)

Defines how frequently should sitemap **routes** being updated.
This option is only effective when `generate` is `false`.
Please note that after each invalidation, `routes` will be evaluated again. (See [routes](#routes-1) section)

### `filter`
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
},
"dependencies": {
"async-cache": "^1.1.0",
"consola": "^2.4.0",
"fs-extra": "^7.0.1",
"is-https": "^1.0.0",
"lodash": "^4.17.10",
Expand Down
77 changes: 33 additions & 44 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,30 @@ const uniq = require('lodash/uniq')
const path = require('path')
const fs = require('fs-extra')
const AsyncCache = require('async-cache')
const consola = require('consola')
const { promisify } = require('util')
const { hostname } = require('os')

// Defaults
const defaults = {
path: '/sitemap.xml',
hostname: undefined,
generate: false,
exclude: [],
routes: [],
cacheTime: 1000 * 60 * 15,
filter: undefined,
gzip: false
}

module.exports = function module (moduleOptions) {
const defaults = {
path: 'sitemap.xml',
hostname: this.options.build.publicPath || undefined,
exclude: [],
routes: this.options.generate.routes || [],
cacheTime: 1000 * 60 * 15,
filter: undefined,
gzip: false
}

const options = Object.assign({}, defaults, this.options.sitemap, moduleOptions)
options.pathGzip = options.gzip ? `${options.path}.gz` : options.path

// sitemap-routes.json is written to dist dir on build mode
const jsonStaticRoutesPath = path.resolve(this.options.buildDir, path.join('dist', 'sitemap-routes.json'))

// sitemap.xml is written to static dir on generate mode
const xmlGeneratePath = path.resolve(this.options.srcDir, path.join(this.options.dir.static || 'static', options.path))

options.pathGzip = (options.gzip) ? `${options.path}.gz` : options.path
const gzipGeneratePath = path.resolve(this.options.srcDir, path.join(this.options.dir.static || 'static', options.pathGzip))

// Ensure no generated file exists
fs.removeSync(xmlGeneratePath)
fs.removeSync(gzipGeneratePath)

let staticRoutes = fs.readJsonSync(jsonStaticRoutesPath, { throws: false })
let cache = null

// TODO find a better way to detect if is a "build", "start" or "generate" command
// on "start" cmd only
if (staticRoutes && !this.options.dev) {
// Create a cache for routes
cache = createCache(staticRoutes, options)
Expand All @@ -62,32 +50,33 @@ module.exports = function module (moduleOptions) {
staticRoutes = staticRoutes.filter(route => minimatch.match(route))
})

if (this.options.dev || options.generate) {
if (this.options.dev) {
// Create a cache for routes
cache = createCache(staticRoutes, options)
}

if (!this.options.dev) {
// TODO on build process only
} else {
// Save static routes
fs.ensureDirSync(path.resolve(this.options.buildDir, 'dist'))
fs.writeJsonSync(jsonStaticRoutesPath, staticRoutes)
}
})

// TODO on generate process only and not on build process
if (options.generate) {
(async () => {
// Generate static sitemap.xml
const routes = await cache.get('routes')
const sitemap = await createSitemap(options, routes)
const xml = await sitemap.toXML()
await fs.ensureFile(xmlGeneratePath)
await fs.writeFile(xmlGeneratePath, xml)
if (options.gzip) {
const gzip = await sitemap.toGzip()
await fs.writeFile(gzipGeneratePath, gzip)
}
})()
}
if (options.generate) {
consola.warn('The option `sitemap.generate` isn\'t needed anymore')
}

// Generate sitemap.xml in dist
this.nuxt.hook('generate:done', async () => {
const routes = await cache.get('routes')
const sitemap = await createSitemap(options, routes)
const xml = await sitemap.toXML()
const xmlGeneratePath = path.resolve(this.options.rootDir, this.options.generate.dir, options.path)
await fs.ensureFile(xmlGeneratePath)
await fs.writeFile(xmlGeneratePath, xml)
if (options.gzip) {
const gzip = await sitemap.toGzip()
const gzipGeneratePath = path.resolve(this.options.rootDir, this.options.generate.dir, options.pathGzip)
await fs.ensureFile(gzipGeneratePath)
await fs.writeFile(gzipGeneratePath, gzip)
}
})

Expand Down
1 change: 0 additions & 1 deletion test/fixture/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ module.exports = {
exclude: [
'/exclude'
],
generate: true,
gzip: true,
hostname: 'http://localhost:3000/',
routes: [
Expand Down
Loading

0 comments on commit 2767ccb

Please sign in to comment.