toiljs 0.0.12 → 0.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/build/cli/.tsbuildinfo +1 -1
- package/build/cli/index.js +2926 -191
- package/build/client/.tsbuildinfo +1 -1
- package/build/client/head/metadata.d.ts +3 -1
- package/build/client/head/metadata.js +8 -0
- package/build/client/index.d.ts +4 -4
- package/build/client/index.js +2 -2
- package/build/client/navigation/navigation.d.ts +2 -0
- package/build/client/navigation/navigation.js +9 -1
- package/build/client/routing/loader.d.ts +2 -0
- package/build/compiler/.tsbuildinfo +1 -1
- package/build/compiler/config.d.ts +2 -0
- package/build/compiler/config.js +1 -0
- package/build/compiler/generate.js +10 -1
- package/build/compiler/index.js +2 -0
- package/build/compiler/prerender.js +1 -0
- package/build/compiler/seo.d.ts +1 -1
- package/build/compiler/seo.js +3 -2
- package/build/compiler/ssg.d.ts +5 -0
- package/build/compiler/ssg.js +90 -0
- package/examples/basic/client/routes/search.tsx +61 -61
- package/package.json +4 -3
- package/src/client/head/metadata.ts +112 -94
- package/src/client/index.ts +89 -79
- package/src/client/navigation/navigation.ts +235 -215
- package/src/client/routing/loader.ts +10 -0
- package/src/client/search/search.ts +189 -189
- package/src/client/search/use-page-search.ts +73 -73
- package/src/compiler/config.ts +182 -173
- package/src/compiler/generate.ts +394 -378
- package/src/compiler/index.ts +3 -0
- package/src/compiler/pages.ts +2 -2
- package/src/compiler/prerender.ts +156 -152
- package/src/compiler/seo.ts +390 -381
- package/src/compiler/ssg.ts +126 -0
- package/test/dom/error-overlay.test.tsx +44 -44
- package/test/dom/router-loading.test.tsx +1 -1
- package/test/dom/use-metadata.test.tsx +58 -0
- package/test/seo.test.ts +164 -164
- package/test/ssg.test.ts +36 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build-time SSG for dynamic routes. After the client bundle is written, this loads each dynamic
|
|
3
|
+
* route that exports `generateStaticParams`, enumerates its concrete URLs, runs the route's
|
|
4
|
+
* `generateMetadata` per URL, and bakes a `<url>/index.html` (so JS-less crawlers get per-page tags)
|
|
5
|
+
* plus a `sitemap.xml` entry. Opt-in: a route without `generateStaticParams` is untouched, and the
|
|
6
|
+
* whole pass is skipped when no such route exists or `seo` is unconfigured. Build-only.
|
|
7
|
+
*
|
|
8
|
+
* Runs from `build()` (not the prerender Vite plugin) so it can reuse `createViteConfig` without the
|
|
9
|
+
* `vite.ts` <-> `prerender.ts` import cycle; it spins up a short-lived SSR server to load route source.
|
|
10
|
+
*/
|
|
11
|
+
import fs from 'node:fs';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
|
|
14
|
+
import { createServer } from 'vite';
|
|
15
|
+
|
|
16
|
+
import { type ResolvedToilConfig } from './config.js';
|
|
17
|
+
import { scanRoutes } from './routes.js';
|
|
18
|
+
import { injectSeoHtml, routeSeo, sitemapXml } from './seo.js';
|
|
19
|
+
import { createViteConfig } from './vite.js';
|
|
20
|
+
|
|
21
|
+
type StaticParams = Record<string, string | string[]>;
|
|
22
|
+
|
|
23
|
+
interface RouteModule {
|
|
24
|
+
generateStaticParams?: () => StaticParams[] | Promise<StaticParams[]>;
|
|
25
|
+
generateMetadata?: (args: {
|
|
26
|
+
params: StaticParams;
|
|
27
|
+
searchParams: URLSearchParams;
|
|
28
|
+
data: unknown;
|
|
29
|
+
}) => unknown;
|
|
30
|
+
loader?: (args: { params: StaticParams; searchParams: URLSearchParams }) => unknown;
|
|
31
|
+
metadata?: Record<string, unknown>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Substitutes `:param` / `*catch-all` segments in a route pattern with concrete param values. */
|
|
35
|
+
export function fillPattern(pattern: string, params: StaticParams): string {
|
|
36
|
+
return pattern.replace(/[:*]([A-Za-z0-9_]+)/g, (_m, name: string) => {
|
|
37
|
+
const value = params[name] as string | string[] | undefined;
|
|
38
|
+
if (Array.isArray(value)) return value.join('/');
|
|
39
|
+
return value ?? '';
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Coerces an unknown module export to a typed Metadata-ish record, or null. */
|
|
44
|
+
function asMetadata(value: unknown): Record<string, unknown> | null {
|
|
45
|
+
return typeof value === 'object' && value !== null ? (value as Record<string, unknown>) : null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Pre-renders every dynamic route that opts in via `generateStaticParams`. Bakes per-URL HTML into
|
|
50
|
+
* `outDir` and rewrites `sitemap.xml` with the generated URLs. Returns the list of generated URLs.
|
|
51
|
+
*/
|
|
52
|
+
export async function prerenderStaticParams(cfg: ResolvedToilConfig): Promise<string[]> {
|
|
53
|
+
if (!cfg.seo) return [];
|
|
54
|
+
const outDir = path.resolve(cfg.root, cfg.outDir);
|
|
55
|
+
// Prefer the clean shell stashed by the prerender plugin (no per-route SEO baked in); fall back
|
|
56
|
+
// to the built index.html.
|
|
57
|
+
const stashed = path.join(cfg.toilDir, 'shell.html');
|
|
58
|
+
const shellPath = fs.existsSync(stashed) ? stashed : path.join(outDir, 'index.html');
|
|
59
|
+
if (!fs.existsSync(shellPath)) return [];
|
|
60
|
+
|
|
61
|
+
const allRoutes = scanRoutes(cfg.routesAbsDir);
|
|
62
|
+
const dynamic = allRoutes.filter(
|
|
63
|
+
(r) => r.slot === undefined && !r.intercept && /[:*]/.test(r.pattern),
|
|
64
|
+
);
|
|
65
|
+
if (dynamic.length === 0) return [];
|
|
66
|
+
|
|
67
|
+
const shell = fs.readFileSync(shellPath, 'utf8');
|
|
68
|
+
const server = await createServer({
|
|
69
|
+
...(await createViteConfig(cfg)),
|
|
70
|
+
server: { middlewareMode: true, hmr: false },
|
|
71
|
+
appType: 'custom',
|
|
72
|
+
logLevel: 'silent',
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const generated: string[] = [];
|
|
76
|
+
const warn = (msg: string): void => {
|
|
77
|
+
process.stderr.write(` toil: SSG ${msg}\n`);
|
|
78
|
+
};
|
|
79
|
+
try {
|
|
80
|
+
for (const route of dynamic) {
|
|
81
|
+
let mod: RouteModule;
|
|
82
|
+
try {
|
|
83
|
+
mod = (await server.ssrLoadModule(route.file)) as RouteModule;
|
|
84
|
+
} catch (err) {
|
|
85
|
+
warn(`skipped ${route.pattern} (${err instanceof Error ? err.message : String(err)})`);
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (typeof mod.generateStaticParams !== 'function') continue;
|
|
89
|
+
const paramSets = await mod.generateStaticParams();
|
|
90
|
+
for (const params of paramSets) {
|
|
91
|
+
const url = fillPattern(route.pattern, params);
|
|
92
|
+
let metadata: Record<string, unknown> | null = null;
|
|
93
|
+
try {
|
|
94
|
+
if (typeof mod.generateMetadata === 'function') {
|
|
95
|
+
const searchParams = new URLSearchParams();
|
|
96
|
+
const data =
|
|
97
|
+
typeof mod.loader === 'function'
|
|
98
|
+
? await mod.loader({ params, searchParams })
|
|
99
|
+
: undefined;
|
|
100
|
+
metadata = asMetadata(await mod.generateMetadata({ params, searchParams, data }));
|
|
101
|
+
} else if (mod.metadata) {
|
|
102
|
+
metadata = asMetadata(mod.metadata);
|
|
103
|
+
}
|
|
104
|
+
} catch (err) {
|
|
105
|
+
warn(`metadata failed for ${url} (${err instanceof Error ? err.message : String(err)})`);
|
|
106
|
+
}
|
|
107
|
+
const html = injectSeoHtml(shell, routeSeo(cfg.seo, metadata, url));
|
|
108
|
+
const target = path.join(outDir, url.replace(/^\//, ''), 'index.html');
|
|
109
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
110
|
+
fs.writeFileSync(target, html);
|
|
111
|
+
generated.push(url);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
} finally {
|
|
115
|
+
await server.close();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (generated.length > 0) {
|
|
119
|
+
const sitemap = sitemapXml(cfg.seo, allRoutes, generated);
|
|
120
|
+
if (sitemap) fs.writeFileSync(path.join(outDir, 'sitemap.xml'), sitemap);
|
|
121
|
+
process.stdout.write(
|
|
122
|
+
` ✓ prerendered ${String(generated.length)} dynamic route${generated.length === 1 ? '' : 's'}\n`,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
return generated;
|
|
126
|
+
}
|
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
// @vitest-environment jsdom
|
|
2
|
-
import { act, cleanup, fireEvent, render } from '@testing-library/react';
|
|
3
|
-
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
DevErrorBoundary,
|
|
7
|
-
DevErrorOverlay,
|
|
8
|
-
initDevErrorOverlay,
|
|
9
|
-
} from '../../src/client/dev/error-overlay';
|
|
10
|
-
|
|
11
|
-
afterEach(cleanup);
|
|
12
|
-
|
|
13
|
-
function Boom(): never {
|
|
14
|
-
throw new Error('render boom');
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
describe('dev error overlay', () => {
|
|
18
|
-
it('surfaces an uncaught render error', () => {
|
|
19
|
-
// React logs caught boundary errors to console.error
|
|
20
|
-
const spy = vi.spyOn(console, 'error').mockImplementation(() => undefined);
|
|
21
|
-
const { getByRole } = render(
|
|
22
|
-
<>
|
|
23
|
-
<DevErrorBoundary>
|
|
24
|
-
<Boom />
|
|
25
|
-
</DevErrorBoundary>
|
|
26
|
-
<DevErrorOverlay />
|
|
27
|
-
</>,
|
|
28
|
-
);
|
|
29
|
-
expect(getByRole('alert').textContent).toContain('render boom');
|
|
30
|
-
spy.mockRestore();
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('surfaces an unhandled window error and dismisses it', async () => {
|
|
34
|
-
initDevErrorOverlay();
|
|
35
|
-
const { findByRole, queryByRole, getByText } = render(<DevErrorOverlay />);
|
|
36
|
-
act(() => {
|
|
37
|
-
window.dispatchEvent(new ErrorEvent('error', { error: new Error('async boom') }));
|
|
38
|
-
});
|
|
39
|
-
const alert = await findByRole('alert');
|
|
40
|
-
expect(alert.textContent).toContain('async boom');
|
|
41
|
-
fireEvent.click(getByText('Dismiss'));
|
|
42
|
-
expect(queryByRole('alert')).toBeNull();
|
|
43
|
-
});
|
|
44
|
-
});
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { act, cleanup, fireEvent, render } from '@testing-library/react';
|
|
3
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
DevErrorBoundary,
|
|
7
|
+
DevErrorOverlay,
|
|
8
|
+
initDevErrorOverlay,
|
|
9
|
+
} from '../../src/client/dev/error-overlay';
|
|
10
|
+
|
|
11
|
+
afterEach(cleanup);
|
|
12
|
+
|
|
13
|
+
function Boom(): never {
|
|
14
|
+
throw new Error('render boom');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
describe('dev error overlay', () => {
|
|
18
|
+
it('surfaces an uncaught render error', () => {
|
|
19
|
+
// React logs caught boundary errors to console.error, silence it for a clean test run.
|
|
20
|
+
const spy = vi.spyOn(console, 'error').mockImplementation(() => undefined);
|
|
21
|
+
const { getByRole } = render(
|
|
22
|
+
<>
|
|
23
|
+
<DevErrorBoundary>
|
|
24
|
+
<Boom />
|
|
25
|
+
</DevErrorBoundary>
|
|
26
|
+
<DevErrorOverlay />
|
|
27
|
+
</>,
|
|
28
|
+
);
|
|
29
|
+
expect(getByRole('alert').textContent).toContain('render boom');
|
|
30
|
+
spy.mockRestore();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('surfaces an unhandled window error and dismisses it', async () => {
|
|
34
|
+
initDevErrorOverlay();
|
|
35
|
+
const { findByRole, queryByRole, getByText } = render(<DevErrorOverlay />);
|
|
36
|
+
act(() => {
|
|
37
|
+
window.dispatchEvent(new ErrorEvent('error', { error: new Error('async boom') }));
|
|
38
|
+
});
|
|
39
|
+
const alert = await findByRole('alert');
|
|
40
|
+
expect(alert.textContent).toContain('async boom');
|
|
41
|
+
fireEvent.click(getByText('Dismiss'));
|
|
42
|
+
expect(queryByRole('alert')).toBeNull();
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -17,7 +17,7 @@ const routes: RouteDef[] = [
|
|
|
17
17
|
load: () => Promise.resolve({ default: () => <div>HOME</div> }),
|
|
18
18
|
},
|
|
19
19
|
{
|
|
20
|
-
// Page chunk never resolves, so the route stays suspended
|
|
20
|
+
// Page chunk never resolves, so the route stays suspended, exercising the fallback path.
|
|
21
21
|
pattern: '/slow',
|
|
22
22
|
load: () => new Promise<{ default: () => null }>(() => undefined),
|
|
23
23
|
loading: () => Promise.resolve({ default: () => <div>LOADING</div> }),
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { afterEach, describe, expect, it } from 'vitest';
|
|
3
|
+
import { cleanup, render } from '@testing-library/react';
|
|
4
|
+
|
|
5
|
+
import { Metadata, useMetadata } from '../../src/client/head/metadata';
|
|
6
|
+
import { setRouteHead } from '../../src/client/head/head';
|
|
7
|
+
|
|
8
|
+
afterEach(() => {
|
|
9
|
+
cleanup();
|
|
10
|
+
setRouteHead(null);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
const meta = (key: string): string | null =>
|
|
14
|
+
document.head.querySelector(`meta[${key}]`)?.getAttribute('content') ?? null;
|
|
15
|
+
|
|
16
|
+
describe('useMetadata / <Metadata>', () => {
|
|
17
|
+
it('applies a full Metadata object to the document head from a component', () => {
|
|
18
|
+
function Article() {
|
|
19
|
+
useMetadata({
|
|
20
|
+
title: 'Article',
|
|
21
|
+
description: 'an article',
|
|
22
|
+
openGraph: { title: 'og title', type: 'website' },
|
|
23
|
+
});
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
render(<Article />);
|
|
27
|
+
expect(document.title).toBe('Article');
|
|
28
|
+
expect(meta('name="description"')).toBe('an article');
|
|
29
|
+
expect(meta('property="og:title"')).toBe('og title');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('reverts the head when the component unmounts', () => {
|
|
33
|
+
function Article() {
|
|
34
|
+
useMetadata({ title: 'Temp' });
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
const { unmount } = render(<Article />);
|
|
38
|
+
expect(document.title).toBe('Temp');
|
|
39
|
+
unmount();
|
|
40
|
+
expect(document.title).not.toBe('Temp');
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('the declarative <Metadata> form applies too', () => {
|
|
44
|
+
render(<Metadata title="Declarative" />);
|
|
45
|
+
expect(document.title).toBe('Declarative');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("a route's metadata still wins over a component's useMetadata", () => {
|
|
49
|
+
function Article() {
|
|
50
|
+
useMetadata({ title: 'Component' });
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
render(<Article />);
|
|
54
|
+
// The route baseline (applied last) takes precedence for keys it sets.
|
|
55
|
+
setRouteHead({ title: 'Route' });
|
|
56
|
+
expect(document.title).toBe('Route');
|
|
57
|
+
});
|
|
58
|
+
});
|
package/test/seo.test.ts
CHANGED
|
@@ -1,164 +1,164 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
|
|
3
|
-
import type { ScannedRoute } from '../src/compiler/routes';
|
|
4
|
-
import {
|
|
5
|
-
injectSeoHtml,
|
|
6
|
-
llmsTxt,
|
|
7
|
-
robotsTxt,
|
|
8
|
-
routeSeo,
|
|
9
|
-
seoHeadTags,
|
|
10
|
-
sitemapXml,
|
|
11
|
-
} from '../src/compiler/seo';
|
|
12
|
-
|
|
13
|
-
const routes: ScannedRoute[] = [
|
|
14
|
-
{ file: 'a', pattern: '/' },
|
|
15
|
-
{ file: 'b', pattern: '/about' },
|
|
16
|
-
{ file: 'c', pattern: '/blog/:id' }, // dynamic
|
|
17
|
-
{ file: 'd', pattern: '/photo/:id', slot: 'modal', intercept: true }, // slot/intercept
|
|
18
|
-
];
|
|
19
|
-
|
|
20
|
-
describe('seoHeadTags', () => {
|
|
21
|
-
it('bakes description, OG, canonical, preconnect, and JSON-LD', () => {
|
|
22
|
-
const html = seoHeadTags({
|
|
23
|
-
url: 'https://x.test',
|
|
24
|
-
title: 'Home',
|
|
25
|
-
description: 'desc',
|
|
26
|
-
openGraph: { type: 'website', image: 'https://x.test/og.png' },
|
|
27
|
-
preconnect: ['https://cdn.test'],
|
|
28
|
-
jsonLd: { '@context': 'https://schema.org', '@type': 'WebSite' },
|
|
29
|
-
});
|
|
30
|
-
expect(html).toContain('<meta name="description" content="desc" />');
|
|
31
|
-
expect(html).toContain('<meta property="og:title" content="Home" />');
|
|
32
|
-
expect(html).toContain('<meta property="og:image" content="https://x.test/og.png" />');
|
|
33
|
-
expect(html).toContain('<link rel="canonical" href="https://x.test" />');
|
|
34
|
-
expect(html).toContain('<link rel="preconnect" href="https://cdn.test" />');
|
|
35
|
-
expect(html).toContain('application/ld+json');
|
|
36
|
-
expect(html).toContain('"@type":"WebSite"');
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('escapes attribute values', () => {
|
|
40
|
-
expect(seoHeadTags({ description: 'a "b" <c>' })).toContain(
|
|
41
|
-
'content="a "b" <c>"',
|
|
42
|
-
);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it('renders a full Twitter card + OG image dimensions + fb:app_id', () => {
|
|
46
|
-
const html = seoHeadTags({
|
|
47
|
-
title: 'Home',
|
|
48
|
-
description: 'd',
|
|
49
|
-
openGraph: {
|
|
50
|
-
image: 'https://x.test/og.png',
|
|
51
|
-
imageAlt: 'alt',
|
|
52
|
-
imageWidth: 1200,
|
|
53
|
-
imageHeight: 630,
|
|
54
|
-
},
|
|
55
|
-
twitter: { site: '@x' },
|
|
56
|
-
facebook: { appId: '123' },
|
|
57
|
-
});
|
|
58
|
-
expect(html).toContain('<meta name="twitter:card" content="summary_large_image" />');
|
|
59
|
-
expect(html).toContain('<meta name="twitter:site" content="@x" />');
|
|
60
|
-
expect(html).toContain('<meta name="twitter:title" content="Home" />');
|
|
61
|
-
expect(html).toContain('<meta name="twitter:image" content="https://x.test/og.png" />');
|
|
62
|
-
expect(html).toContain('<meta property="og:image:width" content="1200" />');
|
|
63
|
-
expect(html).toContain('<meta property="og:image:alt" content="alt" />');
|
|
64
|
-
expect(html).toContain('<meta property="fb:app_id" content="123" />');
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it('neutralizes </script> in JSON-LD (no script breakout)', () => {
|
|
68
|
-
const html = seoHeadTags({ jsonLd: { x: '</script><img src=x onerror=alert(1)>' } });
|
|
69
|
-
expect(html).not.toContain('</script><img');
|
|
70
|
-
expect(html).toContain('\\u003c/script');
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
describe('routeSeo', () => {
|
|
75
|
-
it("overlays a route's metadata over the site defaults and points URLs at the route", () => {
|
|
76
|
-
const site = { url: 'https://x.test', title: 'Site', description: 'site desc' };
|
|
77
|
-
const out = routeSeo(site, { title: 'About', description: 'about desc' }, '/about');
|
|
78
|
-
expect(out.title).toBe('About');
|
|
79
|
-
expect(out.description).toBe('about desc');
|
|
80
|
-
expect(out.url).toBe('https://x.test/about');
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
it('falls back to the site defaults when a route has no metadata', () => {
|
|
84
|
-
const site = { url: 'https://x.test', title: 'Site' };
|
|
85
|
-
expect(routeSeo(site, null, '/x')).toMatchObject({
|
|
86
|
-
title: 'Site',
|
|
87
|
-
url: 'https://x.test/x',
|
|
88
|
-
});
|
|
89
|
-
});
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
describe('injectSeoHtml', () => {
|
|
93
|
-
it('replaces the title + description and inserts the rest before </head>', () => {
|
|
94
|
-
const shell =
|
|
95
|
-
'<!doctype html><html><head><title>old</title><meta name="description" content="" /></head><body></body></html>';
|
|
96
|
-
const out = injectSeoHtml(shell, {
|
|
97
|
-
title: 'New',
|
|
98
|
-
description: 'fresh',
|
|
99
|
-
url: 'https://x.test',
|
|
100
|
-
});
|
|
101
|
-
expect(out).toContain('<title>New</title>');
|
|
102
|
-
expect(out).not.toContain('<title>old</title>');
|
|
103
|
-
expect(out.match(/name="description"/g)).toHaveLength(1);
|
|
104
|
-
expect(out).toContain('content="fresh"');
|
|
105
|
-
expect(out).toContain('<link rel="canonical" href="https://x.test" />');
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
describe('robotsTxt', () => {
|
|
110
|
-
it('allows all + lists AI crawlers + links the sitemap by default', () => {
|
|
111
|
-
const txt = robotsTxt({ url: 'https://x.test' });
|
|
112
|
-
expect(txt).toContain('User-agent: *');
|
|
113
|
-
expect(txt).toContain('Allow: /');
|
|
114
|
-
expect(txt).toContain('User-agent: GPTBot');
|
|
115
|
-
expect(txt).toContain('User-agent: ClaudeBot');
|
|
116
|
-
expect(txt).toContain('Sitemap: https://x.test/sitemap.xml');
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
it('disallows AI crawlers when ai: "disallow"', () => {
|
|
120
|
-
const txt = robotsTxt({ url: 'https://x.test', robots: { ai: 'disallow' } });
|
|
121
|
-
expect(txt).toMatch(/User-agent: GPTBot\nDisallow: \//);
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
it('is empty when robots: false', () => {
|
|
125
|
-
expect(robotsTxt({ robots: false })).toBe('');
|
|
126
|
-
});
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
describe('sitemapXml', () => {
|
|
130
|
-
it('lists only static routes, absolute', () => {
|
|
131
|
-
const xml = sitemapXml({ url: 'https://x.test' }, routes);
|
|
132
|
-
expect(xml).toContain('<loc>https://x.test</loc>');
|
|
133
|
-
expect(xml).toContain('<loc>https://x.test/about</loc>');
|
|
134
|
-
expect(xml).not.toContain(':id');
|
|
135
|
-
expect(xml).not.toContain('/photo');
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
it('is empty without a base url', () => {
|
|
139
|
-
expect(sitemapXml({}, routes)).toBe('');
|
|
140
|
-
});
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
describe('llmsTxt', () => {
|
|
144
|
-
it('renders title, summary, instructions, and pages', () => {
|
|
145
|
-
const txt = llmsTxt(
|
|
146
|
-
{
|
|
147
|
-
url: 'https://x.test',
|
|
148
|
-
title: 'My Site',
|
|
149
|
-
description: 'a site',
|
|
150
|
-
llms: { instructions: 'Be nice.' },
|
|
151
|
-
},
|
|
152
|
-
routes,
|
|
153
|
-
);
|
|
154
|
-
expect(txt).toContain('# My Site');
|
|
155
|
-
expect(txt).toContain('> a site');
|
|
156
|
-
expect(txt).toContain('Be nice.');
|
|
157
|
-
expect(txt).toContain('[Home](https://x.test)');
|
|
158
|
-
expect(txt).toContain('[/about](https://x.test/about)');
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
it('is empty when llms: false', () => {
|
|
162
|
-
expect(llmsTxt({ llms: false }, routes)).toBe('');
|
|
163
|
-
});
|
|
164
|
-
});
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import type { ScannedRoute } from '../src/compiler/routes';
|
|
4
|
+
import {
|
|
5
|
+
injectSeoHtml,
|
|
6
|
+
llmsTxt,
|
|
7
|
+
robotsTxt,
|
|
8
|
+
routeSeo,
|
|
9
|
+
seoHeadTags,
|
|
10
|
+
sitemapXml,
|
|
11
|
+
} from '../src/compiler/seo';
|
|
12
|
+
|
|
13
|
+
const routes: ScannedRoute[] = [
|
|
14
|
+
{ file: 'a', pattern: '/' },
|
|
15
|
+
{ file: 'b', pattern: '/about' },
|
|
16
|
+
{ file: 'c', pattern: '/blog/:id' }, // dynamic, excluded from sitemap
|
|
17
|
+
{ file: 'd', pattern: '/photo/:id', slot: 'modal', intercept: true }, // slot/intercept, excluded
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
describe('seoHeadTags', () => {
|
|
21
|
+
it('bakes description, OG, canonical, preconnect, and JSON-LD', () => {
|
|
22
|
+
const html = seoHeadTags({
|
|
23
|
+
url: 'https://x.test',
|
|
24
|
+
title: 'Home',
|
|
25
|
+
description: 'desc',
|
|
26
|
+
openGraph: { type: 'website', image: 'https://x.test/og.png' },
|
|
27
|
+
preconnect: ['https://cdn.test'],
|
|
28
|
+
jsonLd: { '@context': 'https://schema.org', '@type': 'WebSite' },
|
|
29
|
+
});
|
|
30
|
+
expect(html).toContain('<meta name="description" content="desc" />');
|
|
31
|
+
expect(html).toContain('<meta property="og:title" content="Home" />');
|
|
32
|
+
expect(html).toContain('<meta property="og:image" content="https://x.test/og.png" />');
|
|
33
|
+
expect(html).toContain('<link rel="canonical" href="https://x.test" />');
|
|
34
|
+
expect(html).toContain('<link rel="preconnect" href="https://cdn.test" />');
|
|
35
|
+
expect(html).toContain('application/ld+json');
|
|
36
|
+
expect(html).toContain('"@type":"WebSite"');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('escapes attribute values', () => {
|
|
40
|
+
expect(seoHeadTags({ description: 'a "b" <c>' })).toContain(
|
|
41
|
+
'content="a "b" <c>"',
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('renders a full Twitter card + OG image dimensions + fb:app_id', () => {
|
|
46
|
+
const html = seoHeadTags({
|
|
47
|
+
title: 'Home',
|
|
48
|
+
description: 'd',
|
|
49
|
+
openGraph: {
|
|
50
|
+
image: 'https://x.test/og.png',
|
|
51
|
+
imageAlt: 'alt',
|
|
52
|
+
imageWidth: 1200,
|
|
53
|
+
imageHeight: 630,
|
|
54
|
+
},
|
|
55
|
+
twitter: { site: '@x' },
|
|
56
|
+
facebook: { appId: '123' },
|
|
57
|
+
});
|
|
58
|
+
expect(html).toContain('<meta name="twitter:card" content="summary_large_image" />');
|
|
59
|
+
expect(html).toContain('<meta name="twitter:site" content="@x" />');
|
|
60
|
+
expect(html).toContain('<meta name="twitter:title" content="Home" />');
|
|
61
|
+
expect(html).toContain('<meta name="twitter:image" content="https://x.test/og.png" />');
|
|
62
|
+
expect(html).toContain('<meta property="og:image:width" content="1200" />');
|
|
63
|
+
expect(html).toContain('<meta property="og:image:alt" content="alt" />');
|
|
64
|
+
expect(html).toContain('<meta property="fb:app_id" content="123" />');
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('neutralizes </script> in JSON-LD (no script breakout)', () => {
|
|
68
|
+
const html = seoHeadTags({ jsonLd: { x: '</script><img src=x onerror=alert(1)>' } });
|
|
69
|
+
expect(html).not.toContain('</script><img');
|
|
70
|
+
expect(html).toContain('\\u003c/script');
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe('routeSeo', () => {
|
|
75
|
+
it("overlays a route's metadata over the site defaults and points URLs at the route", () => {
|
|
76
|
+
const site = { url: 'https://x.test', title: 'Site', description: 'site desc' };
|
|
77
|
+
const out = routeSeo(site, { title: 'About', description: 'about desc' }, '/about');
|
|
78
|
+
expect(out.title).toBe('About');
|
|
79
|
+
expect(out.description).toBe('about desc');
|
|
80
|
+
expect(out.url).toBe('https://x.test/about');
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('falls back to the site defaults when a route has no metadata', () => {
|
|
84
|
+
const site = { url: 'https://x.test', title: 'Site' };
|
|
85
|
+
expect(routeSeo(site, null, '/x')).toMatchObject({
|
|
86
|
+
title: 'Site',
|
|
87
|
+
url: 'https://x.test/x',
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe('injectSeoHtml', () => {
|
|
93
|
+
it('replaces the title + description and inserts the rest before </head>', () => {
|
|
94
|
+
const shell =
|
|
95
|
+
'<!doctype html><html><head><title>old</title><meta name="description" content="" /></head><body></body></html>';
|
|
96
|
+
const out = injectSeoHtml(shell, {
|
|
97
|
+
title: 'New',
|
|
98
|
+
description: 'fresh',
|
|
99
|
+
url: 'https://x.test',
|
|
100
|
+
});
|
|
101
|
+
expect(out).toContain('<title>New</title>');
|
|
102
|
+
expect(out).not.toContain('<title>old</title>');
|
|
103
|
+
expect(out.match(/name="description"/g)).toHaveLength(1);
|
|
104
|
+
expect(out).toContain('content="fresh"');
|
|
105
|
+
expect(out).toContain('<link rel="canonical" href="https://x.test" />');
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
describe('robotsTxt', () => {
|
|
110
|
+
it('allows all + lists AI crawlers + links the sitemap by default', () => {
|
|
111
|
+
const txt = robotsTxt({ url: 'https://x.test' });
|
|
112
|
+
expect(txt).toContain('User-agent: *');
|
|
113
|
+
expect(txt).toContain('Allow: /');
|
|
114
|
+
expect(txt).toContain('User-agent: GPTBot');
|
|
115
|
+
expect(txt).toContain('User-agent: ClaudeBot');
|
|
116
|
+
expect(txt).toContain('Sitemap: https://x.test/sitemap.xml');
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('disallows AI crawlers when ai: "disallow"', () => {
|
|
120
|
+
const txt = robotsTxt({ url: 'https://x.test', robots: { ai: 'disallow' } });
|
|
121
|
+
expect(txt).toMatch(/User-agent: GPTBot\nDisallow: \//);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('is empty when robots: false', () => {
|
|
125
|
+
expect(robotsTxt({ robots: false })).toBe('');
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
describe('sitemapXml', () => {
|
|
130
|
+
it('lists only static routes, absolute', () => {
|
|
131
|
+
const xml = sitemapXml({ url: 'https://x.test' }, routes);
|
|
132
|
+
expect(xml).toContain('<loc>https://x.test</loc>');
|
|
133
|
+
expect(xml).toContain('<loc>https://x.test/about</loc>');
|
|
134
|
+
expect(xml).not.toContain(':id');
|
|
135
|
+
expect(xml).not.toContain('/photo');
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('is empty without a base url', () => {
|
|
139
|
+
expect(sitemapXml({}, routes)).toBe('');
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
describe('llmsTxt', () => {
|
|
144
|
+
it('renders title, summary, instructions, and pages', () => {
|
|
145
|
+
const txt = llmsTxt(
|
|
146
|
+
{
|
|
147
|
+
url: 'https://x.test',
|
|
148
|
+
title: 'My Site',
|
|
149
|
+
description: 'a site',
|
|
150
|
+
llms: { instructions: 'Be nice.' },
|
|
151
|
+
},
|
|
152
|
+
routes,
|
|
153
|
+
);
|
|
154
|
+
expect(txt).toContain('# My Site');
|
|
155
|
+
expect(txt).toContain('> a site');
|
|
156
|
+
expect(txt).toContain('Be nice.');
|
|
157
|
+
expect(txt).toContain('[Home](https://x.test)');
|
|
158
|
+
expect(txt).toContain('[/about](https://x.test/about)');
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('is empty when llms: false', () => {
|
|
162
|
+
expect(llmsTxt({ llms: false }, routes)).toBe('');
|
|
163
|
+
});
|
|
164
|
+
});
|
package/test/ssg.test.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { sitemapXml, type SeoConfig } from '../src/compiler/seo';
|
|
4
|
+
import { fillPattern } from '../src/compiler/ssg';
|
|
5
|
+
import { type ScannedRoute } from '../src/compiler/routes';
|
|
6
|
+
|
|
7
|
+
describe('fillPattern', () => {
|
|
8
|
+
it('substitutes :param and *catch-all segments', () => {
|
|
9
|
+
expect(fillPattern('/:a/:b/:c', { a: 'x', b: 'y', c: 'z' })).toBe('/x/y/z');
|
|
10
|
+
expect(fillPattern('/blog/:id', { id: '42' })).toBe('/blog/42');
|
|
11
|
+
expect(fillPattern('/docs/*slug', { slug: ['a', 'b'] })).toBe('/docs/a/b');
|
|
12
|
+
expect(fillPattern('/static', {})).toBe('/static');
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe('sitemapXml with SSG paths', () => {
|
|
17
|
+
const seo: SeoConfig = { url: 'https://x.dev' };
|
|
18
|
+
const routes: ScannedRoute[] = [
|
|
19
|
+
{ file: 'a', pattern: '/' },
|
|
20
|
+
{ file: 'b', pattern: '/about' },
|
|
21
|
+
{ file: 'c', pattern: '/blog/:id' },
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
it('lists static routes plus enumerated SSG URLs, deduped, never the bare pattern', () => {
|
|
25
|
+
const xml = sitemapXml(seo, routes, ['/blog/1', '/blog/2', '/about']);
|
|
26
|
+
expect(xml).toContain('https://x.dev/blog/1');
|
|
27
|
+
expect(xml).toContain('https://x.dev/blog/2');
|
|
28
|
+
expect(xml).toContain('https://x.dev/about');
|
|
29
|
+
expect(xml).not.toContain('/blog/:id'); // dynamic pattern is never listed literally
|
|
30
|
+
expect((xml.match(/<loc>[^<]*\/about<\/loc>/g) ?? []).length).toBe(1); // deduped
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('is empty without a base url', () => {
|
|
34
|
+
expect(sitemapXml({}, routes, ['/blog/1'])).toBe('');
|
|
35
|
+
});
|
|
36
|
+
});
|