Separate from the mail rule so that it can be taken or left. A site whose address matches
iapl({ developmentHosts }) - built in: *.dev.netscale.pl - is noindex with no env: robots.txt disallows
everything, pages carry noindex, IndexNow sends nothing, buildSecurityHeaders() adds X-Robots-Tag. Nothing
kept a development copy out of search engines until now unless somebody set the variable.
- iapl({ developmentNoindex: false }) switches it off for the project, IAPL_NOINDEX=0 for one environment
- buildSecurityHeaders({ developmentHosts, serverUrl }): next.config runs before the plugin
- the mail rule keeps its own two signals: the development pattern, and the variable itself
- changelog "what changes on upgrade", deployment.md, security.md, AGENT.md, skills
Co-Authored-By: Claude Fable 5.2 <[email protected]>
112 lines
4.2 KiB
TypeScript
112 lines
4.2 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
|
|
|
|
import type { I18nConfig } from '../src/modules/i18n/types.js'
|
|
|
|
import { buildIndexNowHook, createIndexNowKeyRoute, indexNowFields } from '../src/modules/seo/indexNow.js'
|
|
|
|
const config: I18nConfig = {
|
|
defaultLocale: 'pl',
|
|
locales: [
|
|
{ code: 'pl', label: 'Polski' },
|
|
{ code: 'en', label: 'English' },
|
|
],
|
|
}
|
|
|
|
function fakeReq(doc: Record<string, unknown>, settings: Record<string, unknown>) {
|
|
return {
|
|
payload: {
|
|
findByID: vi.fn(async () => doc),
|
|
findGlobal: vi.fn(async ({ slug }: { slug: string }) => (slug === 'site-integrations' ? { indexNowKey: 'k123' } : settings)),
|
|
},
|
|
}
|
|
}
|
|
|
|
const flush = () => new Promise((r) => setTimeout(r, 0))
|
|
|
|
describe('IndexNow hook', () => {
|
|
let fetchSpy: ReturnType<typeof vi.spyOn>
|
|
beforeEach(() => {
|
|
fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue(new Response(null, { status: 202 }))
|
|
vi.stubEnv('NODE_ENV', 'production')
|
|
vi.stubEnv('NEXT_PUBLIC_SERVER_URL', 'https://acme.pl')
|
|
})
|
|
afterEach(() => {
|
|
vi.restoreAllMocks()
|
|
vi.unstubAllEnvs()
|
|
})
|
|
|
|
const run = async (row: Record<string, unknown>, opts = {}, settings: Record<string, unknown> = {}) => {
|
|
const req = fakeReq({ slug: { en: 'about', pl: 'o-nas' } }, settings)
|
|
const hook = buildIndexNowHook({ config, ...opts })
|
|
await hook({ collection: { slug: 'pages' }, context: {}, doc: { id: 1, ...row }, req } as never)
|
|
await flush()
|
|
await flush()
|
|
}
|
|
|
|
test('published page → every language submitted with key and keyLocation', async () => {
|
|
await run({ _status: 'published' })
|
|
expect(fetchSpy).toHaveBeenCalledOnce()
|
|
const body = JSON.parse((fetchSpy.mock.calls[0][1] as RequestInit).body as string)
|
|
expect(body).toEqual({
|
|
host: 'acme.pl',
|
|
key: 'k123',
|
|
keyLocation: 'https://acme.pl/indexnow-key.txt',
|
|
urlList: ['https://acme.pl/pl/o-nas', 'https://acme.pl/en/about'],
|
|
})
|
|
})
|
|
|
|
test('archive entry gets the archive prefix; no archive page → nothing sent', async () => {
|
|
await run({}, { contentCollection: 'posts' }, { postsArchive: { slug: { en: 'blog', pl: 'blog-pl' } } })
|
|
const body = JSON.parse((fetchSpy.mock.calls[0][1] as RequestInit).body as string)
|
|
expect(body.urlList).toEqual(['https://acme.pl/pl/blog-pl/o-nas', 'https://acme.pl/en/blog/about'])
|
|
fetchSpy.mockClear()
|
|
await run({}, { contentCollection: 'posts' }, {})
|
|
expect(fetchSpy).not.toHaveBeenCalled()
|
|
})
|
|
|
|
test('drafts, noindex, dev and localhost send nothing', async () => {
|
|
await run({ _status: 'draft' })
|
|
await run({ noindex: true })
|
|
vi.stubEnv('NODE_ENV', 'development')
|
|
await run({})
|
|
vi.stubEnv('NODE_ENV', 'production')
|
|
vi.stubEnv('NEXT_PUBLIC_SERVER_URL', 'http://localhost:3000')
|
|
await run({})
|
|
expect(fetchSpy).not.toHaveBeenCalled()
|
|
})
|
|
|
|
test('a development host sends nothing, with no variable; IAPL_NOINDEX=0 says otherwise', async () => {
|
|
vi.stubEnv('IAPL_NOINDEX', '')
|
|
vi.stubEnv('NEXT_PUBLIC_SERVER_URL', 'https://acme.dev.netscale.pl')
|
|
await run({ _status: 'published' })
|
|
expect(fetchSpy).not.toHaveBeenCalled()
|
|
vi.stubEnv('IAPL_NOINDEX', '0')
|
|
await run({ _status: 'published' })
|
|
expect(fetchSpy).toHaveBeenCalledOnce()
|
|
})
|
|
|
|
test('staging copy (IAPL_NOINDEX) sends nothing even in production', async () => {
|
|
vi.stubEnv('IAPL_NOINDEX', '1')
|
|
await run({ _status: 'published' })
|
|
expect(fetchSpy).not.toHaveBeenCalled()
|
|
})
|
|
})
|
|
|
|
describe('IndexNow key', () => {
|
|
afterEach(() => vi.unstubAllEnvs())
|
|
|
|
test('field generates a 32-char key when empty and keeps an existing one', () => {
|
|
const hook = (indexNowFields[0] as { hooks: { beforeChange: ((a: { value: unknown }) => string)[] } }).hooks.beforeChange[0]
|
|
expect(hook({ value: '' })).toMatch(/^[0-9a-f]{32}$/)
|
|
expect(hook({ value: ' mine ' })).toBe('mine')
|
|
})
|
|
|
|
test('key route: env wins, 404 without a key', async () => {
|
|
const payload = { findGlobal: async () => ({}) } as never
|
|
expect((await createIndexNowKeyRoute({ getCachedPayload: async () => payload })()).status).toBe(404)
|
|
vi.stubEnv('INDEXNOW_KEY', 'fromenv')
|
|
const res = await createIndexNowKeyRoute({ getCachedPayload: async () => payload })()
|
|
expect(await res.text()).toBe('fromenv')
|
|
})
|
|
})
|