router.d.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /// <reference types="node" />
  2. /// <reference types="node" />
  3. import type { ComponentType } from 'react';
  4. import type { DomainLocale } from '../../../server/config';
  5. import type { MittEmitter } from '../mitt';
  6. import type { ParsedUrlQuery } from 'querystring';
  7. import type { RouterEvent } from '../../../client/router';
  8. import type { StyleSheetTuple } from '../../../client/page-loader';
  9. import type { UrlObject } from 'url';
  10. import type PageLoader from '../../../client/page-loader';
  11. import { NextPageContext, NEXT_DATA } from '../utils';
  12. declare global {
  13. interface Window {
  14. __NEXT_DATA__: NEXT_DATA;
  15. }
  16. }
  17. interface RouteProperties {
  18. shallow: boolean;
  19. }
  20. interface TransitionOptions {
  21. shallow?: boolean;
  22. locale?: string | false;
  23. scroll?: boolean;
  24. unstable_skipClientCache?: boolean;
  25. }
  26. interface NextHistoryState {
  27. url: string;
  28. as: string;
  29. options: TransitionOptions;
  30. }
  31. export declare type HistoryState = null | {
  32. __NA: true;
  33. __N?: false;
  34. } | {
  35. __N: false;
  36. __NA?: false;
  37. } | ({
  38. __NA?: false;
  39. __N: true;
  40. key: string;
  41. } & NextHistoryState);
  42. interface MiddlewareEffectParams<T extends FetchDataOutput> {
  43. fetchData?: () => Promise<T>;
  44. locale?: string;
  45. asPath: string;
  46. router: Router;
  47. }
  48. export declare function matchesMiddleware<T extends FetchDataOutput>(options: MiddlewareEffectParams<T>): Promise<boolean>;
  49. /**
  50. * Detects whether a given url is routable by the Next.js router (browser only).
  51. */
  52. export declare function isLocalURL(url: string): boolean;
  53. export declare function interpolateAs(route: string, asPathname: string, query: ParsedUrlQuery): {
  54. params: string[];
  55. result: string;
  56. };
  57. /**
  58. * Resolves a given hyperlink with a certain router state (basePath not included).
  59. * Preserves absolute urls.
  60. */
  61. export declare function resolveHref(router: NextRouter, href: Url, resolveAs?: boolean): string;
  62. declare type Url = UrlObject | string;
  63. export declare type BaseRouter = {
  64. route: string;
  65. pathname: string;
  66. query: ParsedUrlQuery;
  67. asPath: string;
  68. basePath: string;
  69. locale?: string | undefined;
  70. locales?: string[] | undefined;
  71. defaultLocale?: string | undefined;
  72. domainLocales?: DomainLocale[] | undefined;
  73. isLocaleDomain: boolean;
  74. };
  75. export declare type NextRouter = BaseRouter & Pick<Router, 'push' | 'replace' | 'reload' | 'back' | 'prefetch' | 'beforePopState' | 'events' | 'isFallback' | 'isReady' | 'isPreview'>;
  76. export declare type PrefetchOptions = {
  77. priority?: boolean;
  78. locale?: string | false;
  79. unstable_skipClientCache?: boolean;
  80. };
  81. export declare type PrivateRouteInfo = (Omit<CompletePrivateRouteInfo, 'styleSheets'> & {
  82. initial: true;
  83. }) | CompletePrivateRouteInfo;
  84. export declare type CompletePrivateRouteInfo = {
  85. Component: ComponentType;
  86. styleSheets: StyleSheetTuple[];
  87. __N_SSG?: boolean;
  88. __N_SSP?: boolean;
  89. props?: Record<string, any>;
  90. err?: Error;
  91. error?: any;
  92. route?: string;
  93. resolvedAs?: string;
  94. query?: ParsedUrlQuery;
  95. };
  96. export declare type AppProps = Pick<CompletePrivateRouteInfo, 'Component' | 'err'> & {
  97. router: Router;
  98. } & Record<string, any>;
  99. export declare type AppComponent = ComponentType<AppProps>;
  100. declare type Subscription = (data: PrivateRouteInfo, App: AppComponent, resetScroll: {
  101. x: number;
  102. y: number;
  103. } | null) => Promise<void>;
  104. declare type BeforePopStateCallback = (state: NextHistoryState) => boolean;
  105. declare type ComponentLoadCancel = (() => void) | null;
  106. declare type HistoryMethod = 'replaceState' | 'pushState';
  107. interface FetchDataOutput {
  108. dataHref: string;
  109. json: Record<string, any> | null;
  110. response: Response;
  111. text: string;
  112. cacheKey: string;
  113. }
  114. interface NextDataCache {
  115. [asPath: string]: Promise<FetchDataOutput>;
  116. }
  117. export declare function createKey(): string;
  118. export default class Router implements BaseRouter {
  119. basePath: string;
  120. /**
  121. * Map of all components loaded in `Router`
  122. */
  123. components: {
  124. [pathname: string]: PrivateRouteInfo;
  125. };
  126. sdc: NextDataCache;
  127. sub: Subscription;
  128. clc: ComponentLoadCancel;
  129. pageLoader: PageLoader;
  130. _bps: BeforePopStateCallback | undefined;
  131. events: MittEmitter<RouterEvent>;
  132. _wrapApp: (App: AppComponent) => any;
  133. isSsr: boolean;
  134. _inFlightRoute?: string | undefined;
  135. _shallow?: boolean | undefined;
  136. locales?: string[] | undefined;
  137. defaultLocale?: string | undefined;
  138. domainLocales?: DomainLocale[] | undefined;
  139. isReady: boolean;
  140. isLocaleDomain: boolean;
  141. isFirstPopStateEvent: boolean;
  142. _initialMatchesMiddlewarePromise: Promise<boolean>;
  143. private state;
  144. private _key;
  145. static events: MittEmitter<RouterEvent>;
  146. constructor(pathname: string, query: ParsedUrlQuery, as: string, { initialProps, pageLoader, App, wrapApp, Component, err, subscription, isFallback, locale, locales, defaultLocale, domainLocales, isPreview, }: {
  147. subscription: Subscription;
  148. initialProps: any;
  149. pageLoader: any;
  150. Component: ComponentType;
  151. App: AppComponent;
  152. wrapApp: (WrapAppComponent: AppComponent) => any;
  153. err?: Error;
  154. isFallback: boolean;
  155. locale?: string;
  156. locales?: string[];
  157. defaultLocale?: string;
  158. domainLocales?: DomainLocale[];
  159. isPreview?: boolean;
  160. });
  161. onPopState: (e: PopStateEvent) => void;
  162. reload(): void;
  163. /**
  164. * Go back in history
  165. */
  166. back(): void;
  167. /**
  168. * Performs a `pushState` with arguments
  169. * @param url of the route
  170. * @param as masks `url` for the browser
  171. * @param options object you can define `shallow` and other options
  172. */
  173. push(url: Url, as?: Url, options?: TransitionOptions): Promise<boolean>;
  174. /**
  175. * Performs a `replaceState` with arguments
  176. * @param url of the route
  177. * @param as masks `url` for the browser
  178. * @param options object you can define `shallow` and other options
  179. */
  180. replace(url: Url, as?: Url, options?: TransitionOptions): Promise<boolean>;
  181. private change;
  182. changeState(method: HistoryMethod, url: string, as: string, options?: TransitionOptions): void;
  183. handleRouteInfoError(err: Error & {
  184. code?: any;
  185. cancelled?: boolean;
  186. }, pathname: string, query: ParsedUrlQuery, as: string, routeProps: RouteProperties, loadErrorFail?: boolean): Promise<CompletePrivateRouteInfo>;
  187. getRouteInfo({ route: requestedRoute, pathname, query, as, resolvedAs, routeProps, locale, hasMiddleware, isPreview, unstable_skipClientCache, }: {
  188. route: string;
  189. pathname: string;
  190. query: ParsedUrlQuery;
  191. as: string;
  192. resolvedAs: string;
  193. hasMiddleware?: boolean;
  194. routeProps: RouteProperties;
  195. locale: string | undefined;
  196. isPreview: boolean;
  197. unstable_skipClientCache?: boolean;
  198. }): Promise<PrivateRouteInfo | {
  199. type: "redirect-external";
  200. destination: string;
  201. } | {
  202. type: "redirect-internal";
  203. newAs: string;
  204. newUrl: string;
  205. }>;
  206. private set;
  207. /**
  208. * Callback to execute before replacing router state
  209. * @param cb callback to be executed
  210. */
  211. beforePopState(cb: BeforePopStateCallback): void;
  212. onlyAHashChange(as: string): boolean;
  213. scrollToHash(as: string): void;
  214. urlIsNew(asPath: string): boolean;
  215. /**
  216. * Prefetch page code, you may wait for the data during page rendering.
  217. * This feature only works in production!
  218. * @param url the href of prefetched page
  219. * @param asPath the as path of the prefetched page
  220. */
  221. prefetch(url: string, asPath?: string, options?: PrefetchOptions): Promise<void>;
  222. fetchComponent(route: string): Promise<import("../../../client/page-loader").GoodPageCache>;
  223. _getData<T>(fn: () => Promise<T>): Promise<T>;
  224. _getFlightData(dataHref: string): Promise<{
  225. data: string;
  226. }>;
  227. getInitialProps(Component: ComponentType, ctx: NextPageContext): Promise<any>;
  228. get route(): string;
  229. get pathname(): string;
  230. get query(): ParsedUrlQuery;
  231. get asPath(): string;
  232. get locale(): string | undefined;
  233. get isFallback(): boolean;
  234. get isPreview(): boolean;
  235. }
  236. export {};