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

fix: update defaults and generate sitemap.xml in dist directory #42

Merged
merged 6 commits into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
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 @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

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

Last thing I promise! 😄

A notice for the user that generate isn't needed anymore

Copy link
Member Author

Choose a reason for hiding this comment

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

@manniL great review!
Added notice with consola.warn

(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 `generate` isn\'t needed anymore')
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
consola.warn('The option `generate` isn\'t needed anymore')
consola.warn('The option `sitemap.generate` isn\'t needed anymore')

Copy link
Member

Choose a reason for hiding this comment

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

So the user knows which prop it is exactly

Copy link
Member Author

Choose a reason for hiding this comment

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

Ready! 👍

}

// 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