base-server.d.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /// <reference types="node" />
  2. /// <reference types="node" />
  3. import type { __ApiPreviewProps } from './api-utils';
  4. import type { CustomRoutes } from '../lib/load-custom-routes';
  5. import type { DomainLocale } from './config';
  6. import type { DynamicRoutes, PageChecker, Route } from './router';
  7. import type { FontManifest, FontConfig } from './font-utils';
  8. import type { LoadComponentsReturnType } from './load-components';
  9. import type { RouteMatch } from '../shared/lib/router/utils/route-matcher';
  10. import type { MiddlewareRouteMatch } from '../shared/lib/router/utils/middleware-route-matcher';
  11. import type { Params } from '../shared/lib/router/utils/route-matcher';
  12. import type { NextConfig, NextConfigComplete } from './config-shared';
  13. import type { NextParsedUrlQuery, NextUrlWithParsedQuery } from './request-meta';
  14. import type { ParsedUrlQuery } from 'querystring';
  15. import type { RenderOpts, RenderOptsPartial } from './render';
  16. import type { ResponseCacheBase } from './response-cache';
  17. import type { UrlWithParsedQuery } from 'url';
  18. import type { ServerRuntime } from 'next/types';
  19. import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin';
  20. import type { BaseNextRequest, BaseNextResponse } from './base-http';
  21. import type { PayloadOptions } from './send-payload';
  22. import type { PrerenderManifest } from '../build';
  23. import Router from './router';
  24. import RenderResult from './render-result';
  25. import { ImageConfigComplete } from '../shared/lib/image-config';
  26. import { MiddlewareMatcher } from '../build/analysis/get-page-static-info';
  27. export declare type FindComponentsResult = {
  28. components: LoadComponentsReturnType;
  29. query: NextParsedUrlQuery;
  30. };
  31. export interface RoutingItem {
  32. page: string;
  33. match: RouteMatch;
  34. re?: RegExp;
  35. }
  36. export interface MiddlewareRoutingItem {
  37. page: string;
  38. match: MiddlewareRouteMatch;
  39. matchers?: MiddlewareMatcher[];
  40. }
  41. export interface Options {
  42. /**
  43. * Object containing the configuration next.config.js
  44. */
  45. conf: NextConfig;
  46. /**
  47. * Set to false when the server was created by Next.js
  48. */
  49. customServer?: boolean;
  50. /**
  51. * Tells if Next.js is running in dev mode
  52. */
  53. dev?: boolean;
  54. /**
  55. * Where the Next project is located
  56. */
  57. dir?: string;
  58. /**
  59. * Tells if Next.js is running in a Serverless platform
  60. */
  61. minimalMode?: boolean;
  62. /**
  63. * Hide error messages containing server information
  64. */
  65. quiet?: boolean;
  66. /**
  67. * The hostname the server is running behind
  68. */
  69. hostname?: string;
  70. /**
  71. * The port the server is running behind
  72. */
  73. port?: number;
  74. /**
  75. * The HTTP Server that Next.js is running behind
  76. */
  77. httpServer?: import('http').Server;
  78. }
  79. export interface BaseRequestHandler {
  80. (req: BaseNextRequest, res: BaseNextResponse, parsedUrl?: NextUrlWithParsedQuery | undefined): Promise<void>;
  81. }
  82. export declare type RequestContext = {
  83. req: BaseNextRequest;
  84. res: BaseNextResponse;
  85. pathname: string;
  86. query: NextParsedUrlQuery;
  87. renderOpts: RenderOptsPartial;
  88. };
  89. export declare class NoFallbackError extends Error {
  90. }
  91. export declare class WrappedBuildError extends Error {
  92. innerError: Error;
  93. constructor(innerError: Error);
  94. }
  95. declare type ResponsePayload = {
  96. type: 'html' | 'json';
  97. body: RenderResult;
  98. revalidateOptions?: any;
  99. };
  100. export declare function prepareServerlessUrl(req: BaseNextRequest, query: ParsedUrlQuery): void;
  101. export default abstract class Server<ServerOptions extends Options = Options> {
  102. protected dir: string;
  103. protected quiet: boolean;
  104. protected nextConfig: NextConfigComplete;
  105. protected distDir: string;
  106. protected publicDir: string;
  107. protected hasStaticDir: boolean;
  108. protected pagesManifest?: PagesManifest;
  109. protected appPathsManifest?: PagesManifest;
  110. protected buildId: string;
  111. protected minimalMode: boolean;
  112. protected renderOpts: {
  113. poweredByHeader: boolean;
  114. buildId: string;
  115. generateEtags: boolean;
  116. runtimeConfig?: {
  117. [key: string]: any;
  118. };
  119. assetPrefix?: string;
  120. canonicalBase: string;
  121. dev?: boolean;
  122. previewProps: __ApiPreviewProps;
  123. customServer?: boolean;
  124. ampOptimizerConfig?: {
  125. [key: string]: any;
  126. };
  127. basePath: string;
  128. optimizeFonts: FontConfig;
  129. images: ImageConfigComplete;
  130. fontManifest?: FontManifest;
  131. disableOptimizedLoading?: boolean;
  132. optimizeCss: any;
  133. nextScriptWorkers: any;
  134. locale?: string;
  135. locales?: string[];
  136. defaultLocale?: string;
  137. domainLocales?: DomainLocale[];
  138. distDir: string;
  139. runtime?: ServerRuntime;
  140. serverComponents?: boolean;
  141. crossOrigin?: string;
  142. supportsDynamicHTML?: boolean;
  143. serverComponentManifest?: any;
  144. serverCSSManifest?: any;
  145. renderServerComponentData?: boolean;
  146. serverComponentProps?: any;
  147. largePageDataBytes?: number;
  148. };
  149. protected serverOptions: ServerOptions;
  150. private responseCache;
  151. protected router: Router;
  152. protected dynamicRoutes?: DynamicRoutes;
  153. protected appPathRoutes?: Record<string, string[]>;
  154. protected customRoutes: CustomRoutes;
  155. protected serverComponentManifest?: any;
  156. protected serverCSSManifest?: any;
  157. readonly hostname?: string;
  158. readonly port?: number;
  159. protected abstract getPublicDir(): string;
  160. protected abstract getHasStaticDir(): boolean;
  161. protected abstract getPagesManifest(): PagesManifest | undefined;
  162. protected abstract getAppPathsManifest(): PagesManifest | undefined;
  163. protected abstract getBuildId(): string;
  164. protected abstract getFilesystemPaths(): Set<string>;
  165. protected abstract findPageComponents(params: {
  166. pathname: string;
  167. query: NextParsedUrlQuery;
  168. params: Params;
  169. isAppPath: boolean;
  170. appPaths?: string[] | null;
  171. sriEnabled?: boolean;
  172. }): Promise<FindComponentsResult | null>;
  173. protected abstract getFontManifest(): FontManifest | undefined;
  174. protected abstract getPrerenderManifest(): PrerenderManifest;
  175. protected abstract getServerComponentManifest(): any;
  176. protected abstract getServerCSSManifest(): any;
  177. protected abstract attachRequestMeta(req: BaseNextRequest, parsedUrl: NextUrlWithParsedQuery): void;
  178. protected abstract getFallback(page: string): Promise<string>;
  179. protected abstract getCustomRoutes(): CustomRoutes;
  180. protected abstract hasPage(pathname: string): Promise<boolean>;
  181. protected abstract generateRoutes(): {
  182. headers: Route[];
  183. rewrites: {
  184. beforeFiles: Route[];
  185. afterFiles: Route[];
  186. fallback: Route[];
  187. };
  188. fsRoutes: Route[];
  189. redirects: Route[];
  190. catchAllRoute: Route;
  191. catchAllMiddleware: Route[];
  192. pageChecker: PageChecker;
  193. useFileSystemPublicRoutes: boolean;
  194. dynamicRoutes: DynamicRoutes | undefined;
  195. nextConfig: NextConfig;
  196. };
  197. protected abstract sendRenderResult(req: BaseNextRequest, res: BaseNextResponse, options: {
  198. result: RenderResult;
  199. type: 'html' | 'json';
  200. generateEtags: boolean;
  201. poweredByHeader: boolean;
  202. options?: PayloadOptions;
  203. }): Promise<void>;
  204. protected abstract runApi(req: BaseNextRequest, res: BaseNextResponse, query: ParsedUrlQuery, params: Params | undefined, page: string, builtPagePath: string): Promise<boolean>;
  205. protected abstract renderHTML(req: BaseNextRequest, res: BaseNextResponse, pathname: string, query: NextParsedUrlQuery, renderOpts: RenderOpts): Promise<RenderResult | null>;
  206. protected abstract handleCompression(req: BaseNextRequest, res: BaseNextResponse): void;
  207. protected abstract getResponseCache(options: {
  208. dev: boolean;
  209. }): ResponseCacheBase;
  210. protected abstract loadEnvConfig(params: {
  211. dev: boolean;
  212. forceReload?: boolean;
  213. }): void;
  214. constructor(options: ServerOptions);
  215. logError(err: Error): void;
  216. private handleRequest;
  217. getRequestHandler(): BaseRequestHandler;
  218. protected handleUpgrade(_req: BaseNextRequest, _socket: any, _head?: any): Promise<void>;
  219. setAssetPrefix(prefix?: string): void;
  220. prepare(): Promise<void>;
  221. protected close(): Promise<void>;
  222. protected getPreviewProps(): __ApiPreviewProps;
  223. protected _beforeCatchAllRender(_req: BaseNextRequest, _res: BaseNextResponse, _params: Params, _parsedUrl: UrlWithParsedQuery): Promise<boolean>;
  224. protected getDynamicRoutes(): Array<RoutingItem>;
  225. protected getAppPathRoutes(): Record<string, string[]>;
  226. protected run(req: BaseNextRequest, res: BaseNextResponse, parsedUrl: UrlWithParsedQuery): Promise<void>;
  227. private pipe;
  228. private getStaticHTML;
  229. render(req: BaseNextRequest, res: BaseNextResponse, pathname: string, query?: NextParsedUrlQuery, parsedUrl?: NextUrlWithParsedQuery, internalRender?: boolean): Promise<void>;
  230. protected getStaticPaths({ pathname, }: {
  231. pathname: string;
  232. originalAppPath?: string;
  233. }): Promise<{
  234. staticPaths?: string[];
  235. fallbackMode?: 'static' | 'blocking' | false;
  236. }>;
  237. private renderToResponseWithComponents;
  238. private stripNextDataPath;
  239. protected getOriginalAppPaths(route: string): string[] | null;
  240. protected renderPageComponent(ctx: RequestContext, bubbleNoFallback: boolean): Promise<false | ResponsePayload | null>;
  241. private renderToResponse;
  242. renderToHTML(req: BaseNextRequest, res: BaseNextResponse, pathname: string, query?: ParsedUrlQuery): Promise<string | null>;
  243. renderError(err: Error | null, req: BaseNextRequest, res: BaseNextResponse, pathname: string, query?: NextParsedUrlQuery, setHeaders?: boolean): Promise<void>;
  244. private customErrorNo404Warn;
  245. private renderErrorToResponse;
  246. renderErrorToHTML(err: Error | null, req: BaseNextRequest, res: BaseNextResponse, pathname: string, query?: ParsedUrlQuery): Promise<string | null>;
  247. protected getFallbackErrorComponents(): Promise<LoadComponentsReturnType | null>;
  248. render404(req: BaseNextRequest, res: BaseNextResponse, parsedUrl?: NextUrlWithParsedQuery, setHeaders?: boolean): Promise<void>;
  249. }
  250. export {};