Skip to content

Commit

Permalink
cleanup and more renames
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenlautier committed Sep 10, 2024
1 parent b700055 commit 1c78eab
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/lib/prerendering/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { BootFn, BootFuncParams, RenderToStringResult, RenderResult, RedirectResult } from "./prerendering.model";
export { BootFn, BootFnParams, RenderToStringResult, RenderResult, RedirectResult } from "./prerendering.model";
export { createServerRenderer } from "./prerendering";
27 changes: 14 additions & 13 deletions src/lib/prerendering/prerendering.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export interface RenderToStringFn {
(
// callback: RenderToStringCallback,
applicationBasePath: string,
bootModule: BootModuleInfo,
absoluteRequestUrl: string,
Expand All @@ -11,10 +10,6 @@ export interface RenderToStringFn {
): Promise<RenderResult>;
}

// export interface RenderToStringCallback {
// (error: any, result?: RenderResult): void;
// }

export interface RenderToStringResult {
html: string;
statusCode?: number;
Expand All @@ -28,17 +23,23 @@ export interface RedirectResult {
export type RenderResult = RenderToStringResult | RedirectResult;

export interface BootFn {
(params: BootFuncParams): Promise<RenderResult>;
(params: BootFnParams): Promise<RenderResult>;
}

export interface BootFuncParams {
location: unknown; // e.g., Location object containing information '/some/path'
origin: string; // e.g., 'https://example.com:1234'
url: string; // e.g., '/some/path'
baseUrl: string; // e.g., '' or '/myVirtualDir'
absoluteUrl: string; // e.g., 'https://example.com:1234/some/path'
export interface BootFnParams {
/** e.g., Location object containing information '/some/path' */
location: unknown;
/** e.g., 'https://example.com:1234' */
origin: string;
/** // e.g. '/some/path' */
url: string;
/** e.g. '' or '/myVirtualDir' */
baseUrl: string;
/** e.g. 'https://example.com:1234/some/path' */
absoluteUrl: string;
domainTasks: Promise<unknown>;
data: unknown; // any custom object passed through from .NET
/** any custom object passed through from .NET */
data: unknown;
}

export interface BootModuleInfo {
Expand Down
11 changes: 2 additions & 9 deletions src/lib/prerendering/prerendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ import * as url from "url";
import * as domain from "domain";
import { run as domainTaskRun, baseUrl as domainTaskBaseUrl } from "domain-task/main";

import { BootFn, BootFuncParams, BootModuleInfo, RenderResult, RenderToStringFn } from "./prerendering.model";
import { BootFn, BootFnParams, BootModuleInfo, RenderResult, RenderToStringFn } from "./prerendering.model";

const defaultTimeoutMilliseconds = 30 * 1000;

// REF: based on https://github.com/aspnet/JavaScriptServices/blob/master/src/Microsoft.AspNetCore.SpaServices/npm/aspnet-prerendering/src/Prerendering.ts
export function createServerRenderer(bootFunc: BootFn): RenderToStringFn {
console.warn(">>>> createServerRenderer PRE FN x2");
const resultFunc: RenderToStringFn & { isServerRenderer: boolean } = (
// callback: RenderToStringCallback,
applicationBasePath: string,
bootModule: BootModuleInfo,
absoluteRequestUrl: string,
Expand All @@ -20,7 +18,6 @@ export function createServerRenderer(bootFunc: BootFn): RenderToStringFn {
overrideTimeoutMilliseconds: number,
requestPathBase: string
) => {
console.warn(">>>> createServerRenderer applicationBasePath", applicationBasePath);
let renderPromiseResolve: (value?: RenderResult) => void;
let renderPromiseReject: (reason?: any) => void;
const renderPromise = new Promise<RenderResult>((resolve, reject) => {
Expand All @@ -35,7 +32,7 @@ export function createServerRenderer(bootFunc: BootFn): RenderToStringFn {
domainTaskCompletionPromiseResolve = resolve;
});
const parsedAbsoluteRequestUrl = url.parse(absoluteRequestUrl);
const params: BootFuncParams = {
const params: BootFnParams = {
// It's helpful for boot funcs to receive the query as a key-value object, so parse it here
// e.g., react-redux-router requires location.query to be a key-value object for consistency with client-side behaviour
location: url.parse(requestPathAndQuery, /* parseQueryString */ true),
Expand All @@ -61,7 +58,6 @@ export function createServerRenderer(bootFunc: BootFn): RenderToStringFn {
// Begin rendering, and apply a timeout
const bootFuncPromise = bootFunc(params);
if (!bootFuncPromise || typeof bootFuncPromise.then !== "function") {
// callback(`Prerendering failed because the boot function in ${bootModule.moduleName} did not return a promise.`, null);
renderPromiseReject(`Prerendering failed because the boot function in ${bootModule.moduleName} did not return a promise.`);
return;
}
Expand All @@ -75,15 +71,12 @@ export function createServerRenderer(bootFunc: BootFn): RenderToStringFn {

// Actually perform the rendering
bootFuncPromiseWithTimeout.then(successResult => {
// callback(null, successResult);
renderPromiseResolve(successResult);
}, error => {
// callback(error, null);
renderPromiseReject(error);
});
}, /* completion callback */ errorOrNothing => {
if (errorOrNothing) {
// callback(errorOrNothing, null);
renderPromiseReject(errorOrNothing);
} else {
// There are no more ongoing domain tasks (typically data access operations), so we can resolve
Expand Down

0 comments on commit 1c78eab

Please sign in to comment.