Skip to content

Commit bb095cf

Browse files
committed
feat: types for prerender options and result; better result checks
1 parent 34efcd2 commit bb095cf

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

src/plugins/prerender-plugin.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
134134
// Only required for Vite 5 and older. In 6+, this is handled by the
135135
// Environment API (`applyToEnvironment`)
136136
if (config.build?.ssr) {
137-
ssrBuild = true
137+
ssrBuild = true;
138138
return;
139139
}
140140

@@ -319,10 +319,7 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
319319

320320
const assetPath = path.join(tmpDir, output);
321321
await fs.mkdir(path.dirname(assetPath), { recursive: true });
322-
await fs.writeFile(
323-
assetPath,
324-
/** @type {OutputChunk} */ (bundle[output]).code,
325-
);
322+
await fs.writeFile(assetPath, /** @type {OutputChunk} */ (bundle[output]).code);
326323

327324
if (/** @type {OutputChunk} */ (bundle[output]).exports?.includes('prerender')) {
328325
prerenderEntry = /** @type {OutputChunk} */ (bundle[output]);
@@ -348,9 +345,7 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
348345
// do something in browsers only
349346
}`.replace(/^ {20}/gm, '');
350347

351-
const stack = StackTraceParse(e).find((s) =>
352-
s.getFileName()?.includes(tmpDirId),
353-
);
348+
const stack = StackTraceParse(e).find((s) => s.getFileName()?.includes(tmpDirId));
354349

355350
const sourceMapContent = prerenderEntry.map;
356351
if (stack && sourceMapContent) {
@@ -382,14 +377,12 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
382377
return message;
383378
};
384379

385-
/** @type {import('./types.d.ts').Head} */
380+
/** @type {Partial<import('./types.d.ts').Head>} */
386381
let head = { lang: '', title: '', elements: new Set() };
387382

388383
let prerender;
389384
try {
390-
const m = await import(
391-
`file://${path.join(tmpDir, prerenderEntry.fileName)}`
392-
);
385+
const m = await import(`file://${path.join(tmpDir, prerenderEntry.fileName)}`);
393386
prerender = m.prerender;
394387
} catch (e) {
395388
const message = await handlePrerenderError(e);
@@ -420,9 +413,12 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
420413
} catch {}
421414
}
422415

416+
/** @type {import('./types.d.ts').PrerenderResult | string} */
423417
let result;
424418
try {
425-
result = await prerender({ ssr: true, url: route.url, route });
419+
/** @type {import('./types.d.ts').PrerenderOptions} */
420+
const options = { ssr: true, url: route.url, route };
421+
result = await prerender(options);
426422
} catch (e) {
427423
const message = await handlePrerenderError(e);
428424
this.error(message);
@@ -438,7 +434,7 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
438434
head = { lang: '', title: '', elements: new Set() };
439435

440436
// Add any discovered links to the list of routes to pre-render:
441-
if (result.links) {
437+
if (typeof result === 'object' && result.links) {
442438
for (let url of result.links) {
443439
const parsed = new URL(url, 'http://localhost');
444440
url = parsed.pathname.replace(/\/$/, '') || '/';
@@ -494,16 +490,16 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
494490
const target = htmlDoc.querySelector(renderTarget);
495491
if (!target)
496492
this.error(
497-
result.renderTarget == 'body'
493+
renderTarget == 'body'
498494
? '`renderTarget` was not specified in plugin options and <body> does not exist in input HTML template'
499-
: `Unable to detect prerender renderTarget "${result.selector}" in input HTML template`,
495+
: `Unable to detect prerender renderTarget "${renderTarget}" in input HTML template`,
500496
);
501497
target.insertAdjacentHTML('afterbegin', body);
502498

499+
const index = /** @type {OutputAsset} */ (bundle['index.html']);
503500
// Add generated HTML to compilation:
504501
route.url == '/'
505-
? (/** @type {OutputAsset} */ (bundle['index.html']).source =
506-
htmlDoc.toString())
502+
? (index.source = htmlDoc.toString())
507503
: this.emitFile({
508504
type: 'asset',
509505
fileName: assetName,
@@ -531,6 +527,6 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
531527
}
532528
}
533529
}
534-
}
530+
},
535531
};
536532
}

src/plugins/types.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,19 @@ export interface PrerenderedRoute {
1414
url: string;
1515
_discoveredBy?: PrerenderedRoute;
1616
}
17+
18+
export interface PrerenderOptions {
19+
ssr: true;
20+
url: string;
21+
route: PrerenderedRoute;
22+
}
23+
24+
export interface PrerenderResult {
25+
html?: string;
26+
head?: Partial<Head>;
27+
links?: Set<string>;
28+
/**
29+
* @description Caution: this value is stringified via JSON.stringify
30+
*/
31+
data?: any;
32+
}

0 commit comments

Comments
 (0)