Skip to content

Commit

Permalink
fix: cache initialization
Browse files Browse the repository at this point in the history
fix #27 #51
  • Loading branch information
Nicolas Pennec committed Apr 15, 2019
1 parent 26b4a4f commit 67f9462
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -17,11 +17,13 @@
],
"main": "src/index.js",
"scripts": {
"build": "nuxt build test/fixture",
"dev": "nuxt test/fixture",
"generate": "nuxt generate test/fixture",
"lint": "eslint src test",
"preview": "standard-version --dry-run",
"release": "standard-version && git push --follow-tags && npm publish",
"start": "nuxt start test/fixture",
"test": "npm run lint && npm run unit",
"unit": "jest --maxWorkers=4"
},
Expand Down
26 changes: 15 additions & 11 deletions src/index.js
Expand Up @@ -24,20 +24,24 @@ module.exports = function module (moduleOptions) {
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-routes.json is written to dist dir on "build" mode
const jsonStaticRoutesPath = !this.options.dev ? path.resolve(this.options.buildDir, path.join('dist', 'sitemap-routes.json')) : null

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

if (staticRoutes && !this.options.dev) {
// On run cmd "start" or "generate [--no-build]"
if (staticRoutes) {
// Create a cache for routes
cache = createCache(staticRoutes, options)
// Hydrate cache
cache.get('routes')

// On server ready, hydrate cache
this.nuxt.hook('listen', () => {
cache.get('routes')
})
}

// Extend routes
// On extend routes
this.extendRoutes(routes => {
// Get all static routes and ignore dynamic routes
let staticRoutes = flattenRoutes(routes)
Expand All @@ -50,10 +54,10 @@ module.exports = function module (moduleOptions) {
staticRoutes = staticRoutes.filter(route => minimatch.match(route))
})

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

if (!this.options.dev) {
// Save static routes
fs.ensureDirSync(path.resolve(this.options.buildDir, 'dist'))
fs.writeJsonSync(jsonStaticRoutesPath, staticRoutes)
Expand Down Expand Up @@ -191,7 +195,7 @@ function routesUnion (staticRoutes, optionsRoutes) {
// Make sure any routes passed as strings are converted to objects with url properties
staticRoutes = staticRoutes.map(ensureRouteIsObject)
optionsRoutes = optionsRoutes.map(ensureRouteIsObject)
// add static routes to options routes, discarding any defined in options
// Add static routes to options routes, discarding any defined in options
return unionBy(optionsRoutes, staticRoutes, 'url')
}

Expand Down

0 comments on commit 67f9462

Please sign in to comment.