|
| 1 | +/** |
| 2 | + * Entry routes take their runtime manifest from the template that renders |
| 3 | + * them, not from whatever page the site-wide snapshot happens to name. |
| 4 | + * |
| 5 | + * `getLatestPublishedSiteSnapshot` is `order by data_rows.created_at asc limit |
| 6 | + * 1` — the first published page ever created. It exists to carry `site_json`, |
| 7 | + * and its `runtime_assets_json` used to ride along. Entry routes are rendered |
| 8 | + * from a template plus a data row rather than from a page, so they inherited |
| 9 | + * that manifest wholesale: every entry route on a site served one arbitrary |
| 10 | + * page's scripts, and `assetScopeAppliesToPage` was never consulted on the |
| 11 | + * path at all. |
| 12 | + * |
| 13 | + * Over-inclusion was what surfaced in practice — a script scoped to two pages |
| 14 | + * shipping on ten entry routes — but the same bug under-includes just as |
| 15 | + * easily: if the oldest page carries no scripts, a scoped script reaches |
| 16 | + * nothing. |
| 17 | + */ |
| 18 | +import { describe, expect, it } from 'bun:test' |
| 19 | +import type { DbClient } from '../../../server/db' |
| 20 | +import type { PublishedPageSnapshot } from '../../../server/repositories/publish' |
| 21 | +import { snapshotForEntryRoute } from '../../../server/publish/entryTemplateSnapshot' |
| 22 | +import { makeSite } from '../fixtures' |
| 23 | + |
| 24 | +const postsTarget = { kind: 'postTypes' as const, tableSlugs: ['posts'] } |
| 25 | + |
| 26 | +function runtimeAssets(name: string) { |
| 27 | + return { |
| 28 | + scripts: [{ publicPath: `/_instatic/assets/${name}/classic/001-${name}.js`, placement: 'body-end' as const }], |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/** A site whose first page is unrelated and whose second is the entry template. */ |
| 33 | +function siteSnapshot(): PublishedPageSnapshot { |
| 34 | + const site = makeSite() |
| 35 | + site.pages[0].id = 'oldest-page' |
| 36 | + site.pages.push({ |
| 37 | + ...structuredClone(site.pages[0]), |
| 38 | + id: 'post-template', |
| 39 | + slug: 'post-template', |
| 40 | + template: { enabled: true, target: postsTarget, priority: 10 }, |
| 41 | + }) |
| 42 | + return { cmsSnapshotVersion: 1, pageRowId: 'oldest-page', site } |
| 43 | +} |
| 44 | + |
| 45 | +/** Stands in for the DB: only `getPublishedPageSnapshotById` is reached. */ |
| 46 | +function dbReturning(byPageId: Record<string, unknown>): DbClient { |
| 47 | + const fake = (async (_strings: TemplateStringsArray, ...params: unknown[]) => { |
| 48 | + const pageId = String(params[0]) |
| 49 | + const assets = byPageId[pageId] |
| 50 | + return assets |
| 51 | + ? { rows: [{ row_id: pageId, site_json: makeSite(), runtime_assets_json: assets }], rowCount: 1 } |
| 52 | + : { rows: [], rowCount: 0 } |
| 53 | + }) as unknown as DbClient |
| 54 | + return fake |
| 55 | +} |
| 56 | + |
| 57 | +describe('entry-route runtime manifest', () => { |
| 58 | + it('uses the entry template\'s own manifest, not the site snapshot\'s', async () => { |
| 59 | + const snapshot = siteSnapshot() |
| 60 | + const db = dbReturning({ 'post-template': runtimeAssets('template') }) |
| 61 | + |
| 62 | + const resolved = await snapshotForEntryRoute(db, snapshot, 'posts') |
| 63 | + |
| 64 | + expect(resolved.runtimeAssets?.scripts[0]?.publicPath).toContain('001-template.js') |
| 65 | + }) |
| 66 | + |
| 67 | + it('serves no scripts when the template has none, rather than another page\'s', async () => { |
| 68 | + // Reproduce the old shape: a site snapshot already carrying the oldest |
| 69 | + // page's manifest, and a template with none of its own. The old code |
| 70 | + // passed that straight through, so every entry route on the site served |
| 71 | + // `001-oldest.js`. |
| 72 | + const snapshot = { ...siteSnapshot(), runtimeAssets: runtimeAssets('oldest') } |
| 73 | + const db = dbReturning({}) |
| 74 | + |
| 75 | + const resolved = await snapshotForEntryRoute(db, snapshot, 'posts') |
| 76 | + |
| 77 | + expect(resolved.runtimeAssets?.scripts[0]?.publicPath ?? '').not.toContain('001-oldest.js') |
| 78 | + }) |
| 79 | + |
| 80 | + it('overrides a stale manifest on the site snapshot with the template\'s', async () => { |
| 81 | + const snapshot = { ...siteSnapshot(), runtimeAssets: runtimeAssets('oldest') } |
| 82 | + const db = dbReturning({ 'post-template': runtimeAssets('template') }) |
| 83 | + |
| 84 | + const resolved = await snapshotForEntryRoute(db, snapshot, 'posts') |
| 85 | + |
| 86 | + expect(resolved.runtimeAssets?.scripts[0]?.publicPath).toContain('001-template.js') |
| 87 | + }) |
| 88 | + |
| 89 | + it('leaves the site document untouched so the resolved chain still applies', async () => { |
| 90 | + const snapshot = siteSnapshot() |
| 91 | + const db = dbReturning({ 'post-template': runtimeAssets('template') }) |
| 92 | + |
| 93 | + const resolved = await snapshotForEntryRoute(db, snapshot, 'posts') |
| 94 | + |
| 95 | + expect(resolved.site).toBe(snapshot.site) |
| 96 | + }) |
| 97 | + |
| 98 | + it('falls back to the site snapshot when the table has no entry template', async () => { |
| 99 | + const snapshot = siteSnapshot() |
| 100 | + const db = dbReturning({ 'post-template': runtimeAssets('template') }) |
| 101 | + |
| 102 | + const resolved = await snapshotForEntryRoute(db, snapshot, 'no-such-table') |
| 103 | + |
| 104 | + expect(resolved).toBe(snapshot) |
| 105 | + }) |
| 106 | +}) |
0 commit comments