60 lines
2.6 KiB
TypeScript
60 lines
2.6 KiB
TypeScript
import type { BasePayload } from 'payload';
|
|
import type { I18nConfig } 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;
|
|
};
|
|
/**
|
|
* 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 declare function resolveRedirect({ collections, config, homeSlug, locale, payload, segments, }: ResolveRedirectArgs): Promise<null | string>;
|
|
export {};
|