GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Track Owner for Server Components in DEV by sebmarkbage · Pull Request #28753 · react/react

Track Owner for Server Components in DEV #28753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
sebmarkbage merged 7 commits into react:main from sebmarkbage:rscowner
Apr 5, 2024

Uh oh!

There was an error while loading. Please reload this page.

Merged

Track Owner for Server Components in DEV #28753

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit Hold shift + click to select a range
81ce4c0
Test
sebmarkbage Apr 4, 2024
6bfed88
Encode debug info with the console serialization instead of plain JSON
sebmarkbage Apr 4, 2024
a1570ca
Outline Server Component debug info so that we can refer to it by ref…
sebmarkbage Apr 4, 2024
d290097
Initialize current owner in DEV before rendering a server component
sebmarkbage Apr 4, 2024
b44d302
Include the owner of a Server Component which must also be another Se…
sebmarkbage Apr 4, 2024
d8e167a
Since debugOwner can now be a non-Fiber, fix up places that assumes i…
sebmarkbage Apr 4, 2024
79901a4
Track owner of console logs
sebmarkbage Apr 5, 2024
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
Next Next commit
Initialize current owner in DEV before rendering a server component
If we suspend, we stash it on prevThenableState as a hack.

Then we serialize the owner as the last slot in the Element tuple so we
can restore it on the client.
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@sebmarkbage
sebmarkbage committed Apr 5, 2024
commit d290097e877785466ffd8b712334d7570975540d

There are no files selected for viewing

