Skip to content

Commit

Permalink
fix: different sitemaps sharing same routes cache
Browse files Browse the repository at this point in the history
  • Loading branch information
d3xter-dev committed Nov 19, 2022
1 parent 4b9eb76 commit 8c959fe
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
11 changes: 7 additions & 4 deletions src/runtime/sitemap.gzip.mjs
Expand Up @@ -6,7 +6,7 @@ import { excludeRoutes } from '~sitemap/runtime/routes.mjs'
import { createRoutesCache } from '~sitemap/runtime/cache.mjs'
import { useRuntimeConfig } from '#internal/nitro'

export const globalCache = { routes: null, staticRoutes: null }
export const globalCache = { cache: {}, staticRoutes: null }

export default eventHandler(async(event) => {
const runtimeConfig = useRuntimeConfig()
Expand All @@ -18,18 +18,21 @@ export default eventHandler(async(event) => {
console.log('cant use require in middleware')
}
// eslint-disable-next-line no-new-func,no-eval
const options = eval(' (' + runtimeConfig.sitemap.options + ')')[event.req.url]
const options = eval('(' + runtimeConfig.sitemap.options + ')')[event.req.url]
const staticRoutes = runtimeConfig.sitemap.staticRoutes

// Init cache
if (!globalCache.staticRoutes) {
globalCache.staticRoutes = () => excludeRoutes(options.exclude, staticRoutes)
globalCache.routes = createRoutesCache(globalCache, options)
}

if(!globalCache.cache[event.req.url]) {
globalCache.cache[event.req.url] = createRoutesCache(globalCache, options)
}

try {
// Init sitemap
const routes = await globalCache.routes.get('routes')
const routes = await globalCache.cache[event.req.url].get('routes')
const gzip = await createSitemap(options, routes, options.base, req).toGzip()
// Check cache headers
if (validHttpCache(gzip, options.etag, req, res)) {
Expand Down
11 changes: 7 additions & 4 deletions src/runtime/sitemap.mjs
Expand Up @@ -6,7 +6,7 @@ import { excludeRoutes } from '~sitemap/runtime/routes.mjs'
import { createRoutesCache } from '~sitemap/runtime/cache.mjs'
import { useRuntimeConfig } from '#internal/nitro'

export const globalCache = { routes: null, staticRoutes: null }
export const globalCache = { cache: { }, staticRoutes: null }

export default eventHandler(async (event) => {
const runtimeConfig = useRuntimeConfig()
Expand All @@ -18,18 +18,21 @@ export default eventHandler(async (event) => {
console.log('cant use require in middleware')
}
// eslint-disable-next-line no-new-func,no-eval
const options = eval(' (' + runtimeConfig.sitemap.options + ')')[event.req.url]
const options = eval('(' + runtimeConfig.sitemap.options + ')')[event.req.url]
const staticRoutes = runtimeConfig.sitemap.staticRoutes

// Init cache
if (!globalCache.staticRoutes) {
globalCache.staticRoutes = () => excludeRoutes(options.exclude, staticRoutes)
globalCache.routes = createRoutesCache(globalCache, options)
}

if(!globalCache.cache[event.req.url]) {
globalCache.cache[event.req.url] = createRoutesCache(globalCache, options)
}

try {
// Init sitemap
const routes = await globalCache.routes.get('routes')
const routes = await globalCache.cache[event.req.url].get('routes')
const xml = createSitemap(options, routes, options.base, req).toXML()
// Check cache headers
if (validHttpCache(xml, options.etag, req, res)) {
Expand Down
7 changes: 5 additions & 2 deletions src/runtime/sitemapindex.gzip.mjs
Expand Up @@ -8,7 +8,7 @@ import { excludeRoutes } from '~sitemap/runtime/routes.mjs'
import { createRoutesCache } from '~sitemap/runtime/cache.mjs'
import { useRuntimeConfig } from '#internal/nitro'

export const globalCache = { routes: null, staticRoutes: null }
export const globalCache = { cache: {}, staticRoutes: null }

export default eventHandler((event) => {
const runtimeConfig = useRuntimeConfig()
Expand All @@ -26,7 +26,10 @@ export default eventHandler((event) => {
// Init cache
if (!globalCache.staticRoutes) {
globalCache.staticRoutes = () => excludeRoutes(options.exclude, staticRoutes)
globalCache.routes = createRoutesCache(globalCache, options)
}

if(!globalCache.cache[event.req.url]) {
globalCache.cache[event.req.url] = createRoutesCache(globalCache, options)
}

// Init sitemap index
Expand Down
9 changes: 6 additions & 3 deletions src/runtime/sitemapindex.mjs
Expand Up @@ -7,7 +7,7 @@ import { excludeRoutes } from '~sitemap/runtime/routes.mjs'
import { createRoutesCache } from '~sitemap/runtime/cache.mjs'
import { useRuntimeConfig } from '#internal/nitro'

export const globalCache = { routes: null, staticRoutes: null }
export const globalCache = { cache: { }, staticRoutes: null }

export default eventHandler((event) => {
const runtimeConfig = useRuntimeConfig()
Expand All @@ -19,13 +19,16 @@ export default eventHandler((event) => {
console.log('cant use require in middleware')
}
// eslint-disable-next-line no-new-func,no-eval
const options = eval(' (' + runtimeConfig.sitemap.options + ')')[event.req.url]
const options = eval('(' + runtimeConfig.sitemap.options + ')')[event.req.url]
const staticRoutes = runtimeConfig.sitemap.staticRoutes

// Init cache
if (!globalCache.staticRoutes) {
globalCache.staticRoutes = () => excludeRoutes(options.exclude, staticRoutes)
globalCache.routes = createRoutesCache(globalCache, options)
}

if(!globalCache.cache[event.req.url]) {
globalCache.cache[event.req.url] = createRoutesCache(globalCache, options)
}

// Init sitemap index
Expand Down

0 comments on commit 8c959fe

Please sign in to comment.