Skip to content

Commit

Permalink
feat: add routes from routeRules and nitro prerender routes
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Jul 28, 2023
1 parent 85e5bac commit ffc07c8
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 8 deletions.
15 changes: 14 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import type {
} from './runtime/types'
import { setupPrerenderHandler } from './prerender'
import {
convertNuxtPagesToSitemapEntries,
convertNuxtPagesToSitemapEntries, generateExtraRoutesFromNuxtConfig,
// @todo use nuxt kit utils after 3.6.5
getNuxtModuleOptions,
getNuxtModuleVersion,
Expand Down Expand Up @@ -404,6 +404,19 @@ declare module 'nitropack/dist/runtime/types' {
})
}

nuxt.hooks.hook('nitro:config', (nitroConfig) => {
// @ts-expect-error runtime types
nitroConfig.virtual['#nuxt-simple-sitemap/extra-routes.mjs'] = async () => {
const { prerenderUrls, routeRules } = generateExtraRoutesFromNuxtConfig()
return [
// no wild cards supported
`const routeRules = ${JSON.stringify(routeRules)}`,
`const prerenderUrls = ${JSON.stringify(prerenderUrls)}`,
'export default { routeRules, prerenderUrls }',
].join('\n')
}
})

// always add the styles
if (config.xsl === '/__sitemap__/style.xsl') {
addServerHandler({
Expand Down
2 changes: 2 additions & 0 deletions src/prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
} from './runtime/types'
import type { ModuleOptions } from './module'
import { resolveAsyncDataSources } from './runtime/sitemap/entries'
import { generateExtraRoutesFromNuxtConfig } from './utils'

export function setupPrerenderHandler(moduleConfig: ModuleOptions, buildTimeMeta: ModuleComputedOptions, pagesPromise: Promise<SitemapEntryInput[]>, nuxt: Nuxt = useNuxt()) {
const { resolve } = createResolver(import.meta.url)
Expand Down Expand Up @@ -104,6 +105,7 @@ export function setupPrerenderHandler(moduleConfig: ModuleOptions, buildTimeMeta
canonicalUrlResolver: createSitePathResolver({ canonical: true, absolute: true, withBase: true }),
relativeBaseUrlResolver: createSitePathResolver({ absolute: false, withBase: true }),
buildTimeMeta,
extraRoutes: generateExtraRoutesFromNuxtConfig(),
getRouteRulesForPath: routeMatcher,
callHook,
prerenderUrls,
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/middleware/[sitemap]-sitemap.xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { getRouteRulesForPath } from '#internal/nitro/route-rules'
// @ts-expect-error untyped
import pages from '#nuxt-simple-sitemap/pages.mjs'

// @ts-expect-error untyped
import extraRoutes from '#nuxt-simple-sitemap/extra-routes.mjs'

export default defineEventHandler(async (e) => {
const path = parseURL(e.path).pathname
if (!path.endsWith('-sitemap.xml'))
Expand Down Expand Up @@ -41,6 +44,7 @@ export default defineEventHandler(async (e) => {
sitemapName,
...moduleConfig.sitemaps[sitemapName],
},
extraRoutes,
canonicalUrlResolver: createSitePathResolver(e, { canonical: isShowingCanonical || !process.dev, absolute: true, withBase: true }),
relativeBaseUrlResolver: createSitePathResolver(e, { absolute: false, withBase: true }),
moduleConfig,
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/routes/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { createSitePathResolver, useRuntimeConfig } from '#imports'

// @ts-expect-error untyped
import pages from '#nuxt-simple-sitemap/pages.mjs'

// @ts-expect-error untyped
import extraRoutes from '#nuxt-simple-sitemap/extra-routes.mjs'
import { getRouteRulesForPath } from '#internal/nitro/route-rules'

export default defineEventHandler(async (e) => {
Expand All @@ -26,6 +29,7 @@ export default defineEventHandler(async (e) => {
sitemap,
buildTimeMeta,
getRouteRulesForPath,
extraRoutes,
canonicalUrlResolver: createSitePathResolver(e, { canonical: !process.dev, absolute: true, withBase: true }),
relativeBaseUrlResolver: createSitePathResolver(e, { absolute: false, withBase: true }),
pages,
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/routes/sitemap.xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { getRouteRulesForPath } from '#internal/nitro/route-rules'
// @ts-expect-error untyped
import pages from '#nuxt-simple-sitemap/pages.mjs'

// @ts-expect-error untyped
import extraRoutes from '#nuxt-simple-sitemap/extra-routes.mjs'

export default defineEventHandler(async (e) => {
const { moduleConfig, buildTimeMeta } = useRuntimeConfig()['nuxt-simple-sitemap'] as any as ModuleRuntimeConfig
// we need to check if we're rendering multiple sitemaps from the index sitemap
Expand All @@ -31,6 +34,7 @@ export default defineEventHandler(async (e) => {

sitemap = await buildSitemap({
moduleConfig,
extraRoutes,
buildTimeMeta,
canonicalUrlResolver: createSitePathResolver(e, { canonical: isShowingCanonical || !process.dev, absolute: true, withBase: true }),
relativeBaseUrlResolver: createSitePathResolver(e, { absolute: false, withBase: true }),
Expand Down
5 changes: 5 additions & 0 deletions src/runtime/routes/sitemap_index.xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ import { getRouteRulesForPath } from '#internal/nitro/route-rules'

// @ts-expect-error untyped
import pages from '#nuxt-simple-sitemap/pages.mjs'

import { useNitroApp } from '#internal/nitro'

// @ts-expect-error untyped
import extraRoutes from '#nuxt-simple-sitemap/extra-routes.mjs'

export default defineEventHandler(async (e) => {
const { moduleConfig, buildTimeMeta } = useRuntimeConfig()['nuxt-simple-sitemap'] as any as ModuleRuntimeConfig

Expand All @@ -28,6 +32,7 @@ export default defineEventHandler(async (e) => {
buildTimeMeta,
getRouteRulesForPath,
callHook,
extraRoutes,
canonicalUrlResolver: createSitePathResolver(e, { canonical: isShowingCanonical || !process.dev, absolute: true, withBase: true }),
relativeBaseUrlResolver: createSitePathResolver(e, { absolute: false, withBase: true }),
pages,
Expand Down
18 changes: 16 additions & 2 deletions src/runtime/sitemap/entries/sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,22 @@ export async function resolveAsyncDataSources(input: BuildSitemapInput | BuildSi
})
}

if (input.extraRoutes.prerenderUrls.length) {
entries.push({
context: 'nuxt-config.nitro-prerender',
urls: input.extraRoutes.prerenderUrls,
})
}

if (input.extraRoutes.routeRules.length) {
entries.push({
context: 'nuxt-config.route-rules',
urls: input.extraRoutes.routeRules,
})
}

entries.push({
context: 'nuxt-config',
context: 'nuxt-config.module',
path: 'urls',
urls: await resolveUrls(input.moduleConfig.urls),
})
Expand Down Expand Up @@ -85,7 +99,7 @@ export async function resolveAsyncDataSources(input: BuildSitemapInput | BuildSi
async function loadSitemapSources(sitemap: SitemapRoot) {
if (sitemap.urls) {
entries.push({
context: 'nuxt-config',
context: 'nuxt-config.module',
path: `sitemaps.${sitemap.sitemapName}.urls`,
urls: await resolveUrls(sitemap.urls),
})
Expand Down
11 changes: 6 additions & 5 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type MaybePromise<T> = T | Promise<T>
export type SitemapEntryInput = SitemapEntry | string

export interface DataSourceResult {
context: 'pages' | 'nuxt-config' | 'api' | 'prerender'
context: 'pages' | 'nuxt-config.module' | 'nuxt-config.nitro-prerender' | 'nuxt-config.route-rules' | 'api' | 'prerender'
urls: SitemapEntryInput[]
path?: string
error?: Error | string
Expand Down Expand Up @@ -84,10 +84,6 @@ export type Changefreq =
| 'never'

export interface SitemapEntry {
/**
* @deprecated use `loc`
*/
url?: string
loc: string
lastmod?: string | Date
changefreq?: Changefreq
Expand All @@ -96,6 +92,10 @@ export interface SitemapEntry {
news?: GoogleNewsEntry
images?: Array<ImageEntry>
videos?: Array<VideoEntry>
/**
* @deprecated use `loc`
*/
url?: string
}

export interface AlternativeEntry {
Expand Down Expand Up @@ -158,6 +158,7 @@ export interface BuildSitemapInput {
canonicalUrlResolver: (path: string) => string
relativeBaseUrlResolver: (path: string) => string
callHook?: (ctx: SitemapRenderCtx) => Promise<void>
extraRoutes: { routeRules: string[]; prerenderUrls: string[] }
prerenderUrls?: SitemapEntryInput[]
pages: SitemapEntryInput[]
}
Expand Down
18 changes: 18 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,21 @@ export async function hasNuxtModuleCompatibility(module: string | NuxtModule, se
includePrerelease: true,
})
}

export function generateExtraRoutesFromNuxtConfig(nuxt: Nuxt = useNuxt()) {
const routeRules = Object.entries(nuxt.options.routeRules || {})
.filter(([k, v]) => {
// make sure key doesn't use a wildcard and its not for a file
if (k.includes('*') || k.includes('.'))
return false
if (typeof v.index === 'boolean' && !v.index)
return false
// make sure that we're not redirecting
return !v.redirect
})
.map(([k]) => k)
// don't support files
const prerenderUrls = (nuxt.options.nitro.prerender?.routes || [])
.filter(p => p && !p.includes('.')) as string[]
return { routeRules, prerenderUrls }
}

0 comments on commit ffc07c8

Please sign in to comment.