Skip to content

Commit

Permalink
refactor hash to digest
Browse files Browse the repository at this point in the history
  • Loading branch information
gnoff committed Jun 2, 2022
1 parent e4c8a38 commit 553420e
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 76 deletions.
8 changes: 4 additions & 4 deletions packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js
Expand Up @@ -92,11 +92,11 @@ describe('ReactDOMFizzServer', () => {
function expectErrors(errorsArr, toBeDevArr, toBeProdArr) {
const mappedErrows = errorsArr.map(({error, errorInfo}) => {
const stack = errorInfo && errorInfo.componentStack;
const errorHash = errorInfo && errorInfo.errorHash;
const digest = errorInfo && errorInfo.digest;
if (stack) {
return [error.message, errorHash, normalizeCodeLocInfo(stack)];
} else if (errorHash) {
return [error.message, errorHash];
return [error.message, digest, normalizeCodeLocInfo(stack)];
} else if (digest) {
return [error.message, digest];
}
return error.message;
});
Expand Down
43 changes: 32 additions & 11 deletions packages/react-dom/src/client/ReactDOMHostConfig.js
Expand Up @@ -729,22 +729,43 @@ export function isSuspenseInstancePending(instance: SuspenseInstance) {
export function isSuspenseInstanceFallback(instance: SuspenseInstance) {
return instance.data === SUSPENSE_FALLBACK_START_DATA;
}

export function getSuspenseInstanceFallbackErrorDetails(
instance: SuspenseInstance,
): {message?: string, stack?: string, hash?: string} {
const nextSibling = instance.nextSibling;
if (
nextSibling &&
nextSibling.nodeType === ELEMENT_NODE &&
nextSibling.nodeName.toLowerCase() === 'template'
) {
): {digest: ?string, message?: string, stack?: string} {
const dataset =
instance.nextSibling && ((instance.nextSibling: any): HTMLElement).dataset;
let digest, message, stack;
if (dataset) {
digest = dataset.dgst;
if (__DEV__) {
message = dataset.msg;
stack = dataset.stck;
}
}
if (__DEV__) {
return {
message: ((nextSibling: any): HTMLTemplateElement).dataset.msg,
stack: ((nextSibling: any): HTMLTemplateElement).dataset.stack,
hash: ((nextSibling: any): HTMLTemplateElement).dataset.hash,
message,
digest,
stack,
};
} else {
return {
digest,
};
}
return {};

// let value = {message: undefined, hash: undefined};
// const nextSibling = instance.nextSibling;
// if (nextSibling) {
// const dataset = ((nextSibling: any): HTMLTemplateElement).dataset;
// value.message = dataset.msg;
// value.hash = dataset.hash;
// if (__DEV__) {
// value.stack = dataset.stack;
// }
// }
// return value;
}

export function registerSuspenseInstanceRetry(
Expand Down
54 changes: 26 additions & 28 deletions packages/react-dom/src/server/ReactDOMServerFormatConfig.js
Expand Up @@ -1527,13 +1527,13 @@ const startClientRenderedSuspenseBoundary = stringToPrecomputedChunk(
const endSuspenseBoundary = stringToPrecomputedChunk('<!--/$-->');

const clientRenderedSuspenseBoundaryError1 = stringToPrecomputedChunk(
'<template data-hash="',
'<template data-dgst="',
);
const clientRenderedSuspenseBoundaryError1A = stringToPrecomputedChunk(
'" data-msg="',
);
const clientRenderedSuspenseBoundaryError1B = stringToPrecomputedChunk(
'" data-stack="',
'" data-stck="',
);
const clientRenderedSuspenseBoundaryError2 = stringToPrecomputedChunk(
'"></template>',
Expand Down Expand Up @@ -1576,7 +1576,7 @@ export function writeStartPendingSuspenseBoundary(
export function writeStartClientRenderedSuspenseBoundary(
destination: Destination,
responseState: ResponseState,
errorHash: ?string,
errorDigest: ?string,
errorMesssage: ?string,
errorComponentStack: ?string,
): boolean {
Expand All @@ -1585,33 +1585,31 @@ export function writeStartClientRenderedSuspenseBoundary(
destination,
startClientRenderedSuspenseBoundary,
);
if (errorHash) {
writeChunk(destination, clientRenderedSuspenseBoundaryError1);
writeChunk(destination, stringToChunk(escapeTextForBrowser(errorHash)));
// In prod errorMessage will usually be nullish but there is one case where
// it is used (currently when the server aborts the task) so we leave it ungated.
writeChunk(destination, clientRenderedSuspenseBoundaryError1);
writeChunk(
destination,
stringToChunk(escapeTextForBrowser(errorDigest || '')),
);
if (__DEV__) {
if (errorMesssage) {
writeChunk(destination, clientRenderedSuspenseBoundaryError1A);
writeChunk(
destination,
stringToChunk(escapeTextForBrowser(errorMesssage)),
);
}
if (__DEV__) {
// Component stacks are currently only captured in dev
if (errorComponentStack) {
writeChunk(destination, clientRenderedSuspenseBoundaryError1B);
writeChunk(
destination,
stringToChunk(escapeTextForBrowser(errorComponentStack)),
);
}
if (errorComponentStack) {
writeChunk(destination, clientRenderedSuspenseBoundaryError1B);
writeChunk(
destination,
stringToChunk(escapeTextForBrowser(errorComponentStack)),
);
}
result = writeChunkAndReturn(
destination,
clientRenderedSuspenseBoundaryError2,
);
}
result = writeChunkAndReturn(
destination,
clientRenderedSuspenseBoundaryError2,
);
return result;
}
export function writeEndCompletedSuspenseBoundary(
Expand Down Expand Up @@ -1772,7 +1770,7 @@ export function writeEndSegment(
// const SUSPENSE_PENDING_START_DATA = '$?';
// const SUSPENSE_FALLBACK_START_DATA = '$!';
//
// function clientRenderBoundary(suspenseBoundaryID, errorHash, errorMsg, errorComponentStack) {
// function clientRenderBoundary(suspenseBoundaryID, errorDigest, errorMsg, errorComponentStack) {
// // Find the fallback's first element.
// const suspenseIdNode = document.getElementById(suspenseBoundaryID);
// if (!suspenseIdNode) {
Expand All @@ -1786,9 +1784,9 @@ export function writeEndSegment(
// suspenseNode.data = SUSPENSE_FALLBACK_START_DATA;
// // assign error metadata to first sibling
// let dataset = suspenseIdNode.dataset;
// if (errorHash) dataset.hash = errorHash;
// if (errorDigest) dataset.dgst = errorDigest;
// if (errorMsg) dataset.msg = errorMsg;
// if (errorComponentStack) dataset.stack = errorComponentStack;
// if (errorComponentStack) dataset.stck = errorComponentStack;
// // Tell React to retry it if the parent already hydrated.
// if (suspenseNode._reactRetry) {
// suspenseNode._reactRetry();
Expand Down Expand Up @@ -1876,7 +1874,7 @@ const completeSegmentFunction =
const completeBoundaryFunction =
'function $RC(a,b){a=document.getElementById(a);b=document.getElementById(b);b.parentNode.removeChild(b);if(a){a=a.previousSibling;var f=a.parentNode,c=a.nextSibling,e=0;do{if(c&&8===c.nodeType){var d=c.data;if("/$"===d)if(0===e)break;else e--;else"$"!==d&&"$?"!==d&&"$!"!==d||e++}d=c.nextSibling;f.removeChild(c);c=d}while(c);for(;b.firstChild;)f.insertBefore(b.firstChild,c);a.data="$";a._reactRetry&&a._reactRetry()}}';
const clientRenderFunction =
'function $RX(b,c,d,e){var a=document.getElementById(b);a&&(b=a.previousSibling,b.data="$!",a=a.dataset,c&&(a.hash=c),d&&(a.msg=d),e&&(a.stack=e),b._reactRetry&&b._reactRetry())}';
'function $RX(b,c,d,e){var a=document.getElementById(b);a&&(b=a.previousSibling,b.data="$!",a=a.dataset,c&&(a.dgst=c),d&&(a.msg=d),e&&(a.stck=e),b._reactRetry&&b._reactRetry())}';

const completeSegmentScript1Full = stringToPrecomputedChunk(
completeSegmentFunction + ';$RS("',
Expand Down Expand Up @@ -1957,7 +1955,7 @@ export function writeClientRenderBoundaryInstruction(
destination: Destination,
responseState: ResponseState,
boundaryID: SuspenseBoundaryID,
errorHash: ?string,
errorDigest: ?string,
errorMessage?: string,
errorComponentStack?: string,
): boolean {
Expand All @@ -1979,11 +1977,11 @@ export function writeClientRenderBoundaryInstruction(

writeChunk(destination, boundaryID);
writeChunk(destination, clientRenderScript1A);
if (errorHash || errorMessage || errorComponentStack) {
if (errorDigest || errorMessage || errorComponentStack) {
writeChunk(destination, clientRenderErrorScriptArgInterstitial);
writeChunk(
destination,
stringToChunk(escapeJSStringsForInstructionScripts(errorHash || '')),
stringToChunk(escapeJSStringsForInstructionScripts(errorDigest || '')),
);
}
if (errorMessage || errorComponentStack) {
Expand Down
Expand Up @@ -149,7 +149,7 @@ export function writeStartClientRenderedSuspenseBoundary(
destination: Destination,
responseState: ResponseState,
// flushing these error arguments are not currently supported in this legacy streaming format.
errorHash: ?string,
errorDigest: ?string,
errorMessage?: string,
errorComponentStack?: string,
): boolean {
Expand Down
Expand Up @@ -226,7 +226,7 @@ export function writeStartClientRenderedSuspenseBoundary(
destination: Destination,
responseState: ResponseState,
// TODO: encode error for native
errorHash: ?string,
errorDigest: ?string,
errorMessage: ?string,
errorComponentStack: ?string,
): boolean {
Expand Down Expand Up @@ -300,7 +300,7 @@ export function writeClientRenderBoundaryInstruction(
responseState: ResponseState,
boundaryID: SuspenseBoundaryID,
// TODO: encode error for native
errorHash: ?string,
errorDigest: ?string,
errorMessage: ?string,
errorComponentStack: ?string,
): boolean {
Expand Down
10 changes: 5 additions & 5 deletions packages/react-reconciler/src/ReactCapturedValue.js
Expand Up @@ -15,7 +15,7 @@ export type CapturedValue<T> = {|
value: T,
source: Fiber | null,
stack: string | null,
hash: string | null,
digest: string | null,
|};

export function createCapturedValueAtFiber<T>(
Expand All @@ -28,19 +28,19 @@ export function createCapturedValueAtFiber<T>(
value,
source,
stack: getStackByFiberInDevAndProd(source),
hash: null,
digest: null,
};
}

export function createCapturedValue<T>(
value: T,
hash?: string,
stack?: string,
digest: ?string,
stack: ?string,
): CapturedValue<T> {
return {
value,
source: null,
stack: stack != null ? stack : null,
hash: hash != null ? hash : null,
digest: digest != null ? digest : null,
};
}
14 changes: 10 additions & 4 deletions packages/react-reconciler/src/ReactFiberBeginWork.new.js
Expand Up @@ -2583,9 +2583,15 @@ function updateDehydratedSuspenseComponent(
// This boundary is in a permanent fallback state. In this case, we'll never
// get an update and we'll never be able to hydrate the final content. Let's just try the
// client side render instead.
const {message, hash, stack} = getSuspenseInstanceFallbackErrorDetails(
suspenseInstance,
);
let digest, message, stack;
if (__DEV__) {
({digest, message, stack} = getSuspenseInstanceFallbackErrorDetails(
suspenseInstance,
));
} else {
({digest} = getSuspenseInstanceFallbackErrorDetails(suspenseInstance));
}

const error = message
? // eslint-disable-next-line react-internal/prod-error-codes
new Error(message)
Expand All @@ -2594,7 +2600,7 @@ function updateDehydratedSuspenseComponent(
'due to an error during server rendering. Switched to ' +
'client rendering.',
);
const capturedValue = createCapturedValue(error, hash, stack);
const capturedValue = createCapturedValue(error, digest, stack);
return retrySuspenseComponentWithoutHydrating(
current,
workInProgress,
Expand Down
14 changes: 10 additions & 4 deletions packages/react-reconciler/src/ReactFiberBeginWork.old.js
Expand Up @@ -2583,9 +2583,15 @@ function updateDehydratedSuspenseComponent(
// This boundary is in a permanent fallback state. In this case, we'll never
// get an update and we'll never be able to hydrate the final content. Let's just try the
// client side render instead.
const {message, hash, stack} = getSuspenseInstanceFallbackErrorDetails(
suspenseInstance,
);
let digest, message, stack;
if (__DEV__) {
({digest, message, stack} = getSuspenseInstanceFallbackErrorDetails(
suspenseInstance,
));
} else {
({digest} = getSuspenseInstanceFallbackErrorDetails(suspenseInstance));
}

const error = message
? // eslint-disable-next-line react-internal/prod-error-codes
new Error(message)
Expand All @@ -2594,7 +2600,7 @@ function updateDehydratedSuspenseComponent(
'due to an error during server rendering. Switched to ' +
'client rendering.',
);
const capturedValue = createCapturedValue(error, hash, stack);
const capturedValue = createCapturedValue(error, digest, stack);
return retrySuspenseComponentWithoutHydrating(
current,
workInProgress,
Expand Down
4 changes: 2 additions & 2 deletions packages/react-reconciler/src/ReactFiberWorkLoop.new.js
Expand Up @@ -2343,8 +2343,8 @@ function commitRootImpl(
for (let i = 0; i < recoverableErrors.length; i++) {
const recoverableError = recoverableErrors[i];
const componentStack = recoverableError.stack;
const errorHash = recoverableError.hash;
onRecoverableError(recoverableError.value, {componentStack, errorHash});
const digest = recoverableError.digest;
onRecoverableError(recoverableError.value, {componentStack, digest});
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/react-reconciler/src/ReactFiberWorkLoop.old.js
Expand Up @@ -2343,8 +2343,8 @@ function commitRootImpl(
for (let i = 0; i < recoverableErrors.length; i++) {
const recoverableError = recoverableErrors[i];
const componentStack = recoverableError.stack;
const errorHash = recoverableError.hash;
onRecoverableError(recoverableError.value, {componentStack, errorHash});
const digest = recoverableError.digest;
onRecoverableError(recoverableError.value, {componentStack, digest});
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/react-reconciler/src/ReactInternalTypes.js
Expand Up @@ -249,7 +249,7 @@ type BaseFiberRootProperties = {|

onRecoverableError: (
error: mixed,
errorInfo: {errorHash?: ?string, componentStack?: ?string},
errorInfo: {errorDigest?: ?string, componentStack?: ?string},
) => void,
|};

Expand Down

0 comments on commit 553420e

Please sign in to comment.