import { buildLocalizedPath } from '../i18n/index.js'; /** * 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/) 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 }) { 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; try { const res = await payload.find({ collection: col.slug, depth: 0, limit: 1, locale: locale, pagination: false, where: { [`${historyField}.slug`]: { equals: oldSlug } } }); const doc = res?.docs?.[0] ?? null; if (doc && typeof doc[slugField] === 'string') { currentSlug = doc[slugField]; } } catch { 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; } //# sourceMappingURL=resolveRedirect.js.map