Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/plugins/prerender-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
return message;
};

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

let prerender;
Expand Down Expand Up @@ -420,9 +420,12 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
} catch {}
}

/** @type {import('./types.d.ts').PrerenderResult | string} */
let result;
try {
result = await prerender({ ssr: true, url: route.url, route });
/** @type {import('./types.d.ts').PrerenderOptions} */
const options = { ssr: true, url: route.url, route };
result = await prerender(options);
Comment on lines +426 to +428
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need for this line to change

Suggested change
/** @type {import('./types.d.ts').PrerenderOptions} */
const options = { ssr: true, url: route.url, route };
result = await prerender(options);
result = await prerender({ ssr: true, url: route.url, route });

} catch (e) {
const message = await handlePrerenderError(e);
this.error(message);
Expand All @@ -438,7 +441,7 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
head = { lang: '', title: '', elements: new Set() };

// Add any discovered links to the list of routes to pre-render:
if (result.links) {
if (typeof result === 'object' && result.links) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need for this line to change

Suggested change
if (typeof result === 'object' && result.links) {
if (result.links) {

for (let url of result.links) {
const parsed = new URL(url, 'http://localhost');
url = parsed.pathname.replace(/\/$/, '') || '/';
Expand Down Expand Up @@ -494,9 +497,9 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
const target = htmlDoc.querySelector(renderTarget);
if (!target)
this.error(
result.renderTarget == 'body'
renderTarget == 'body'
? '`renderTarget` was not specified in plugin options and <body> does not exist in input HTML template'
: `Unable to detect prerender renderTarget "${result.selector}" in input HTML template`,
: `Unable to detect prerender renderTarget "${renderTarget}" in input HTML template`,
Comment on lines -497 to +502
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oof, good spot on both (or good spot from the types). Changed this API ages ago and I guess it slipped through.

);
target.insertAdjacentHTML('afterbegin', body);

Expand Down
16 changes: 16 additions & 0 deletions src/plugins/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,19 @@ export interface PrerenderedRoute {
url: string;
_discoveredBy?: PrerenderedRoute;
}

export interface PrerenderOptions {
ssr: true;
url: string;
route: PrerenderedRoute;
}

export interface PrerenderResult {
html?: string;
head?: Partial<Head>;
links?: Set<string>;
/**
* @description Caution: should be a valid JSON object
*/
data?: any;
}
Comment on lines +24 to +32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you spotted above in the source file, this should be a union with a string. return '<h1>Hello World</h1>'; is perfectly valid too.