mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
On the server we have a similar translation map from the file path that the loader uses to the refer to the original module and to the bundled module ID. The Flight server is optimized to emit the smallest format for the client. However during SSR, the same client component might go by a different module ID since it's a different bundle than the client bundle. This provides an option to add a translation map from client ID to SSR ID when reading the Flight stream. Ideally we should have a special SSR Flight Client that takes this option but for now we only have one Client for both.
82 lines
2.1 KiB
JavaScript
82 lines
2.1 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow
|
|
*/
|
|
|
|
import type {JSONValue, ResponseBase} from 'react-client/src/ReactFlightClient';
|
|
|
|
import type {JSResourceReference} from 'JSResourceReference';
|
|
|
|
import type {ModuleMetaData} from 'ReactFlightNativeRelayClientIntegration';
|
|
|
|
export type ModuleReference<T> = JSResourceReference<T>;
|
|
|
|
import {
|
|
parseModelString,
|
|
parseModelTuple,
|
|
} from 'react-client/src/ReactFlightClient';
|
|
|
|
export {
|
|
preloadModule,
|
|
requireModule,
|
|
} from 'ReactFlightNativeRelayClientIntegration';
|
|
|
|
import {resolveModuleReference as resolveModuleReferenceImpl} from 'ReactFlightNativeRelayClientIntegration';
|
|
|
|
import isArray from 'shared/isArray';
|
|
|
|
export type {ModuleMetaData} from 'ReactFlightNativeRelayClientIntegration';
|
|
|
|
export type BundlerConfig = null;
|
|
|
|
export type UninitializedModel = JSONValue;
|
|
|
|
export type Response = ResponseBase;
|
|
|
|
export function resolveModuleReference<T>(
|
|
bundlerConfig: BundlerConfig,
|
|
moduleData: ModuleMetaData,
|
|
): ModuleReference<T> {
|
|
return resolveModuleReferenceImpl(moduleData);
|
|
}
|
|
|
|
function parseModelRecursively(response: Response, parentObj, value) {
|
|
if (typeof value === 'string') {
|
|
return parseModelString(response, parentObj, value);
|
|
}
|
|
if (typeof value === 'object' && value !== null) {
|
|
if (isArray(value)) {
|
|
const parsedValue = [];
|
|
for (let i = 0; i < value.length; i++) {
|
|
(parsedValue: any)[i] = parseModelRecursively(
|
|
response,
|
|
value,
|
|
value[i],
|
|
);
|
|
}
|
|
return parseModelTuple(response, parsedValue);
|
|
} else {
|
|
const parsedValue = {};
|
|
for (const innerKey in value) {
|
|
(parsedValue: any)[innerKey] = parseModelRecursively(
|
|
response,
|
|
value,
|
|
value[innerKey],
|
|
);
|
|
}
|
|
return parsedValue;
|
|
}
|
|
}
|
|
return value;
|
|
}
|
|
|
|
const dummy = {};
|
|
|
|
export function parseModel<T>(response: Response, json: UninitializedModel): T {
|
|
return (parseModelRecursively(response, dummy, json): any);
|
|
}
|