Added healhtRoute, honeypot, ConsentScript, asserEnv
This commit is contained in:
@@ -2,6 +2,7 @@ export { buildArchiveFields } from './archiveFields.js'
|
||||
export { getArchiveEntries } from './getArchiveEntries.js'
|
||||
export type { ArchiveEntries } from './getArchiveEntries.js'
|
||||
export { buildArchivePath, buildEntryPath, parsePageParam } from './paths.js'
|
||||
export { resolveRedirect } from './resolveRedirect.js'
|
||||
export { resolveRoute } from './resolveRoute.js'
|
||||
export type { ResolvedRoute } from './resolveRoute.js'
|
||||
export type { ContentCollectionOption, ContentOption } from './types.js'
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
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
|
||||
/** 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 = { 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,
|
||||
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
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user