Construction mode hides pages from Markdown, MCP and search; MCP rejects non-object messages
createContentHelpers exposes isPathVisible (built from getConstructionMode) and passes it to markdownRoute and mcpPOST; createSearchRoute, createMcpRoute and createMarkdownRoute accept it. A null/number/string JSON-RPC message now gets -32600 instead of crashing the route. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
9a72ebfaff
commit
7c05a1dba9
@@ -1,5 +1,6 @@
|
||||
import type { BasePayload } from 'payload'
|
||||
|
||||
import type { PathVisibility } from '../construction/visibility.js'
|
||||
import type { ContentOption } from './types.js'
|
||||
|
||||
import { blocksToMarkdown } from './blocksToMarkdown.js'
|
||||
@@ -187,14 +188,23 @@ function routeOf(
|
||||
* (details only in the server log).
|
||||
*/
|
||||
export function createMarkdownRoute(
|
||||
args: { getCachedPayload: () => Promise<BasePayload> } & CreateMarkdownRendererArgs,
|
||||
args: {
|
||||
getCachedPayload: () => Promise<BasePayload>
|
||||
/** Hide pages the site hides (construction mode) — 404 like an unknown page. */
|
||||
isPathVisible?: PathVisibility
|
||||
} & CreateMarkdownRendererArgs,
|
||||
): { GET: (req: Request, ctx: MarkdownRouteContext) => Promise<Response> } {
|
||||
const { getCachedPayload, ...rendererArgs } = args
|
||||
const { getCachedPayload, isPathVisible, ...rendererArgs } = args
|
||||
const render = createMarkdownRenderer(rendererArgs)
|
||||
return {
|
||||
GET: async (_req, { params }) => {
|
||||
try {
|
||||
const { slug, locale } = routeOf(await params, rendererArgs.config)
|
||||
if (isPathVisible) {
|
||||
const multilingual = (rendererArgs.config?.locales.length ?? 0) > 1
|
||||
const path = `/${[...(multilingual ? [locale] : []), ...(slug ?? [])].join('/')}`
|
||||
if (!(await isPathVisible(path, locale))) {return markdownResponse(null, {})}
|
||||
}
|
||||
const md = await render({ slug, locale, payload: await getCachedPayload() })
|
||||
return markdownResponse(md, { canonical: canonicalOf(rendererArgs.baseUrl, rendererArgs.config, locale, slug) })
|
||||
} catch (err) {
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { BasePayload } from 'payload'
|
||||
import type { I18nConfig } from '../i18n/index.js'
|
||||
import type { ClientIpOptions } from '../shared/rateLimit.js'
|
||||
|
||||
import { type PathVisibility, pathOf } from '../construction/visibility.js'
|
||||
import { clientIp, consumeRateLimit, tooManyRequests } from '../shared/rateLimit.js'
|
||||
import { searchContent } from './searchContent.js'
|
||||
|
||||
@@ -26,6 +27,8 @@ type CreateSearchRouteArgs = {
|
||||
rateLimit?: { max?: number; windowMs?: number } | false
|
||||
/** Field holding a richer AI summary (buildSummaryField) to prefer as description. */
|
||||
summaryField?: string
|
||||
/** Hide pages the site hides (construction mode) — pass `isPathVisible` from createContentHelpers. */
|
||||
isPathVisible?: PathVisibility
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,7 +50,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, summaryField } = args
|
||||
const { baseUrl, collection = 'pages', config, defaultLimit = 10, getCachedPayload, homeSlug, isPathVisible, 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 }
|
||||
|
||||
@@ -73,7 +76,7 @@ export function createSearchRoute(args: CreateSearchRouteArgs): (req: Request) =
|
||||
|
||||
try {
|
||||
const payload = await getCachedPayload()
|
||||
const results = await searchContent({
|
||||
const found = await searchContent({
|
||||
baseUrl,
|
||||
collection,
|
||||
config,
|
||||
@@ -84,6 +87,9 @@ export function createSearchRoute(args: CreateSearchRouteArgs): (req: Request) =
|
||||
query,
|
||||
summaryField,
|
||||
})
|
||||
const shownLocale = locale ?? config.defaultLocale
|
||||
const keep = isPathVisible ? await Promise.all(found.map((r) => isPathVisible(pathOf(r.url), shownLocale))) : null
|
||||
const results = keep ? found.filter((_, i) => keep[i]) : found
|
||||
return Response.json(
|
||||
{ count: results.length, locale: locale ?? config.defaultLocale, query, results },
|
||||
{ headers: { 'cache-control': 'public, max-age=0, s-maxage=60, stale-while-revalidate=600' } },
|
||||
|
||||
Reference in New Issue
Block a user