@nuxtjs/sitemap
Advanced tools
Comparing version 0.0.1 to 0.0.2
109
index.js
@@ -24,50 +24,60 @@ const { Minimatch } = require('minimatch') | ||
// Static Routes | ||
const staticRoutesPromise = new Promise((resolve, reject) => { | ||
this.extendRoutes(routes => { | ||
// Map to path and filter dynamic routes | ||
routes = routes.map(r => r.path).filter(r => r.indexOf(':') === -1) | ||
// sitemap-routes.json is written to dist dir on build mode | ||
const jsonStaticRoutesPath = path.resolve(this.options.buildDir, path.join('dist', 'sitemap-routes.json')) | ||
// Apply excludes | ||
// sitemap.xml is written to static dir on generate mode | ||
const xmlGeneratePath = path.resolve(this.options.srcDir, path.join('static', options.path)) | ||
// Ensure no generated file exists | ||
fs.removeSync(xmlGeneratePath) | ||
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); | ||
// Hydrate cache | ||
cache.get('routes') | ||
} | ||
// Extend build | ||
this.extendBuild((config, { isClient, isServer }) => { | ||
if (isClient) { | ||
let staticRoutes = this.nuxt.routes | ||
// Exclude routes | ||
options.exclude.forEach(pattern => { | ||
const minimatch = new Minimatch(pattern) | ||
minimatch.negate = true | ||
routes = routes.filter(route => minimatch.match(route)) | ||
staticRoutes = staticRoutes.filter(route => minimatch.match(route)) | ||
}) | ||
resolve(routes) | ||
}) | ||
}) | ||
// Create a cache for routes | ||
const cache = new AsyncCache({ | ||
maxAge: options.cacheTime, | ||
load (_, callback) { | ||
Promise.all([staticRoutesPromise, promisifyRoute(options.routes)]) | ||
.then(sources => Array.prototype.concat.apply([], sources)) | ||
.then(routes => uniq(routes)) | ||
.then(routes => { | ||
callback(null, routes) | ||
}) | ||
.catch(err => { | ||
callback(err) | ||
}) | ||
} | ||
}) | ||
cache.get = pify(cache.get) | ||
if (this.options.dev || options.generate) { | ||
// Create a cache for routes | ||
cache = createCache(staticRoutes, options); | ||
} | ||
// sitemap.xml is written to static dir on generate mode | ||
const xmlGeneratePath = path.resolve(this.options.srcDir, path.join('static', options.path)) | ||
if (!this.options.dev) { | ||
if (options.generate) { | ||
// Generate static sitemap.xml | ||
cache.get('routes') | ||
.then(routes => createSitemap(options, routes)) | ||
.then(sitemap => sitemap.toXML()) | ||
.then(xml => fs.writeFile(xmlGeneratePath, xml)) | ||
// TODO on build process only | ||
// Save static routes | ||
fs.ensureDirSync(path.resolve(this.options.buildDir, 'dist')) | ||
fs.writeJsonSync(jsonStaticRoutesPath, staticRoutes) | ||
return | ||
} | ||
// TODO on generate process only and not build process | ||
if (options.generate) { | ||
// Generate static sitemap.xml | ||
cache.get('routes') | ||
.then(routes => createSitemap(options, routes)) | ||
.then(sitemap => sitemap.toXML()) | ||
.then(xml => fs.writeFile(xmlGeneratePath, xml)) | ||
// Ensure no generated file exists | ||
fs.removeSync(xmlGeneratePath) | ||
return | ||
} | ||
} | ||
} | ||
}) | ||
@@ -91,2 +101,23 @@ // Add server middleware | ||
// Initialize a AsyncCache instance for | ||
function createCache (staticRoutes, options) { | ||
let cache = new AsyncCache({ | ||
maxAge: options.cacheTime, | ||
load (_, callback) { | ||
promisifyRoute(options.routes) | ||
.then(routes => staticRoutes.concat(routes)) | ||
.then(routes => uniq(routes)) | ||
.then(routes => { | ||
callback(null, routes) | ||
}) | ||
.catch(err => { | ||
callback(err) | ||
}) | ||
} | ||
}) | ||
cache.get = pify(cache.get) | ||
return cache | ||
} | ||
// Initialize a fresh sitemap instance | ||
@@ -113,3 +144,3 @@ function createSitemap (options, routes, req) { | ||
// Borrowed from nuxt/common/utils | ||
// Borrowed from nuxt/common/utils | ||
function promisifyRoute (fn) { | ||
@@ -116,0 +147,0 @@ // If routes is an array |
{ | ||
"name": "@nuxtjs/sitemap", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -48,3 +48,3 @@ # Sitemap | ||
See as well the [#routes](routes) examples below. | ||
See as well the [routes](#routes-1) examples below. | ||
@@ -73,3 +73,3 @@ ### `path` | ||
This option is only effective when `generate` is `false`. | ||
Pleae note that after each invalidation, `routes` will be evalouated again. (See [#routes](routes) section) | ||
Pleae note that after each invalidation, `routes` will be evalouated again. (See [routes](#routes-1) section) | ||
@@ -76,0 +76,0 @@ ## Routes |
8972
4
140