diff --git a/docs/content.md b/docs/content.md index 563f53c..0b60a6b 100644 --- a/docs/content.md +++ b/docs/content.md @@ -221,7 +221,8 @@ Zwraca docelową ścieżkę (zlokalizowaną, z collapsowaniem home przez `homeSl albo `null`. Plugin tylko liczy — `permanentRedirect` woła projekt (plugin nie importuje `next/navigation`). Każda kolekcja bez pola historii jest pomijana bezpiecznie. `collections[].historyField` (domyślnie `'slugHistory'`) i -`slugField` (domyślnie `'slug'`) konfigurowalne. +`slugField` (domyślnie `'slug'`) konfigurowalne. Dokument nigdy nieopublikowany (`_status: 'draft'`) +nie przekierowuje — jego nowy slug nie wycieka przez redirect. Ograniczenia: tylko kolekcje „płaskie" (slug → ścieżka). Wpisy w archiwach (`/artykuly/`) nie są jeszcze obsługiwane. Jeśli `slug` jest `localized`, diff --git a/src/modules/content/resolveRedirect.ts b/src/modules/content/resolveRedirect.ts index 1343543..ac03f4b 100644 --- a/src/modules/content/resolveRedirect.ts +++ b/src/modules/content/resolveRedirect.ts @@ -34,7 +34,7 @@ type ResolveRedirectArgs = { segments: string[] | undefined } -type DocShape = { slug?: unknown } +type DocShape = { _status?: unknown; slug?: unknown } /** * Resolves a 301/308 destination for a URL that no longer matches any current @@ -90,6 +90,10 @@ export async function resolveRedirect({ 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 (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 } diff --git a/test/redirectDrafts.test.ts b/test/redirectDrafts.test.ts new file mode 100644 index 0000000..0a228af --- /dev/null +++ b/test/redirectDrafts.test.ts @@ -0,0 +1,25 @@ +import type { BasePayload } from 'payload' + +import { describe, expect, test, vi } from 'vitest' + +import { resolveRedirect } from '../src/modules/content/resolveRedirect.js' + +const config = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'PL' }] } +const payloadWith = (doc: object) => ({ find: vi.fn(async () => ({ docs: [doc] })) }) as unknown as BasePayload + +describe('old-slug redirects and drafts', () => { + test('a published page redirects from its old slug', async () => { + const payload = payloadWith({ _status: 'published', slug: 'nowa-oferta' }) + expect(await resolveRedirect({ config, locale: 'pl', payload, segments: ['oferta'] })).toBe('/nowa-oferta') + }) + + test('a never-published draft does not redirect (its new slug stays private)', async () => { + const payload = payloadWith({ _status: 'draft', slug: 'tajny-projekt' }) + expect(await resolveRedirect({ config, locale: 'pl', payload, segments: ['oferta'] })).toBeNull() + }) + + test('collections without drafts still redirect', async () => { + const payload = payloadWith({ slug: 'kontakt-2' }) + expect(await resolveRedirect({ config, locale: 'pl', payload, segments: ['kontakt'] })).toBe('/kontakt-2') + }) +})