Skip to content

Commit

Permalink
fix: support /api vue pages
Browse files Browse the repository at this point in the history
Fixes #232
  • Loading branch information
harlan-zw committed Mar 4, 2024
1 parent 7672a0c commit 9d4746e
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .playground/nuxt.config.ts
Expand Up @@ -63,6 +63,7 @@ export default defineNuxtConfig({
'/should-be-in-sitemap',
'/foo.bar/',
'/test.doc',
'/api/prerendered',
],
failOnError: false,
},
Expand Down Expand Up @@ -141,6 +142,9 @@ export default defineNuxtConfig({
},
},
routeRules: {
'/api/prerendered': {
prerender: true,
},
'/secret': {
index: false,
},
Expand Down
3 changes: 3 additions & 0 deletions .playground/pages/api/foo.vue
@@ -0,0 +1,3 @@
<template>
<div>hello world</div>
</template>
5 changes: 5 additions & 0 deletions .playground/server/api/prerendered.ts
@@ -0,0 +1,5 @@
import { defineEventHandler } from 'h3'

export default defineEventHandler(async () => {
return { foo: 'bar' }
})
9 changes: 7 additions & 2 deletions src/module.ts
Expand Up @@ -80,7 +80,7 @@ export default defineNuxtModule<ModuleOptions>({
defaults: {},
// index sitemap options filtering
include: [],
exclude: ['/_nuxt/**', '/api/**'],
exclude: ['/_nuxt/**', '/_**'],
// sources
sources: [],
excludeAppSources: [],
Expand Down Expand Up @@ -516,7 +516,12 @@ declare module 'vue-router' {
const prerenderUrlsFinal = [
...prerenderUrls,
...((await nitroPromise)._prerenderedRoutes || [])
.filter(r => (!r.fileName || r.fileName.endsWith('.html')) && !r.route.endsWith('.html') && !r.route.startsWith('/api/'))
.filter((r) => {
// make sure it's not excluded
// search for just noindex in a robots meta tag
const isRobotsBlocking = r.contents?.match(/<meta[^>]+name="robots"[^>]+content="[^"]*noindex[^"]*"[^>]*>/)
return r.contentType === 'text/html' && !isRobotsBlocking
})
.map(r => r._sitemap),
]
const pageSource = convertNuxtPagesToSitemapEntries(await pagesPromise, {
Expand Down
2 changes: 1 addition & 1 deletion src/util/i18n.ts
Expand Up @@ -13,7 +13,7 @@ export interface StrategyProps {

export function splitPathForI18nLocales(path: FilterInput, autoI18n: AutoI18nConfig) {
const locales = autoI18n.strategy === 'prefix_except_default' ? autoI18n.locales.filter(l => l.code !== autoI18n.defaultLocale) : autoI18n.locales
if (typeof path !== 'string' || path.startsWith('/api') || path.startsWith('/_nuxt'))
if (typeof path !== 'string' || path.startsWith('/_'))
return path
const match = splitForLocales(path, locales.map(l => l.code))
const locale = match[0]
Expand Down
4 changes: 3 additions & 1 deletion src/util/nuxtSitemap.ts
Expand Up @@ -155,6 +155,7 @@ export function convertNuxtPagesToSitemapEntries(pages: NuxtPage[], config: Nuxt
}

export function generateExtraRoutesFromNuxtConfig(nuxt: Nuxt = useNuxt()) {
const filterForValidPage = p => p && !extname(p) && !p.startsWith('/api/') && !p.startsWith('/_')
const routeRules = Object.entries(nuxt.options.routeRules || {})
.filter(([k, v]) => {
// make sure key doesn't use a wildcard and its not for a file
Expand All @@ -166,8 +167,9 @@ export function generateExtraRoutesFromNuxtConfig(nuxt: Nuxt = useNuxt()) {
return !v.redirect
})
.map(([k]) => k)
.filter(filterForValidPage)
// don't support files
const prerenderUrls = (nuxt.options.nitro.prerender?.routes || [])
.filter(p => p && !extname(p) && !p.startsWith('/api/')) as string[]
.filter(filterForValidPage) as string[]
return { routeRules, prerenderUrls }
}

0 comments on commit 9d4746e

Please sign in to comment.