Fix error handling in resolveClientReference (#31332)

When a React Server Consumer Manifest does not include an entry for a
client reference ID, we must not try to look up the export name (or
`'*'`) for the client reference. Otherwise this will fail with
`TypeError: Cannot read properties of undefined (reading '...')` instead
of the custom error we intended to throw.
This commit is contained in:
Hendrik Liebau 2024-10-23 17:36:40 +02:00 committed by GitHub
parent b4cbdc5a7c
commit 2dc5bebd46
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 12 additions and 12 deletions

View File

@ -62,19 +62,19 @@ export function resolveClientReference<T>(
metadata: ClientReferenceMetadata,
): ClientReference<T> {
const moduleExports = bundlerConfig[metadata[ID]];
let resolvedModuleData = moduleExports[metadata[NAME]];
let resolvedModuleData = moduleExports && moduleExports[metadata[NAME]];
let name;
if (resolvedModuleData) {
// The potentially aliased name.
name = resolvedModuleData.name;
} else {
// If we don't have this specific name, we might have the full module.
resolvedModuleData = moduleExports['*'];
resolvedModuleData = moduleExports && moduleExports['*'];
if (!resolvedModuleData) {
throw new Error(
'Could not find the module "' +
metadata[ID] +
'" in the React SSR Manifest. ' +
'" in the React Server Consumer Manifest. ' +
'This is probably a bug in the React Server Components bundler.',
);
}

View File

@ -68,19 +68,19 @@ export function resolveClientReference<T>(
): ClientReference<T> {
if (bundlerConfig) {
const moduleExports = bundlerConfig[metadata[ID]];
let resolvedModuleData = moduleExports[metadata[NAME]];
let resolvedModuleData = moduleExports && moduleExports[metadata[NAME]];
let name;
if (resolvedModuleData) {
// The potentially aliased name.
name = resolvedModuleData.name;
} else {
// If we don't have this specific name, we might have the full module.
resolvedModuleData = moduleExports['*'];
resolvedModuleData = moduleExports && moduleExports['*'];
if (!resolvedModuleData) {
throw new Error(
'Could not find the module "' +
metadata[ID] +
'" in the React SSR Manifest. ' +
'" in the React Server Consumer Manifest. ' +
'This is probably a bug in the React Server Components bundler.',
);
}

View File

@ -62,19 +62,19 @@ export function resolveClientReference<T>(
metadata: ClientReferenceMetadata,
): ClientReference<T> {
const moduleExports = bundlerConfig[metadata[ID]];
let resolvedModuleData = moduleExports[metadata[NAME]];
let resolvedModuleData = moduleExports && moduleExports[metadata[NAME]];
let name;
if (resolvedModuleData) {
// The potentially aliased name.
name = resolvedModuleData.name;
} else {
// If we don't have this specific name, we might have the full module.
resolvedModuleData = moduleExports['*'];
resolvedModuleData = moduleExports && moduleExports['*'];
if (!resolvedModuleData) {
throw new Error(
'Could not find the module "' +
metadata[ID] +
'" in the React SSR Manifest. ' +
'" in the React Server Consumer Manifest. ' +
'This is probably a bug in the React Server Components bundler.',
);
}

View File

@ -68,19 +68,19 @@ export function resolveClientReference<T>(
): ClientReference<T> {
if (bundlerConfig) {
const moduleExports = bundlerConfig[metadata[ID]];
let resolvedModuleData = moduleExports[metadata[NAME]];
let resolvedModuleData = moduleExports && moduleExports[metadata[NAME]];
let name;
if (resolvedModuleData) {
// The potentially aliased name.
name = resolvedModuleData.name;
} else {
// If we don't have this specific name, we might have the full module.
resolvedModuleData = moduleExports['*'];
resolvedModuleData = moduleExports && moduleExports['*'];
if (!resolvedModuleData) {
throw new Error(
'Could not find the module "' +
metadata[ID] +
'" in the React SSR Manifest. ' +
'" in the React Server Consumer Manifest. ' +
'This is probably a bug in the React Server Components bundler.',
);
}