@dr.pogodin/react-utils
Version:
Collection of generic ReactJS components and utils
115 lines (114 loc) • 4.26 kB
TypeScript
/**
* ExpressJS middleware for server-side rendering of a ReactJS app.
*/
import type { Request, RequestHandler } from 'express';
import type { ComponentType } from 'react';
import type { Configuration } from 'webpack';
import winston from 'winston';
import { SsrContext } from '@dr.pogodin/react-global-state';
import { type BuildInfoT } from '../shared/utils/isomorphy/buildInfo';
import type { ChunkGroupsT, SsrContextT } from '../shared/utils/globalState';
interface LogMethodI {
(level: string, message: string, ...meta: unknown[]): void;
}
interface LeveledLogMethodI {
(message: string, ...meta: unknown[]): void;
}
export interface LoggerI {
debug: LeveledLogMethodI;
error: LeveledLogMethodI;
info: LeveledLogMethodI;
log: LogMethodI;
warn: LeveledLogMethodI;
}
export declare enum SCRIPT_LOCATIONS {
BODY_OPEN = "BODY_OPEN",
DEFAULT = "DEFAULT",
HEAD_OPEN = "HEAD_OPEN"
}
export declare class ServerSsrContext<StateT> extends SsrContext<StateT> implements SsrContextT<StateT> {
chunkGroups: ChunkGroupsT;
chunks: string[];
redirectTo?: string;
req: Request;
status: number;
constructor(req: Request, chunkGroups: ChunkGroupsT, initialState?: StateT);
}
type ScriptT = {
code: string;
location: SCRIPT_LOCATIONS;
};
/**
* Given an incoming HTTP requests, it deduces whether Brotli-encoded responses
* are acceptable to the caller.
* @param req
*/
export declare function isBrotliAcceptable(req: Request): boolean;
/**
* Creates a new default (Winston) logger.
* @param {object} [options={}]
* @param {string} [options.defaultLogLevel='info']
* @return {object}
*/
export declare function newDefaultLogger({ defaultLogLevel, }?: {
defaultLogLevel?: string | undefined;
}): winston.Logger;
export type ConfigT = {
[key: string]: ConfigT | string;
};
export type BeforeRenderResT = {
configToInject?: ConfigT;
extraScripts?: Array<ScriptT | string>;
initialState?: unknown;
};
export type BeforeRenderT = (req: Request, config: ConfigT) => BeforeRenderResT | Promise<BeforeRenderResT>;
type CacheRefT = {
key: string;
maxage?: number;
};
export type OptionsT = {
Application?: ComponentType;
beforeRender?: BeforeRenderT;
buildInfo?: BuildInfoT;
defaultLoggerLogLevel?: string;
favicon?: string;
logger?: LoggerI;
maxSsrRounds?: number;
noCsp?: boolean;
staticCacheSize?: number;
ssrTimeout?: number;
staticCacheController?: (req: Request) => CacheRefT | null | undefined;
};
/**
* Creates the middleware.
* @param webpackConfig
* @param options Additional options:
* @param [options.Application] The root ReactJS component of
* the app to use for the server-side rendering. When not provided
* the server-side rendering is disabled.
* @param [options.buildInfo] "Build info" object to use. If provided,
* it will be used, instead of trying to load from the filesystem the one
* generated by the Webpack build. It is intended for test environments,
* where passing this stuff via file system is no bueno.
* @param [options.favicon] `true` will include favicon
* link into the rendered HTML templates.
* @param [options.noCsp] `true` means that no
* Content-Security-Policy (CSP) is used by server, thus the renderer
* may cut a few corners.
* @param [options.maxSsrRounds=10] Maximum number of SSR rounds.
* @param [options.ssrTimeout=1000] SSR timeout in milliseconds,
* defaults to 1 second.
* @param [options.staticCacheSize=1.e7] The maximum
* static cache size in bytes. Defaults to ~10 MB.
* @param [options.staticCacheController] When given, it activates,
* and controls the static caching of generated HTML markup. When this function
* is provided, on each incoming request it is triggered with the request
* passed in as the argument. To attempt to serve the response from the cache
* it should return the object with the following fields:
* - `key: string` – the cache key for the response;
* - `maxage?: number` – the maximum age of cached result in ms.
* If undefined - infinite age is assumed.
* @return Created middleware.
*/
export default function factory(webpackConfig: Configuration, options: OptionsT): RequestHandler;
export {};