Skip to content

Commit

Permalink
Fix error handling in resolveClientReference (#31332)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
unstubbable authored Oct 23, 2024
1 parent b4cbdc5 commit 2dc5beb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
);
}
Expand Down

0 comments on commit 2dc5beb

Please sign in to comment.