Server entries no longer load 'use client' modules
The main entry and next/middleware re-exported module barrels that also export React client components (LanguageSwitcher, AlternateLinks*, AccessibilityWidget), so payload.config, the CLI and the proxy bundle evaluated React UI. They now import concrete files; i18n client components moved to modules/i18n/client.ts. test/entryGraph guards it. Also lint fixes in tests added on this branch. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
e5d86b8afe
commit
14fd2c497f
@@ -92,14 +92,14 @@ describe('under construction — Site Settings → createPageRoute', () => {
|
||||
const helpers = createContentHelpers({ baseUrl: 'https://acme.pl', config: configPromise, i18n })
|
||||
expect((await helpers.getConstructionMode('pl')).enabled).toBe(true)
|
||||
|
||||
const md = (slug: string[]) => helpers.markdownRoute(new Request('https://acme.pl/md'), { params: Promise.resolve({ locale: 'pl', slug }) })
|
||||
const md = (slug: string[]) => helpers.markdownRoute(new Request('https://acme.pl/md'), { params: Promise.resolve({ slug, locale: 'pl' }) })
|
||||
expect((await md(['o-nas'])).status).toBe(404)
|
||||
expect((await md(['brief'])).status).toBe(200)
|
||||
|
||||
const rpc = async (name: string, args: Record<string, unknown>) => {
|
||||
const res = await helpers.mcpPOST(
|
||||
new Request('https://acme.pl/api/mcp', {
|
||||
body: JSON.stringify({ id: 1, jsonrpc: '2.0', method: 'tools/call', params: { arguments: args, name } }),
|
||||
body: JSON.stringify({ id: 1, jsonrpc: '2.0', method: 'tools/call', params: { name, arguments: args } }),
|
||||
headers: { 'content-type': 'application/json', 'x-real-ip': '198.51.100.4' },
|
||||
method: 'POST',
|
||||
}),
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* The main entry (payload.config, the payload CLI, server code) and the proxy
|
||||
* entry must not load 'use client' modules: in plain Node every re-exported
|
||||
* module is evaluated, and the proxy bundle should stay free of React UI.
|
||||
*/
|
||||
import { existsSync, readFileSync } from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import { describe, expect, test } from 'vitest'
|
||||
|
||||
const root = path.resolve(import.meta.dirname, '..')
|
||||
|
||||
function resolve(from: string, spec: string): null | string {
|
||||
if (!spec.startsWith('.')) {return null}
|
||||
const base = path.resolve(path.dirname(from), spec).replace(/\.js$/, '')
|
||||
for (const ext of ['.ts', '.tsx', '/index.ts']) {
|
||||
if (existsSync(base + ext)) {return base + ext}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/** Runtime (non-type) imports and re-exports reachable from `entry`; returns the 'use client' ones. */
|
||||
function clientModulesReachable(entry: string): string[] {
|
||||
const seen = new Set<string>()
|
||||
const hits: string[] = []
|
||||
const stack = [path.join(root, entry)]
|
||||
while (stack.length) {
|
||||
const file = stack.pop()!
|
||||
if (seen.has(file)) {continue}
|
||||
seen.add(file)
|
||||
const src = readFileSync(file, 'utf8')
|
||||
if (/^\s*['"]use client['"]/.test(src)) {hits.push(path.relative(root, file))}
|
||||
for (const m of src.matchAll(/^(?:import|export) (?!type\b)[^'"]*?from ['"]([^'"]+)['"]/gm)) {
|
||||
const next = resolve(file, m[1])
|
||||
if (next) {stack.push(next)}
|
||||
}
|
||||
}
|
||||
return hits.sort()
|
||||
}
|
||||
|
||||
describe('server entries load no client modules', () => {
|
||||
test.each(['src/exports/next-middleware.ts', 'src/index.ts'])('%s', (entry) => {
|
||||
expect(clientModulesReachable(entry)).toEqual([])
|
||||
})
|
||||
})
|
||||
@@ -4,7 +4,7 @@ import { describe, expect, test, vi } from 'vitest'
|
||||
import { buildCrossLocaleFieldSync } from '../src/modules/hooks/buildCrossLocaleFieldSync.js'
|
||||
import { buildPreventDeleteSystemPage } from '../src/modules/hooks/preventDeleteSystemPage.js'
|
||||
|
||||
const config = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'PL' }, { code: 'en', label: 'EN' }] }
|
||||
const config = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'PL' }, { code: 'en', label: 'EN' }] } as const
|
||||
|
||||
/** Mimics the Local API: a call with `req` sets req.locale / req.fallbackLocale to its own. */
|
||||
function fakeReq() {
|
||||
|
||||
@@ -3,9 +3,9 @@ import { afterEach, describe, expect, test, vi } from 'vitest'
|
||||
import {
|
||||
checkRateLimit,
|
||||
clientIp,
|
||||
UNKNOWN_IP_FACTOR,
|
||||
consumeRateLimit,
|
||||
setRateLimitStore,
|
||||
UNKNOWN_IP_FACTOR,
|
||||
} from '../src/modules/shared/rateLimit.js'
|
||||
|
||||
const req = (headers: Record<string, string>) => new Request('http://x/', { headers })
|
||||
|
||||
@@ -4,17 +4,17 @@ 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 config = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'PL' }] } as const
|
||||
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' })
|
||||
const payload = payloadWith({ slug: 'nowa-oferta', _status: 'published' })
|
||||
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' })
|
||||
const payload = payloadWith({ slug: 'tajny-projekt', _status: 'draft' })
|
||||
expect(await resolveRedirect({ config, locale: 'pl', payload, segments: ['oferta'] })).toBeNull()
|
||||
})
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import { afterEach, describe, expect, test, vi } from 'vitest'
|
||||
import { buildGlobalRevalidateHook } from '../src/modules/hooks/buildRevalidateHook.js'
|
||||
import { ipalKit } from '../src/plugin.js'
|
||||
|
||||
const i18n = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }] }
|
||||
const i18n = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }] } as const
|
||||
const run = (g: GlobalConfig | undefined, context: object = {}) =>
|
||||
g?.hooks?.afterChange?.forEach((h) => h({ context, doc: {}, global: g, previousDoc: {}, req: {} } as never))
|
||||
|
||||
|
||||
@@ -51,12 +51,12 @@ beforeAll(async () => {
|
||||
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 },
|
||||
file: { name: 'hero.png', data: png, mimetype: 'image/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 })
|
||||
await payload.updateGlobal({ slug: 'site-settings' as never, data: { featured: home.id, homepage: home.id, postsArchive: blog.id, siteName: 'Acme' } as never })
|
||||
}, 60_000)
|
||||
|
||||
afterAll(async () => {
|
||||
|
||||
Reference in New Issue
Block a user