Files
ipal-kit/test/seoIntegration.test.ts
T
Radosław SmolińskiandClaude Opus 5.5 94e563055f SEO/GEO/GSC: sitemap index + caching, Search & AI settings, getRobots
- Sitemap: batched reads (500), published-only where, field select; sitemap
  index over maxUrls (default 10k) with ?part=N; Cache-Control; in-process
  memo; runtime errors → 503 Retry-After (empty urlset only during next build)
- Site Settings → Search & AI tab (seo.searchSettings: false removes it):
  Google/Bing/Yandex verification (paste code or whole meta tag) added to
  page metadata; AI crawler policy + per-bot exceptions; llms.txt note
- buildRobots aiBotRules; getRobots helper reads the panel; more AI agents
- Under construction: sitemap and llms.txt list only open pages
- collectSiteIndex: no silent 1000 cap; llms routes send Cache-Control
- IndexNow: nothing sent with IPAL_NOINDEX
- Unknown route metadata is noindex
- Tests (unit + SQLite integration) and docs/seo.md

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-22 17:09:01 +02:00

105 lines
5.0 KiB
TypeScript

import type { Payload } from 'payload'
import { sqliteAdapter } from '@payloadcms/db-sqlite'
import { lexicalEditor } from '@payloadcms/richtext-lexical'
import os from 'node:os'
import path from 'node:path'
import { buildConfig, getPayload } from 'payload'
import { afterAll, beforeAll, describe, expect, test, vi } from 'vitest'
import { createContentHelpers } from '../src/modules/frontend/createContentHelpers.js'
import { ipalKit } from '../src/plugin.js'
vi.mock('server-only', () => ({}))
/**
* Sitemap, llms.txt and robots against a real (SQLite) Payload: the batched,
* `select`-ed, published-only reads must work with the database adapter, and
* Site Settings → Search & AI / Under construction must reach the output.
*/
describe('SEO outputs against a real database', () => {
let payload: Payload
let configPromise: Parameters<typeof getPayload>[0]['config']
const i18n = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }, { code: 'en', label: 'English' }] } as const
const helpers = () => createContentHelpers({ baseUrl: 'https://acme.pl', config: configPromise, i18n })
const urls = async () => (await helpers().sitemap()).map((e) => e.url).sort()
beforeAll(async () => {
configPromise = buildConfig({
collections: [
{ slug: 'users', auth: true, fields: [] },
{
slug: 'pages',
fields: [
{ name: 'title', type: 'text' },
{ name: 'slug', type: 'text', index: true, localized: true },
{ name: 'noindex', type: 'checkbox' },
],
versions: { drafts: true },
},
{ slug: 'media', fields: [], upload: { staticDir: path.join(os.tmpdir(), 'ipal-test-media-seo') } },
],
db: sqliteAdapter({ client: { url: 'file::memory:' }, push: true }),
editor: lexicalEditor(),
plugins: [ipalKit({ access: { authCollection: 'users' }, i18n, pages: { slug: 'pages' }, twoFactor: false })],
secret: 'seo-integration-test-secret',
telemetry: false,
})
payload = await getPayload({ config: configPromise })
const create = async (slug: Record<string, string>, extra: Record<string, unknown> = {}) => {
const doc = await payload.create({
collection: 'pages' as never,
data: { slug: slug.pl, _status: 'published', title: slug.pl, ...extra } as never,
locale: 'pl' as never,
})
await payload.update({ id: doc.id, collection: 'pages' as never, data: { slug: slug.en } as never, locale: 'en' as never })
return doc
}
const home = await create({ en: 'home', pl: 'start' })
await create({ en: 'about', pl: 'o-nas' })
await create({ en: 'hidden', pl: 'ukryta' }, { noindex: true })
await create({ en: 'draft', pl: 'szkic' }, { _status: 'draft' })
await payload.updateGlobal({ slug: 'site-settings' as never, data: { homepage: home.id, siteName: 'Acme' } as never })
}, 60_000)
afterAll(async () => {
await payload?.destroy()
})
test('sitemap: published and indexable pages in every language', async () => {
expect(await urls()).toEqual(['https://acme.pl/en', 'https://acme.pl/en/about', 'https://acme.pl/pl', 'https://acme.pl/pl/o-nas'])
})
test('verification codes and AI crawler rules from Site Settings → Search & AI', async () => {
await payload.updateGlobal({
slug: 'site-settings' as never,
data: {
aiCrawlers: { policy: 'block-training', rules: [{ access: 'disallow', userAgent: 'PerplexityBot' }] },
searchVerification: { google: '<meta name="google-site-verification" content="G-tok_1" />' },
} as never,
})
const stored = (await payload.findGlobal({ slug: 'site-settings' as never })) as { searchVerification?: { google?: string } }
expect(stored.searchVerification?.google).toBe('G-tok_1')
const meta = await helpers().generatePageMetadata('pl', ['o-nas'])
expect(meta.verification).toEqual({ google: 'G-tok_1' })
const rules = (await helpers().getRobots()).rules as { disallow?: string[]; userAgent: string | string[] }[]
expect(rules).toContainEqual({ disallow: ['/'], userAgent: ['PerplexityBot'] })
const assistants = rules.find((r) => Array.isArray(r.userAgent) && r.userAgent.includes('ClaudeBot'))
expect(assistants?.userAgent).not.toContain('PerplexityBot')
// the code-only handler ignores the panel
expect(helpers().robots().rules).not.toContainEqual({ disallow: ['/'], userAgent: ['PerplexityBot'] })
})
test('under construction: sitemap and llms.txt list only the open pages', async () => {
await payload.updateGlobal({ slug: 'site-settings' as never, data: { construction: { enabled: true } } as never, locale: 'pl' as never })
expect(await urls()).toEqual(['https://acme.pl/en', 'https://acme.pl/pl'])
const llms = await (await helpers().llmsRoute()).text()
expect(llms).not.toContain('o-nas')
await payload.updateGlobal({ slug: 'site-settings' as never, data: { construction: { enabled: false } } as never, locale: 'pl' as never })
expect(await urls()).toHaveLength(4)
})
})