Skip to content
Permalink
Browse files

fix: automatically create sitemap in `dist` (#42)

  • Loading branch information...
ricardogobbosouza authored and manniL committed Feb 7, 2019
1 parent eefc7a6 commit 2a0f0e38f46e56b8669cacda462ad818f3d1961b
Showing with 49 additions and 1,322 deletions.
  1. +0 −7 README.md
  2. +1 −0 package.json
  3. +33 −44 src/index.js
  4. +0 −1 test/fixture/nuxt.config.js
  5. +15 −1,270 yarn.lock
@@ -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/**'
@@ -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`
@@ -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",
@@ -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)
@@ -63,32 +51,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)
}
})

@@ -12,7 +12,6 @@ module.exports = {
exclude: [
'/exclude'
],
generate: true,
gzip: true,
hostname: 'http://localhost:3000/',
routes: [

0 comments on commit 2a0f0e3

Please sign in to comment.
You can’t perform that action at this time.