From 561a9e2bb7e524078186dd82dc5607b4175c3129 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Smoli=C5=84ski?= Date: Thu, 22 Oct 2026 17:02:07 +0200 Subject: [PATCH] Site Settings read at depth 1; archive page re-fetched at page depth getSettings ran findGlobal at depth 2 on every request, pulling the relations of every page a project field links to (menus). Depth 1 keeps slug/title. The archive page itself was rendered from the settings copy, which maxDepth caps at 1, so its media never populated; resolveRoute now re-fetches it at depth 2 like the homepage. Co-Authored-By: Claude Opus 5.5 --- docs/content.md | 6 ++ src/modules/content/resolveRoute.ts | 14 +++- src/modules/frontend/createContentHelpers.ts | 7 +- test/settingsDepth.test.ts | 84 ++++++++++++++++++++ 4 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 test/settingsDepth.test.ts diff --git a/docs/content.md b/docs/content.md index ebeb2ba..721e8c1 100644 --- a/docs/content.md +++ b/docs/content.md @@ -104,6 +104,12 @@ fabryce i jest współdzielona — dlatego to fabryka, nie luźne funkcje. Bez t `cache()` nie dedupikowałby między helperami, a Next woła generateMetadata i komponent strony osobno. +`getSettings(locale)` czyta Site Settings z **`depth: 1`** — relacje (logo, strony w menu, +strony systemowe) przychodzą jako dokumenty (`slug`, `title`), ale **ich** relacje już jako ID. +Strona, którą `resolveRoute` renderuje (homepage, archiwum, zwykła strona), jest pobierana +osobno z `depth: 2`. Potrzebujesz głębiej w layoucie (np. obraz strony podlinkowanej w menu)? +Pobierz ten jeden dokument sam, zamiast podnosić głębokość całego globala na każdym żądaniu. + `resolveRoute` z fabryki przyjmuje `(locale, segments, page)` — rozstrzyga trasę. Pobranie wpisów to osobna funkcja `getEntries` (patrz niżej), bo routing i pobieranie danych to dwie różne odpowiedzialności. diff --git a/src/modules/content/resolveRoute.ts b/src/modules/content/resolveRoute.ts index d7afe46..46efc0b 100644 --- a/src/modules/content/resolveRoute.ts +++ b/src/modules/content/resolveRoute.ts @@ -143,12 +143,22 @@ export async function resolveRoute({ if (archiveDoc.slug !== first) {continue} if (!isPublic(archiveDoc, draft)) {return null} - // /pl/artykuly → the archive page itself. + // /pl/artykuly → the archive page itself. Settings hold it shallow (depth + // 1); re-fetch it at page depth so its blocks render like any other page. if (rest.length === 0) { + const doc = await payload.findByID({ + id: archiveDoc.id, + collection: pagesSlug as never, + depth: 2, + disableErrors: true, + draft, + locale: locale as never, + }) + if (!doc || !isPublic(doc, draft)) {return null} return { type: 'archive', collection: collection.slug, - doc: archiveDoc as Record, + doc: doc as Record, page, perPage: collection.perPage ?? 10, } diff --git a/src/modules/frontend/createContentHelpers.ts b/src/modules/frontend/createContentHelpers.ts index 584c8b5..2a8ec21 100644 --- a/src/modules/frontend/createContentHelpers.ts +++ b/src/modules/frontend/createContentHelpers.ts @@ -188,10 +188,15 @@ export function createContentHelpers({ /** * SiteSettings for a locale — ONE query per locale per request, shared by the * layout, generatePageMetadata, resolveRoute and anything else that asks. + * + * Depth 1: logos and assigned pages arrive as documents (slug, title), but + * not THEIR relations. Depth 2 pulled every assigned page's blocks and media + * (homepage, archives, system pages) into every request; resolveRoute + * re-fetches the one page it actually renders. */ const getSettings = cache(async (locale: string) => { const payload = await getCachedPayload() - return payload.findGlobal({ slug: settingsSlug as never, depth: 2, locale: locale as never }) + return payload.findGlobal({ slug: settingsSlug as never, depth: 1, locale: locale as never }) }) /** What does this URL point at? Routing only — no listing data. */ diff --git a/test/settingsDepth.test.ts b/test/settingsDepth.test.ts new file mode 100644 index 0000000..d66880b --- /dev/null +++ b/test/settingsDepth.test.ts @@ -0,0 +1,84 @@ +/** Site Settings must stay shallow on every request; the rendered page is re-fetched at page depth. */ +import type { Payload } from 'payload' + +import { sqliteAdapter } from '@payloadcms/db-sqlite' +import { lexicalEditor } from '@payloadcms/richtext-lexical' +import os from 'os' +import path from 'path' +import { buildConfig, getPayload } from 'payload' +import { afterAll, beforeAll, describe, expect, test } from 'vitest' + +import { resolveRoute } from '../src/modules/content/resolveRoute.js' +import { createContentHelpers } from '../src/modules/frontend/createContentHelpers.js' +import { ipalKit } from '../src/plugin.js' + +let payload: Payload +let configPromise: ReturnType +let mediaId: number | string + +beforeAll(async () => { + configPromise = buildConfig({ + collections: [ + { + slug: 'pages', + fields: [ + { name: 'title', type: 'text' }, + { name: 'slug', type: 'text', localized: true }, + { name: 'hero', type: 'upload', relationTo: 'media' }, + ], + }, + { slug: 'posts', fields: [{ name: 'title', type: 'text' }, { name: 'slug', type: 'text', localized: true }] }, + { slug: 'media', fields: [{ name: 'alt', type: 'text' }], upload: { staticDir: path.join(os.tmpdir(), 'ipal-test-media') } }, + ], + db: sqliteAdapter({ client: { url: 'file::memory:' }, push: true }), + editor: lexicalEditor(), + plugins: [ + ipalKit({ + access: { authCollection: 'users' }, + content: { collections: [{ slug: 'posts' }] }, + i18n: { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }] }, + pages: { slug: 'pages' }, + // a project field, e.g. a menu linking pages — not capped by maxDepth + siteSettingsFields: [{ name: 'featured', type: 'relationship', relationTo: 'pages' }], + twoFactor: false, + }), + ], + secret: 'depth-test', + telemetry: false, + }) + payload = await getPayload({ config: configPromise }) + const png = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==', 'base64') + const media = await payload.create({ + collection: 'media' as never, + data: { alt: 'hero' } as never, + file: { data: png, mimetype: 'image/png', name: 'hero.png', size: png.length }, + }) + mediaId = media.id + const home = await payload.create({ collection: 'pages' as never, data: { slug: 'home', hero: mediaId, title: 'Home' } as never }) + const blog = await payload.create({ collection: 'pages' as never, data: { slug: 'blog', hero: mediaId, title: 'Blog' } as never }) + await payload.updateGlobal({ slug: 'site-settings' as never, data: { homepage: home.id, featured: home.id, postsArchive: blog.id, siteName: 'Acme' } as never }) +}, 60_000) + +afterAll(async () => { + await payload?.destroy() +}) + +describe('Site Settings depth', () => { + test('getSettings populates related pages one level only (their media stays an id)', async () => { + const { getSettings } = createContentHelpers({ config: configPromise }) + const settings = (await getSettings('pl')) as { featured: { hero: unknown; slug: string } } + expect(settings.featured.slug).toBe('home') + expect(settings.featured.hero).toBe(mediaId) + }) + + test('the archive page is re-fetched at page depth, so its blocks and media render', async () => { + const route = await resolveRoute({ + content: { collections: [{ slug: 'posts' }] }, + locale: 'pl', + payload, + segments: ['blog'], + }) + expect(route?.type).toBe('archive') + expect((route?.doc as { hero: { alt?: string } }).hero).toMatchObject({ alt: 'hero' }) + }) +})