From 3abf0f2a460d056d645717e0aa9445e45fe61c99 Mon Sep 17 00:00:00 2001 From: rylax <33907164+rylax@users.noreply.github.com> Date: Tue, 17 Aug 2021 18:58:04 +0100 Subject: [PATCH 1/2] enable option to access host in dynamic routes function --- lib/cache.js | 13 ++++++++++--- lib/middleware.js | 4 ++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/cache.js b/lib/cache.js index ef613f9..2149021 100644 --- a/lib/cache.js +++ b/lib/cache.js @@ -13,16 +13,23 @@ 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)) { From 25287b063c45eca88c6b6607dac550cb615dba36 Mon Sep 17 00:00:00 2001 From: rylax <33907164+rylax@users.noreply.github.com> Date: Thu, 26 Aug 2021 14:17:13 +0100 Subject: [PATCH 2/2] restore } to close load function --- lib/cache.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/cache.js b/lib/cache.js index 2149021..18c5c07 100644 --- a/lib/cache.js +++ b/lib/cache.js @@ -30,6 +30,7 @@ function createRoutesCache(globalCache, options) { /* istanbul ignore next */ callback(err) } + } }) cache.get = promisify(cache.get)