Skip to content

Commit 39b142e

Browse files
committed
Reimplement SSR changes in ReactDOMServerFormatConfig
1 parent a59042e commit 39b142e

4 files changed

Lines changed: 58 additions & 36 deletions

File tree

packages/react-dom/src/__tests__/ReactServerRendering-test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,4 +1097,43 @@ describe('ReactDOMServer', () => {
10971097
'However, it is set to a string.',
10981098
);
10991099
});
1100+
1101+
describe('custom element server rendering', () => {
1102+
it('String properties should be server rendered for custom elements', () => {
1103+
const output = ReactDOMServer.renderToString(
1104+
<my-custom-element foo="bar" />,
1105+
);
1106+
expect(output).toBe(`<my-custom-element foo="bar"></my-custom-element>`);
1107+
});
1108+
1109+
it('Number properties should be server rendered for custom elements', () => {
1110+
const output = ReactDOMServer.renderToString(
1111+
<my-custom-element foo={5} />,
1112+
);
1113+
expect(output).toBe(`<my-custom-element foo="5"></my-custom-element>`);
1114+
});
1115+
1116+
// @gate enableCustomElementPropertySupport
1117+
it('Object properties should not be server rendered for custom elements', () => {
1118+
const output = ReactDOMServer.renderToString(
1119+
<my-custom-element foo={{foo: 'bar'}} />,
1120+
);
1121+
expect(output).toBe(`<my-custom-element></my-custom-element>`);
1122+
});
1123+
1124+
// @gate enableCustomElementPropertySupport
1125+
it('Array properties should not be server rendered for custom elements', () => {
1126+
const output = ReactDOMServer.renderToString(
1127+
<my-custom-element foo={['foo', 'bar']} />,
1128+
);
1129+
expect(output).toBe(`<my-custom-element></my-custom-element>`);
1130+
});
1131+
1132+
it('Function properties should not be server rendered for custom elements', () => {
1133+
const output = ReactDOMServer.renderToString(
1134+
<my-custom-element foo={() => console.log('bar')} />,
1135+
);
1136+
expect(output).toBe(`<my-custom-element></my-custom-element>`);
1137+
});
1138+
});
11001139
});

packages/react-dom/src/server/DOMMarkupOperations.js

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
import sanitizeURL from '../shared/sanitizeURL';
1919
import {checkAttributeStringCoercion} from 'shared/CheckStringCoercion';
2020
import quoteAttributeValueForBrowser from './quoteAttributeValueForBrowser';
21-
import {enableCustomElementPropertySupport} from 'shared/ReactFeatureFlags';
2221

2322
/**
2423
* Operations for dealing with DOM properties.
@@ -31,33 +30,12 @@ import {enableCustomElementPropertySupport} from 'shared/ReactFeatureFlags';
3130
* @param {*} value
3231
* @return {?string} Markup string, or null if the property was invalid.
3332
*/
34-
export function createMarkupForProperty(
35-
name: string,
36-
value: mixed,
37-
isCustomComponent: boolean,
38-
): string {
39-
const propertyInfo =
40-
enableCustomElementPropertySupport && isCustomComponent
41-
? null
42-
: getPropertyInfo(name);
43-
if (
44-
name !== 'style' &&
45-
shouldIgnoreAttribute(
46-
name,
47-
propertyInfo,
48-
isCustomComponent && enableCustomElementPropertySupport,
49-
)
50-
) {
33+
export function createMarkupForProperty(name: string, value: mixed): string {
34+
const propertyInfo = getPropertyInfo(name);
35+
if (name !== 'style' && shouldIgnoreAttribute(name, propertyInfo, false)) {
5136
return '';
5237
}
53-
if (
54-
shouldRemoveAttribute(
55-
name,
56-
value,
57-
propertyInfo,
58-
isCustomComponent && enableCustomElementPropertySupport,
59-
)
60-
) {
38+
if (shouldRemoveAttribute(name, value, propertyInfo, false)) {
6139
return '';
6240
}
6341
if (propertyInfo !== null) {

packages/react-dom/src/server/ReactDOMServerFormatConfig.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ function pushAttribute(
385385
return;
386386
}
387387

388+
// is this where i should do stuff?
388389
const propertyInfo = getPropertyInfo(name);
389390
if (propertyInfo !== null) {
390391
// shouldRemoveAttribute
@@ -1103,6 +1104,15 @@ function pushStartCustomElement(
11031104
if (propValue == null) {
11041105
continue;
11051106
}
1107+
if (
1108+
enableCustomElementPropertySupport &&
1109+
(typeof propValue === 'function' || typeof propValue === 'object')
1110+
) {
1111+
// It is normal to render functions and objects on custom elements when
1112+
// client rendering, but when server rendering the output isn't useful,
1113+
// so skip it.
1114+
continue;
1115+
}
11061116
if (enableCustomElementPropertySupport && propKey === 'className') {
11071117
// className gets rendered as class on the client, so it should be
11081118
// rendered as class on the server.

packages/react-dom/src/server/ReactPartialRenderer.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
disableModulePatternComponents,
2424
enableSuspenseServerRenderer,
2525
enableScopeAPI,
26-
enableCustomElementPropertySupport,
2726
} from 'shared/ReactFeatureFlags';
2827
import {
2928
checkPropStringCoercion,
@@ -379,16 +378,12 @@ function createOpenTagMarkup(
379378
propValue = createMarkupForStyles(propValue);
380379
}
381380
let markup = null;
382-
if (enableCustomElementPropertySupport) {
383-
markup = createMarkupForProperty(propKey, propValue, isCustomComponent);
384-
} else {
385-
if (isCustomComponent) {
386-
if (!RESERVED_PROPS.hasOwnProperty(propKey)) {
387-
markup = createMarkupForCustomAttribute(propKey, propValue);
388-
}
389-
} else {
390-
markup = createMarkupForProperty(propKey, propValue, false);
381+
if (isCustomComponent) {
382+
if (!RESERVED_PROPS.hasOwnProperty(propKey)) {
383+
markup = createMarkupForCustomAttribute(propKey, propValue);
391384
}
385+
} else {
386+
markup = createMarkupForProperty(propKey, propValue);
392387
}
393388
if (markup) {
394389
ret += ' ' + markup;

0 commit comments

Comments
 (0)