Added module Links
This commit is contained in:
@@ -30,7 +30,13 @@ export const AI_TRAINING_BOTS = [
|
||||
|
||||
type AiBotsPolicy = 'allow' | 'block-training' | 'disallow' | false
|
||||
|
||||
type BuildRobotsArgs = {
|
||||
export type RobotsOptions = {
|
||||
/**
|
||||
* Admin path to disallow. Default '/admin'. With a hidden panel
|
||||
* (ipalKit adminRoute) pass `false`: robots.txt is public, so listing the
|
||||
* path would reveal it — the admin is noindex anyway.
|
||||
*/
|
||||
adminRoute?: false | string
|
||||
/** Override the assistant/search bot list. */
|
||||
aiAssistantAgents?: readonly string[]
|
||||
/**
|
||||
@@ -47,14 +53,29 @@ type BuildRobotsArgs = {
|
||||
aiBots?: AiBotsPolicy
|
||||
/** Override the training crawler list. */
|
||||
aiTrainingAgents?: readonly string[]
|
||||
/** Absolute origin, e.g. 'https://example.com'. */
|
||||
baseUrl: string
|
||||
/** Paths to disallow. Defaults to Payload's admin and API. */
|
||||
/**
|
||||
* Full disallow list — REPLACES the defaults ([adminRoute, '/api']). Prefer
|
||||
* `extraDisallow` / `disallowParams`, which add to the defaults instead.
|
||||
*/
|
||||
disallow?: string[]
|
||||
/**
|
||||
* Query parameters whose URLs crawlers should skip (thin/duplicate pages that
|
||||
* waste crawl budget), e.g. ['s', 'meter', 'utm_source']. Each becomes
|
||||
* `/*?name=` and `/*&name=` (the parameter may not be first).
|
||||
* Don't list `page` — paginated archives are real, canonical-to-self pages.
|
||||
*/
|
||||
disallowParams?: string[]
|
||||
/** Paths added to the default disallow list, e.g. ['/koszyk']. */
|
||||
extraDisallow?: string[]
|
||||
/** Whether to advertise the sitemap. Defaults to true. */
|
||||
sitemap?: boolean
|
||||
}
|
||||
|
||||
type BuildRobotsArgs = {
|
||||
/** Absolute origin, e.g. 'https://example.com'. */
|
||||
baseUrl: string
|
||||
} & RobotsOptions
|
||||
|
||||
/**
|
||||
* Shape for Next's `app/robots.ts`. `rules` may be a single rule or an array
|
||||
* (Next accepts both) — it becomes an array when AI-bot rules are added.
|
||||
@@ -86,21 +107,32 @@ export type RobotsRules = {
|
||||
* PerplexityBot, Google-Extended and friends. To keep AI answers but opt out of
|
||||
* model training, use `aiBots: 'block-training'`. To block them all, 'disallow'.
|
||||
*
|
||||
* Pass custom disallow to block parametrized/thin URLs that waste crawl budget:
|
||||
* Block parametrized/thin URLs that waste crawl budget — added to the defaults:
|
||||
* ```ts
|
||||
* buildRobots({ baseUrl, disallow: ['/admin', '/api', '/*?meter=*', '/*?s=*'] })
|
||||
* buildRobots({ baseUrl, disallowParams: ['s', 'meter'] })
|
||||
* // Disallow: /admin, /api, /*?s=, /*&s=, /*?meter=, /*&meter=
|
||||
* ```
|
||||
*/
|
||||
export function buildRobots({
|
||||
adminRoute = '/admin',
|
||||
aiAssistantAgents = AI_ASSISTANT_BOTS,
|
||||
aiBots = 'allow',
|
||||
aiTrainingAgents = AI_TRAINING_BOTS,
|
||||
baseUrl,
|
||||
disallow = ['/admin', '/api'],
|
||||
disallow: disallowOverride,
|
||||
disallowParams = [],
|
||||
extraDisallow = [],
|
||||
sitemap = true,
|
||||
}: BuildRobotsArgs): RobotsRules {
|
||||
const origin = baseUrl.replace(/\/$/, '')
|
||||
|
||||
const defaults = [...(adminRoute ? [adminRoute] : []), '/api']
|
||||
const paramRules = disallowParams
|
||||
.map((p) => p.trim())
|
||||
.filter(Boolean)
|
||||
.flatMap((p) => [`/*?${p}=`, `/*&${p}=`])
|
||||
const disallow = [...new Set([...(disallowOverride ?? defaults), ...extraDisallow, ...paramRules])]
|
||||
|
||||
const baseRule: RobotRule = { allow: '/', disallow, userAgent: '*' }
|
||||
|
||||
const aiRules: RobotRule[] = []
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { BasePayload } from 'payload'
|
||||
|
||||
import type { ContentOption } from '../content/index.js'
|
||||
import type { ContentOption, ResolvedRoute } from '../content/index.js'
|
||||
import type { I18nConfig } from '../i18n/index.js'
|
||||
import type { PageMetadata } from './buildMetadata.js'
|
||||
import type { SeoMeta } from './types.js'
|
||||
@@ -22,6 +22,19 @@ type CreatePageMetadataArgs = {
|
||||
* omit it and only pages are handled.
|
||||
*/
|
||||
content?: ContentOption
|
||||
/**
|
||||
* Request-cached loaders (createContentHelpers passes its own), so metadata
|
||||
* reuses the SAME settings read and route resolution as the layout and the
|
||||
* page component instead of querying again. Omit → plain queries.
|
||||
*/
|
||||
loaders?: {
|
||||
getSettings?: (locale: string) => Promise<null | Record<string, unknown>>
|
||||
resolveRoute?: (
|
||||
locale: string,
|
||||
segments: string[] | undefined,
|
||||
page: number,
|
||||
) => Promise<null | ResolvedRoute>
|
||||
}
|
||||
/** SiteSettings global slug. Defaults to 'site-settings'. */
|
||||
settingsSlug?: string
|
||||
/** Field on SiteSettings holding the site name. Defaults to 'siteName'. */
|
||||
@@ -89,6 +102,7 @@ export function createPageMetadata(args: CreatePageMetadataArgs) {
|
||||
collection = 'pages',
|
||||
config,
|
||||
content,
|
||||
loaders,
|
||||
settingsSlug = 'site-settings',
|
||||
siteNameField = 'siteName',
|
||||
} = args
|
||||
@@ -99,7 +113,15 @@ export function createPageMetadata(args: CreatePageMetadataArgs) {
|
||||
page,
|
||||
payload,
|
||||
}: PageMetadataContext): Promise<PageMetadata> {
|
||||
const site = await readSiteMetaConfig({ locale, payload, settingsSlug, siteNameField })
|
||||
const settings = loaders?.getSettings ? await loaders.getSettings(locale) : undefined
|
||||
const site = await readSiteMetaConfig({
|
||||
locale,
|
||||
pagesSlug: collection,
|
||||
payload,
|
||||
settings,
|
||||
settingsSlug,
|
||||
siteNameField,
|
||||
})
|
||||
|
||||
const base = {
|
||||
baseUrl,
|
||||
@@ -111,15 +133,18 @@ export function createPageMetadata(args: CreatePageMetadataArgs) {
|
||||
siteName: site.siteName,
|
||||
}
|
||||
|
||||
const route = await resolveRoute({
|
||||
content,
|
||||
locale,
|
||||
page,
|
||||
pagesSlug: collection,
|
||||
payload,
|
||||
segments: slug,
|
||||
settingsSlug,
|
||||
})
|
||||
const route = loaders?.resolveRoute
|
||||
? await loaders.resolveRoute(locale, slug, page ?? 1)
|
||||
: await resolveRoute({
|
||||
content,
|
||||
locale,
|
||||
page,
|
||||
pagesSlug: collection,
|
||||
payload,
|
||||
segments: slug,
|
||||
settings,
|
||||
settingsSlug,
|
||||
})
|
||||
|
||||
// Unknown route (the page component will 404) — still return something
|
||||
// coherent rather than throwing during metadata generation.
|
||||
|
||||
@@ -12,7 +12,7 @@ export type { PageMetadata } from './buildMetadata.js'
|
||||
export { buildNoindexField } from './buildNoindexField.js'
|
||||
export { buildOrganizationJsonLd } from './buildOrganizationJsonLd.js'
|
||||
export { AI_ASSISTANT_BOTS, AI_TRAINING_BOTS, buildRobots } from './buildRobots.js'
|
||||
export type { RobotsRules } from './buildRobots.js'
|
||||
export type { RobotsOptions, RobotsRules } from './buildRobots.js'
|
||||
export { buildServiceJsonLd } from './buildServiceJsonLd.js'
|
||||
export { buildSitemapEntries } from './buildSitemapEntries.js'
|
||||
export type { SitemapEntry } from './buildSitemapEntries.js'
|
||||
|
||||
@@ -35,22 +35,29 @@ export type SiteMetaConfig = {
|
||||
*/
|
||||
export async function readSiteMetaConfig({
|
||||
locale,
|
||||
pagesSlug = 'pages',
|
||||
payload,
|
||||
settings: preloaded,
|
||||
settingsSlug = 'site-settings',
|
||||
siteNameField = 'siteName',
|
||||
}: {
|
||||
locale: string
|
||||
/** Collection holding the homepage. Default 'pages'. */
|
||||
pagesSlug?: string
|
||||
payload: BasePayload
|
||||
/** SiteSettings already read for this locale (depth ≥ 1) — skips the read. */
|
||||
settings?: null | Record<string, unknown>
|
||||
settingsSlug?: string
|
||||
siteNameField?: string
|
||||
}): Promise<SiteMetaConfig> {
|
||||
// depth 1 populates the homepage relationship, so its slug is available
|
||||
// without a second query.
|
||||
const settings = (await payload.findGlobal({
|
||||
slug: settingsSlug as never,
|
||||
depth: 1,
|
||||
locale: locale as never,
|
||||
})) as SettingsShape
|
||||
const settings = (preloaded ??
|
||||
(await payload.findGlobal({
|
||||
slug: settingsSlug as never,
|
||||
depth: 1,
|
||||
locale: locale as never,
|
||||
}))) as SettingsShape
|
||||
|
||||
const homepage =
|
||||
settings.homepage && typeof settings.homepage === 'object' ? settings.homepage : null
|
||||
@@ -64,7 +71,7 @@ export async function readSiteMetaConfig({
|
||||
try {
|
||||
const homeDoc = (await payload.findByID({
|
||||
id: homepage.id,
|
||||
collection: 'pages' as never,
|
||||
collection: pagesSlug as never,
|
||||
depth: 0,
|
||||
locale: 'all' as never,
|
||||
})) as { slug?: unknown }
|
||||
|
||||
Reference in New Issue
Block a user