diff --git a/lib/cache.js b/lib/cache.js index ef613f9..18c5c07 100644 --- a/lib/cache.js +++ b/lib/cache.js @@ -13,16 +13,24 @@ const unionBy = require('lodash.unionby') function createRoutesCache(globalCache, options) { const cache = new AsyncCache({ maxAge: options.cacheTime, - async load(_, callback) { + async load(key, callback) { try { - let routes = await promisifyRoute(options.routes) + let routes; + if (options.dynamicHost) { + // strip away 'routes--' string to parse host + const host = key.replace('routes--', '') + // make host param available in options route function + routes = await options.routes(host) + } else { + routes = await promisifyRoute(options.routes) + } routes = joinRoutes(globalCache.staticRoutes ? globalCache.staticRoutes() : [], routes) callback(null, routes) } catch (err) { /* istanbul ignore next */ callback(err) } - }, + } }) cache.get = promisify(cache.get) diff --git a/lib/middleware.js b/lib/middleware.js index f9f7f12..07acb16 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -68,7 +68,7 @@ function registerSitemap(options, globalCache, nuxtInstance, depth = 0) { async handler(req, res, next) { try { // Init sitemap - const routes = await cache.routes.get('routes') + const routes = await cache.routes.get('routes--' + req.headers.host) const gzip = await createSitemap(options, routes, base, req).toGzip() // Check cache headers if (validHttpCache(gzip, options.etag, req, res)) { @@ -96,7 +96,7 @@ function registerSitemap(options, globalCache, nuxtInstance, depth = 0) { async handler(req, res, next) { try { // Init sitemap - const routes = await cache.routes.get('routes') + const routes = await cache.routes.get('routes--' + req.headers.host) const xml = await createSitemap(options, routes, base, req).toXML() // Check cache headers if (validHttpCache(xml, options.etag, req, res)) {