- setUnknownIpFactor (0 = old free pass), UNKNOWN_IP_FACTOR exported
- createContentHelpers({ settingsDepth }) and markdown/mcp isPathVisible overrides
- <RenderRichText safeLinks={false}>
- resolveRedirect({ draft: true }) for preview
- isPathVisibleIn(mode, path), pathOf, PathVisibility exported; page route uses it
Co-Authored-By: Claude Opus 5.5 <[email protected]>
130 lines
4.5 KiB
TypeScript
130 lines
4.5 KiB
TypeScript
import type { BasePayload } from 'payload'
|
|
|
|
import type { I18nConfig } from '../i18n/index.js'
|
|
|
|
import { buildLocalizedPath } from '../i18n/index.js'
|
|
|
|
type RedirectCollection = {
|
|
/**
|
|
* Array field holding past slugs, each row `{ slug }`. Default 'slugHistory'
|
|
* — the field `trackSlugHistoryHook` writes. The subfield queried is
|
|
* `<field>.slug`.
|
|
*/
|
|
historyField?: string
|
|
/** Collection slug to search, e.g. 'pages'. */
|
|
slug: string
|
|
/** Current-slug field on the doc. Default 'slug'. */
|
|
slugField?: string
|
|
}
|
|
|
|
type ResolveRedirectArgs = {
|
|
/**
|
|
* Collections to search, in order. Default [{ slug: 'pages' }]. A collection
|
|
* without a slug-history field is skipped safely (no crash), so passing a
|
|
* collection that lacks it costs nothing.
|
|
*/
|
|
collections?: RedirectCollection[]
|
|
config: I18nConfig
|
|
/**
|
|
* Preview (draftMode): also redirect from a never-published draft's old
|
|
* slug. Default false — public redirects never reveal a draft's new slug.
|
|
*/
|
|
draft?: boolean
|
|
/** Home slug map/string, so a renamed homepage still collapses to the root. */
|
|
homeSlug?: Record<string, string> | string
|
|
/** Active locale (the request's locale). */
|
|
locale: string
|
|
payload: BasePayload
|
|
/** Incoming route segments (what 404'd), e.g. ['uslugi', 'stara-nazwa']. */
|
|
segments: string[] | undefined
|
|
}
|
|
|
|
type DocShape = { _status?: unknown; slug?: unknown }
|
|
|
|
/**
|
|
* Resolves a 301/308 destination for a URL that no longer matches any current
|
|
* slug, by looking the old slug up in each collection's slug-history. Returns
|
|
* the current localized path to redirect to, or null when nothing matches.
|
|
*
|
|
* This is the read side of `trackSlugHistoryHook`: that hook records old slugs
|
|
* on save; this turns them back into a redirect so a renamed page never 404s
|
|
* (a real SEO loss). The plugin only COMPUTES the destination — the project
|
|
* issues the redirect, keeping the peer-dep boundary (no next/navigation import
|
|
* here):
|
|
*
|
|
* // app/[locale]/[[...slug]]/page.tsx (or app/[[...slug]] when single-locale)
|
|
* const route = await resolveRoute({ ... })
|
|
* if (!route) {
|
|
* const dest = await resolveRedirect({
|
|
* payload, locale, segments: slug, config: i18nConfig, homeSlug,
|
|
* collections: [{ slug: 'pages' }],
|
|
* })
|
|
* if (dest) permanentRedirect(dest) // from 'next/navigation' → HTTP 308 (SEO-equivalent to 301)
|
|
* notFound()
|
|
* }
|
|
*
|
|
* Multi-segment slugs (an editor slug like 'uslugi/biznesowe') are matched as
|
|
* the joined path, so they work unchanged. Archive ENTRY URLs
|
|
* (/artykuly/<entry>) aren't handled here yet — only flat collections; pass
|
|
* the collections whose slugs map directly to a path.
|
|
*/
|
|
export async function resolveRedirect({
|
|
collections = [{ slug: 'pages' }],
|
|
config,
|
|
draft = false,
|
|
homeSlug,
|
|
locale,
|
|
payload,
|
|
segments,
|
|
}: ResolveRedirectArgs): Promise<null | string> {
|
|
const oldSlug = (segments ?? []).filter(Boolean).join('/')
|
|
// Root never redirects — there's no "old slug" for the locale home.
|
|
if (!oldSlug) {return null}
|
|
|
|
for (const col of collections) {
|
|
const historyField = col.historyField ?? 'slugHistory'
|
|
const slugField = col.slugField ?? 'slug'
|
|
|
|
let currentSlug: string | undefined
|
|
try {
|
|
const res = await payload.find({
|
|
collection: col.slug as never,
|
|
depth: 0,
|
|
limit: 1,
|
|
locale: locale as never,
|
|
pagination: false,
|
|
where: { [`${historyField}.slug`]: { equals: oldSlug } } as never,
|
|
})
|
|
const doc = (res?.docs?.[0] ?? null) as DocShape | null
|
|
// A never-published draft must not be reachable — nor its new slug
|
|
// revealed — through a redirect (same rule as resolveRoute).
|
|
const status = doc?._status
|
|
if (!draft && doc && status !== undefined && status !== null && status !== 'published') {continue}
|
|
if (doc && typeof doc[slugField as keyof DocShape] === 'string') {
|
|
currentSlug = doc[slugField as keyof DocShape] as string
|
|
}
|
|
} catch {
|
|
// Collection has no slug-history field (path not queryable) or the query
|
|
// failed — skip it rather than 500 the 404 path. Same guard as the
|
|
// drafts/_status lesson.
|
|
continue
|
|
}
|
|
|
|
if (!currentSlug || currentSlug === oldSlug) {continue}
|
|
|
|
const dest = buildLocalizedPath({
|
|
config,
|
|
homeSlug,
|
|
locale,
|
|
slugs: { [locale]: currentSlug },
|
|
})
|
|
|
|
// Never redirect a path to itself (would loop).
|
|
if (dest && dest !== `/${oldSlug}` && dest !== `/${locale}/${oldSlug}`) {
|
|
return dest
|
|
}
|
|
}
|
|
|
|
return null
|
|
}
|