12 changes: 9 additions & 3 deletions 12 packages/react-client/src/ReactFlightClient.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ function createElement(
type: mixed,
key: mixed,
props: mixed,
owner: null | ReactComponentInfo, // DEV-only
): React$Element<any> {
let element: any;
if (__DEV__ && enableRefAsProp) {
Expand All @@ -493,7 +494,7 @@ function createElement(
type,
key,
props,
_owner: null,
_owner: owner,
}: any);
Object.defineProperty(element, 'ref', {
enumerable: false,
Expand All @@ -520,7 +521,7 @@ function createElement(
props,

// Record the component responsible for creating this element.
_owner: null,
_owner: owner,
}: any);
}

Expand Down Expand Up @@ -854,7 +855,12 @@ function parseModelTuple(
if (tuple[0] === REACT_ELEMENT_TYPE) {
// TODO: Consider having React just directly accept these arrays as elements.
// Or even change the ReactElement type to be an array.
return createElement(tuple[1], tuple[2], tuple[3]);
return createElement(
tuple[1],
tuple[2],
tuple[3],
__DEV__ ? (tuple: any)[4] : null,
);
}
return value;
}
Expand Down
13 changes: 12 additions & 1 deletion 13 packages/react-server/src/ReactFlightHooks.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import type {Dispatcher} from 'react-reconciler/src/ReactInternalTypes';
import type {Request} from './ReactFlightServer';
import type {Thenable, Usable} from 'shared/ReactTypes';
import type {Thenable, Usable, ReactComponentInfo} from 'shared/ReactTypes';
import type {ThenableState} from './ReactFlightThenable';
import {
REACT_MEMO_CACHE_SENTINEL,
Expand All @@ -21,6 +21,7 @@ import {isClientReference} from './ReactFlightServerConfig';
let currentRequest = null;
let thenableIndexCounter = 0;
let thenableState = null;
let currentComponentDebugInfo = null;

export function prepareToUseHooksForRequest(request: Request) {
currentRequest = request;
Expand All @@ -32,16 +33,26 @@ export function resetHooksForRequest() {

export function prepareToUseHooksForComponent(
prevThenableState: ThenableState | null,
componentDebugInfo: null | ReactComponentInfo,
) {
thenableIndexCounter = 0;
thenableState = prevThenableState;
if (__DEV__) {
currentComponentDebugInfo = componentDebugInfo;
}
}

export function getThenableStateAfterSuspending(): ThenableState {
// If you use() to Suspend this should always exist but if you throw a Promise instead,
// which is not really supported anymore, it will be empty. We use the empty set as a
// marker to know if this was a replay of the same component or first attempt.
const state = thenableState || createThenableState();
if (__DEV__) {
// This is a hack but we stash the debug info here so that we don't need a completely
// different data structure just for this in DEV. Not too happy about it.
(state: any)._componentDebugInfo = currentComponentDebugInfo;
currentComponentDebugInfo = null;
}
thenableState = null;
return state;
}
Expand Down
58 changes: 44 additions & 14 deletions 58 packages/react-server/src/ReactFlightServer.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import type {
ReactComponentInfo,
ReactAsyncInfo,
} from 'shared/ReactTypes';
import type {ReactElement} from 'shared/ReactElementType';
import type {LazyComponent} from 'react/src/ReactLazy';
import type {TemporaryReference} from './ReactFlightServerTemporaryReferences';

Expand Down Expand Up @@ -303,6 +304,7 @@ const {
ReactCurrentCache,
} = ReactServerSharedInternals;
const ReactCurrentDispatcher = ReactSharedInternals.ReactCurrentDispatcher;
const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;

function throwTaintViolation(message: string) {
// eslint-disable-next-line react-internal/prod-error-codes
Expand Down Expand Up @@ -601,6 +603,7 @@ function renderFunctionComponent<Props>(
const prevThenableState = task.thenableState;
task.thenableState = null;

let componentDebugInfo: null | ReactComponentInfo = null;
if (__DEV__) {
if (debugID === null) {
// We don't have a chunk to assign debug info. We need to outline this
Expand All @@ -609,29 +612,41 @@ function renderFunctionComponent<Props>(
} else if (prevThenableState !== null) {
// This is a replay and we've already emitted the debug info of this component
// in the first pass. We skip emitting a duplicate line.
// As a hack we stashed the previous component debug info on this object in DEV.
componentDebugInfo = (prevThenableState: any)._componentDebugInfo;
} else {
// This is a new component in the same task so we can emit more debug info.
const componentName =
(Component: any).displayName || Component.name || '';
request.pendingChunks++;

const componentDebugID = debugID;
const serverComponentMetaData: ReactComponentInfo = {
componentDebugInfo = {
name: componentName,
env: request.environmentName,
};
// We outline this model eagerly so that we can refer to by reference as an owner.
// If we had a smarter way to dedupe we might not have to do this if there ends up
// being no references to this as an owner.
outlineModel(request, serverComponentMetaData);
emitDebugChunk(request, componentDebugID, serverComponentMetaData);
outlineModel(request, componentDebugInfo);
emitDebugChunk(request, componentDebugID, componentDebugInfo);
}
}

prepareToUseHooksForComponent(prevThenableState);
prepareToUseHooksForComponent(prevThenableState, componentDebugInfo);
// The secondArg is always undefined in Server Components since refs error early.
const secondArg = undefined;
let result = Component(props, secondArg);
let result;
if (__DEV__) {
ReactCurrentOwner.current = componentDebugInfo;
try {
result = Component(props, secondArg);
} finally {
ReactCurrentOwner.current = null;
}
} else {
result = Component(props, secondArg);
}
if (
typeof result === 'object' &&
result !== null &&
Expand Down Expand Up @@ -730,9 +745,12 @@ function renderClientElement(
type: any,
key: null | string,
props: any,
owner: null | ReactComponentInfo, // DEV-only
): ReactJSONValue {
if (!enableServerComponentKeys) {
return [REACT_ELEMENT_TYPE, type, key, props];
return __DEV__
? [REACT_ELEMENT_TYPE, type, key, props, owner]
: [REACT_ELEMENT_TYPE, type, key, props];
}
// We prepend the terminal client element that actually gets serialized with
// the keys of any Server Components which are not serialized.
Expand All @@ -742,7 +760,9 @@ function renderClientElement(
} else if (keyPath !== null) {
key = keyPath + ',' + key;
}
const element = [REACT_ELEMENT_TYPE, type, key, props];
const element = __DEV__
? [REACT_ELEMENT_TYPE, type, key, props, owner]
: [REACT_ELEMENT_TYPE, type, key, props];
if (task.implicitSlot && key !== null) {
// The root Server Component had no key so it was in an implicit slot.
// If we had a key lower, it would end up in that slot with an explicit key.
Expand Down Expand Up @@ -788,6 +808,7 @@ function renderElement(
key: null | string,
ref: mixed,
props: any,
owner: null | ReactComponentInfo, // DEV only
): ReactJSONValue {
if (ref !== null && ref !== undefined) {
// When the ref moves to the regular props object this will implicitly
Expand All @@ -808,13 +829,13 @@ function renderElement(
if (typeof type === 'function') {
if (isClientReference(type) || isTemporaryReference(type)) {
// This is a reference to a Client Component.
return renderClientElement(task, type, key, props);
return renderClientElement(task, type, key, props, owner);
}
// This is a Server Component.
return renderFunctionComponent(request, task, key, type, props);
} else if (typeof type === 'string') {
// This is a host element. E.g. HTML.
return renderClientElement(task, type, key, props);
return renderClientElement(task, type, key, props, owner);
} else if (typeof type === 'symbol') {
if (type === REACT_FRAGMENT_TYPE && key === null) {
// For key-less fragments, we add a small optimization to avoid serializing
Expand All @@ -835,24 +856,32 @@ function renderElement(
}
// This might be a built-in React component. We'll let the client decide.
// Any built-in works as long as its props are serializable.
return renderClientElement(task, type, key, props);
return renderClientElement(task, type, key, props, owner);
} else if (type != null && typeof type === 'object') {
if (isClientReference(type)) {
// This is a reference to a Client Component.
return renderClientElement(task, type, key, props);
return renderClientElement(task, type, key, props, owner);
}
switch (type.$$typeof) {
case REACT_LAZY_TYPE: {
const payload = type._payload;
const init = type._init;
const wrappedType = init(payload);
return renderElement(request, task, wrappedType, key, ref, props);
return renderElement(
request,
task,
wrappedType,
key,
ref,
props,
owner,
);
}
case REACT_FORWARD_REF_TYPE: {
return renderFunctionComponent(request, task, key, type.render, props);
}
case REACT_MEMO_TYPE: {
return renderElement(request, task, type.type, key, ref, props);
return renderElement(request, task, type.type, key, ref, props, owner);
}
}
}
Expand Down Expand Up @@ -1363,7 +1392,7 @@ function renderModelDestructive(
writtenObjects.set((value: any).props, NEVER_OUTLINED);
}

const element: React$Element<any> = (value: any);
const element: ReactElement = (value: any);

if (__DEV__) {
const debugInfo: ?ReactDebugInfo = (value: any)._debugInfo;
Expand Down Expand Up @@ -1401,6 +1430,7 @@ function renderModelDestructive(
element.key,
ref,
props,
__DEV__ ? element._owner : null,
);
}
case REACT_LAZY_TYPE: {
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402809 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.