100 lines
4.6 KiB
TypeScript
100 lines
4.6 KiB
TypeScript
import type { FlightRouterState, InitialRSCPayload, NavigationFlightResponse } from '../../../shared/lib/app-router-types';
|
|
import { type NEXT_ROUTER_PREFETCH_HEADER, type NEXT_ROUTER_SEGMENT_PREFETCH_HEADER, type NEXT_INSTANT_PREFETCH_HEADER, NEXT_ROUTER_STATE_TREE_HEADER, NEXT_URL, RSC_HEADER, NEXT_HMR_REFRESH_HEADER, NEXT_HTML_REQUEST_ID_HEADER, NEXT_REQUEST_ID_HEADER } from '../app-router-headers';
|
|
import { type NormalizedFlightData } from '../../flight-data-helpers';
|
|
import type { NormalizedSearch } from '../segment-cache/cache-key';
|
|
export interface FetchServerResponseOptions {
|
|
readonly flightRouterState: FlightRouterState;
|
|
readonly nextUrl: string | null;
|
|
readonly isHmrRefresh?: boolean;
|
|
}
|
|
export type StaticStageData<T extends NavigationFlightResponse | InitialRSCPayload = NavigationFlightResponse> = {
|
|
readonly response: T;
|
|
readonly isResponsePartial: boolean;
|
|
};
|
|
type SpaFetchServerResponseResult = {
|
|
flightData: NormalizedFlightData[];
|
|
canonicalUrl: URL;
|
|
renderedSearch: NormalizedSearch;
|
|
couldBeIntercepted: boolean;
|
|
supportsPerSegmentPrefetching: boolean;
|
|
postponed: boolean;
|
|
dynamicStaleTime: number;
|
|
staticStageData: StaticStageData | null;
|
|
runtimePrefetchStream: ReadableStream<Uint8Array> | null;
|
|
responseHeaders: Headers;
|
|
debugInfo: Array<any> | null;
|
|
};
|
|
type MpaFetchServerResponseResult = string;
|
|
export type FetchServerResponseResult = MpaFetchServerResponseResult | SpaFetchServerResponseResult;
|
|
export type RequestHeaders = {
|
|
[RSC_HEADER]?: '1';
|
|
[NEXT_ROUTER_STATE_TREE_HEADER]?: string;
|
|
[NEXT_URL]?: string;
|
|
[NEXT_ROUTER_PREFETCH_HEADER]?: '1' | '2';
|
|
[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER]?: string;
|
|
'x-deployment-id'?: string;
|
|
[NEXT_HMR_REFRESH_HEADER]?: '1';
|
|
'Next-Test-Fetch-Priority'?: RequestInit['priority'];
|
|
[NEXT_HTML_REQUEST_ID_HEADER]?: string;
|
|
[NEXT_REQUEST_ID_HEADER]?: string;
|
|
[NEXT_INSTANT_PREFETCH_HEADER]?: '1';
|
|
};
|
|
/**
|
|
* Fetch the flight data for the provided url. Takes in the current router state
|
|
* to decide what to render server-side.
|
|
*/
|
|
export declare function fetchServerResponse(url: URL, options: FetchServerResponseOptions): Promise<FetchServerResponseResult>;
|
|
export type RSCResponse<T> = {
|
|
ok: boolean;
|
|
redirected: boolean;
|
|
headers: Headers;
|
|
body: ReadableStream<Uint8Array> | null;
|
|
status: number;
|
|
url: string;
|
|
flightResponsePromise: (Promise<T> & {
|
|
_debugInfo?: Array<any>;
|
|
}) | null;
|
|
cacheData: Promise<FetchResponseCacheData | null>;
|
|
};
|
|
type FetchResponseCacheData = {
|
|
isResponsePartial: boolean;
|
|
responseBodyClone?: ReadableStream<Uint8Array>;
|
|
};
|
|
/**
|
|
* Strips the leading isPartial byte from an RSC navigation response and
|
|
* clones the body for segment cache extraction.
|
|
*
|
|
* When cache components is enabled, the server prepends a single byte:
|
|
* '~' (0x7e) for partial, '#' (0x23) for complete. This must be stripped
|
|
* before Flight decoding because it's not valid RSC data. The body is
|
|
* cloned before Flight can consume it so the clone is available for later use.
|
|
*
|
|
* When cache components is disabled, returns the original response with
|
|
* cacheData: null.
|
|
*/
|
|
export declare function processFetch(response: Response): Promise<{
|
|
response: Response;
|
|
cacheData: FetchResponseCacheData | null;
|
|
}>;
|
|
/**
|
|
* Resolves the static stage response from the raw `processFetch` outputs and
|
|
* the decoded flight response, for writing into the segment cache.
|
|
*
|
|
* - Fully static: use the decoded flight response as-is, no truncation needed.
|
|
* - Not fully static + `l` field: truncate the body clone at the static stage
|
|
* byte boundary and decode.
|
|
* - Otherwise: no cache-worthy data.
|
|
*/
|
|
export declare function resolveStaticStageData<T extends NavigationFlightResponse | InitialRSCPayload>(cacheData: FetchResponseCacheData, flightResponse: T, headers: RequestHeaders | undefined): Promise<StaticStageData<T> | null>;
|
|
/**
|
|
* Truncates a Flight stream clone at the given byte boundary and decodes the
|
|
* static stage prefix. Used by both the navigation path and the initial HTML
|
|
* hydration path.
|
|
*/
|
|
export declare function decodeStaticStage<T>(responseBodyClone: ReadableStream<Uint8Array>, staticStageByteLengthPromise: Promise<number>, headers: RequestHeaders | undefined): Promise<T>;
|
|
export declare function createFetch<T>(url: URL, headers: RequestHeaders, fetchPriority: 'auto' | 'high' | 'low' | null, shouldImmediatelyDecode: boolean, signal?: AbortSignal): Promise<RSCResponse<T>>;
|
|
export declare function createFromNextReadableStream<T>(flightStream: ReadableStream<Uint8Array>, requestHeaders: RequestHeaders | undefined, options?: {
|
|
allowPartialStream?: boolean;
|
|
}): Promise<T>;
|
|
export {};
|