|
| 1 | +/** |
| 2 | + * Per-page <title>/meta on the auto-shell (SSG/app-shell) path — #1756. |
| 3 | + * |
| 4 | + * Before the fix, every page generated by buildApp() carried the identical |
| 5 | + * config title: @section('title', …) had no @yield target in the shell model |
| 6 | + * so it was silently dropped, and @head … @endhead content was extracted by |
| 7 | + * processHeadDirective but discarded at the call site. |
| 8 | + */ |
| 9 | +import { afterEach, describe, expect, it } from 'bun:test' |
| 10 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs' |
| 11 | +import { tmpdir } from 'node:os' |
| 12 | +import { join } from 'node:path' |
| 13 | +import { defaultConfig, processDirectives } from '../../src/index' |
| 14 | + |
| 15 | +const tmpDirs: string[] = [] |
| 16 | + |
| 17 | +function tmp(): string { |
| 18 | + const dir = mkdtempSync(join(tmpdir(), 'stx-per-page-head-')) |
| 19 | + tmpDirs.push(dir) |
| 20 | + return dir |
| 21 | +} |
| 22 | + |
| 23 | +afterEach(() => { |
| 24 | + for (const dir of tmpDirs.splice(0)) |
| 25 | + rmSync(dir, { recursive: true, force: true }) |
| 26 | +}) |
| 27 | + |
| 28 | +function shellOptions(extra: Record<string, unknown> = {}): Record<string, unknown> { |
| 29 | + return { |
| 30 | + ...defaultConfig, |
| 31 | + partialsDir: '/tmp', |
| 32 | + componentsDir: '/tmp', |
| 33 | + autoShell: true, |
| 34 | + app: { head: { title: 'Config Title' } }, |
| 35 | + ...extra, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +describe('per-page head on the auto-shell path (#1756)', () => { |
| 40 | + it('uses @section("title", literal) as the shell title', async () => { |
| 41 | + const result = await processDirectives( |
| 42 | + '@section(\'title\', \'About Us\')\n<h1>About</h1>', |
| 43 | + {}, |
| 44 | + '/test.stx', |
| 45 | + shellOptions(), |
| 46 | + new Set(), |
| 47 | + ) |
| 48 | + expect(result).toContain('<title>About Us</title>') |
| 49 | + expect(result).not.toContain('<title>Config Title</title>') |
| 50 | + }) |
| 51 | + |
| 52 | + it('uses @section("title", expression) resolved from server context', async () => { |
| 53 | + const result = await processDirectives( |
| 54 | + '@section(\'title\', pageTitle)\n<h1>Product</h1>', |
| 55 | + { pageTitle: 'Widget · Shop' }, |
| 56 | + '/test.stx', |
| 57 | + shellOptions(), |
| 58 | + new Set(), |
| 59 | + ) |
| 60 | + expect(result).toContain('<title>Widget · Shop</title>') |
| 61 | + }) |
| 62 | + |
| 63 | + it('injects @head … @endhead content into the generated <head>', async () => { |
| 64 | + const result = await processDirectives( |
| 65 | + '@head\n<meta name="description" content="Per-page description">\n<link rel="canonical" href="https://example.com/about">\n@endhead\n<h1>About</h1>', |
| 66 | + {}, |
| 67 | + '/test.stx', |
| 68 | + shellOptions(), |
| 69 | + new Set(), |
| 70 | + ) |
| 71 | + const headEnd = result.indexOf('</head>') |
| 72 | + const head = result.slice(0, headEnd) |
| 73 | + expect(head).toContain('<meta name="description" content="Per-page description">') |
| 74 | + expect(head).toContain('<link rel="canonical" href="https://example.com/about">') |
| 75 | + // directive markers removed from the body |
| 76 | + expect(result).not.toContain('@head') |
| 77 | + expect(result).not.toContain('@endhead') |
| 78 | + }) |
| 79 | + |
| 80 | + it('promotes a <title> inside @head to the shell title (no competing default)', async () => { |
| 81 | + const result = await processDirectives( |
| 82 | + '@head\n<title>Head Title</title>\n@endhead\n<h1>Hi</h1>', |
| 83 | + {}, |
| 84 | + '/test.stx', |
| 85 | + shellOptions(), |
| 86 | + new Set(), |
| 87 | + ) |
| 88 | + expect(result).toContain('<title>Head Title</title>') |
| 89 | + expect(result).not.toContain('<title>Config Title</title>') |
| 90 | + // exactly one <title> in the document |
| 91 | + expect(result.match(/<title/g)!.length).toBe(1) |
| 92 | + }) |
| 93 | + |
| 94 | + it('@title() still wins over @section("title") and config', async () => { |
| 95 | + const result = await processDirectives( |
| 96 | + '@title(\'Runtime Title\')\n@section(\'title\', \'Section Title\')\n<h1>Hi</h1>', |
| 97 | + {}, |
| 98 | + '/test.stx', |
| 99 | + shellOptions(), |
| 100 | + new Set(), |
| 101 | + ) |
| 102 | + expect(result).toContain('<title>Runtime Title</title>') |
| 103 | + expect(result).not.toContain('Section Title') |
| 104 | + }) |
| 105 | + |
| 106 | + it('falls back to the config title when no per-page source exists', async () => { |
| 107 | + const result = await processDirectives( |
| 108 | + '<h1>Plain</h1>', |
| 109 | + {}, |
| 110 | + '/test.stx', |
| 111 | + shellOptions(), |
| 112 | + new Set(), |
| 113 | + ) |
| 114 | + expect(result).toContain('<title>Config Title</title>') |
| 115 | + }) |
| 116 | + |
| 117 | + it('keeps config headRaw alongside @head content', async () => { |
| 118 | + const result = await processDirectives( |
| 119 | + '@head\n<meta name="robots" content="noindex">\n@endhead\n<h1>Hi</h1>', |
| 120 | + {}, |
| 121 | + '/test.stx', |
| 122 | + shellOptions({ app: { head: { title: 'Config Title', headRaw: '<meta name="generator" content="stx">' } } }), |
| 123 | + new Set(), |
| 124 | + ) |
| 125 | + const head = result.slice(0, result.indexOf('</head>')) |
| 126 | + expect(head).toContain('<meta name="generator" content="stx">') |
| 127 | + expect(head).toContain('<meta name="robots" content="noindex">') |
| 128 | + }) |
| 129 | + |
| 130 | + it('honors @section("title") from a view extending a body-fragment layout', async () => { |
| 131 | + const dir = tmp() |
| 132 | + const layoutsDir = join(dir, 'layouts') |
| 133 | + rmSync(layoutsDir, { recursive: true, force: true }) |
| 134 | + // body-fragment layout: no <html>/<head>, just structure + @yield('content') |
| 135 | + const { mkdirSync } = await import('node:fs') |
| 136 | + mkdirSync(layoutsDir, { recursive: true }) |
| 137 | + writeFileSync(join(layoutsDir, 'app.stx'), '<main>@yield(\'content\')</main>\n', 'utf8') |
| 138 | + const pagePath = join(dir, 'about.stx') |
| 139 | + writeFileSync(pagePath, '@extends(\'app\')\n@section(\'title\', \'About Page\')\n@section(\'content\')\n<h1>About</h1>\n@endsection\n', 'utf8') |
| 140 | + |
| 141 | + const result = await processDirectives( |
| 142 | + await Bun.file(pagePath).text(), |
| 143 | + {}, |
| 144 | + pagePath, |
| 145 | + shellOptions({ layoutsDir }), |
| 146 | + new Set(), |
| 147 | + ) |
| 148 | + expect(result).toContain('<title>About Page</title>') |
| 149 | + expect(result).toContain('<h1>About</h1>') |
| 150 | + expect(result).not.toContain('<title>Config Title</title>') |
| 151 | + }) |
| 152 | +}) |
0 commit comments