Search leaves out the 404 page: searchContent, the search route, MCP search_content

searchContent / listContent read Site settings once (depth 0, homepage + notFound)
and skip unlistedPageIds; createSearchRoute and the MCP route pass settingsSlug.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-10-29 15:31:43 +01:00
co-authored by Claude Opus 5.5
parent 3645e61f7d
commit 6b2c9b3cc6
7 changed files with 58 additions and 9 deletions
+4 -1
View File
@@ -27,6 +27,8 @@ type CreateSearchRouteArgs = {
* `false` = off (a limiter in front, e.g. Cloudflare WAF rate limiting).
*/
rateLimit?: { max?: number; windowMs?: number } | false
/** SiteSettings global (the assigned 404 page is left out of results). Default 'site-settings'. */
settingsSlug?: string
/** Field holding a richer AI summary (buildSummaryField) to prefer as description. */
summaryField?: string
}
@@ -50,7 +52,7 @@ type CreateSearchRouteArgs = {
* Uses the global Request/Response — no next/server import.
*/
export function createSearchRoute(args: CreateSearchRouteArgs): (req: Request) => Promise<Response> {
const { baseUrl, collection = 'pages', config, defaultLimit = 10, getCachedPayload, homeSlug, isPathVisible, summaryField } = args
const { baseUrl, collection = 'pages', config, defaultLimit = 10, getCachedPayload, homeSlug, isPathVisible, settingsSlug, summaryField } = args
// 120/min: a debounced search-as-you-type box stays well under it; a scraper doesn't.
const rl = args.rateLimit === false ? null : { max: 120, windowMs: 60_000, ...args.rateLimit }
@@ -85,6 +87,7 @@ export function createSearchRoute(args: CreateSearchRouteArgs): (req: Request) =
locale,
payload,
query,
settingsSlug,
summaryField,
})
const shownLocale = locale ?? config.defaultLocale
+29 -3
View File
@@ -3,6 +3,7 @@ import type { BasePayload } from 'payload'
import type { I18nConfig } from '../i18n/index.js'
import { buildLocalizedPath, getDefaultLocale, isValidLocale } from '../i18n/index.js'
import { unlistedPageIds } from '../pages/unlistedPages.js'
export type SearchResult = {
description?: string
@@ -27,6 +28,8 @@ type SearchContentArgs = {
payload: BasePayload
/** Keywords. */
query: string
/** SiteSettings global — the assigned 404 page is left out (unlistedPageIds). Default 'site-settings'. */
settingsSlug?: string
/**
* Field holding a richer AI summary (see buildSummaryField). When set and
* present on a doc, it's preferred over meta.description for the result's
@@ -64,6 +67,7 @@ export async function searchContent({
locale,
payload,
query,
settingsSlug,
summaryField,
}: SearchContentArgs): Promise<SearchResult[]> {
const q = query.trim()
@@ -89,7 +93,25 @@ export async function searchContent({
return []
}
return toResults(docs, { config, homeSlug, locale: loc, max, origin, summaryField })
const unlisted = await readUnlisted(payload, settingsSlug)
return toResults(docs, { config, homeSlug, locale: loc, max, origin, summaryField, unlisted })
}
/**
* Ids of pages with no URL of their own (the assigned 404 page), from Site settings — one small read
* (depth 0, two fields). A missing global means none.
*/
async function readUnlisted(payload: BasePayload, settingsSlug = 'site-settings'): Promise<Set<number | string>> {
try {
const settings = (await payload.findGlobal({
slug: settingsSlug as never,
depth: 0,
select: { homepage: true, notFound: true } as never,
})) as Record<string, unknown>
return unlistedPageIds(settings)
} catch {
return new Set()
}
}
type ToResultsArgs = {
@@ -99,13 +121,15 @@ type ToResultsArgs = {
max: number
origin: string
summaryField?: string
unlisted: Set<number | string>
}
/** Published, indexable docs → results. Shared by searchContent and listContent. */
function toResults(docs: PageRow[], { config, homeSlug, locale, max, origin, summaryField }: ToResultsArgs): SearchResult[] {
function toResults(docs: PageRow[], { config, homeSlug, locale, max, origin, summaryField, unlisted }: ToResultsArgs): SearchResult[] {
const results: SearchResult[] = []
for (const doc of docs) {
if (doc._status && doc._status !== 'published') {continue}
if (unlisted.has(doc.id)) {continue}
if (doc.noindex || doc.meta?.noindex) {continue}
const title = typeof doc.title === 'string' ? doc.title : undefined
const slug = typeof doc.slug === 'string' ? doc.slug : undefined
@@ -145,6 +169,7 @@ export async function listContent({
limit = 50,
locale,
payload,
settingsSlug,
summaryField,
}: Omit<SearchContentArgs, 'query'>): Promise<SearchResult[]> {
const loc = locale && isValidLocale(locale, config) ? locale : getDefaultLocale(config)
@@ -164,5 +189,6 @@ export async function listContent({
} catch {
return []
}
return toResults(docs, { config, homeSlug, locale: loc, max, origin, summaryField })
const unlisted = await readUnlisted(payload, settingsSlug)
return toResults(docs, { config, homeSlug, locale: loc, max, origin, summaryField, unlisted })
}
+1
View File
@@ -246,6 +246,7 @@ export function createMcpRoute(args: CreateMcpRouteArgs) {
locale,
payload,
query,
settingsSlug,
summaryField,
}), locale)
if (results.length === 0) {return `No pages matched "${query}".`}