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

feat: Support option to access req.headers.host in dynamic routes function #218

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
14 changes: 11 additions & 3 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand Down