Skip to content

Commit 0d1fdb5

Browse files
authored
Add doctype to renderToMarkup when html tags are rendered (#30122)
Stacked on top of #30121. This is the same thing we do for `renderToReadableStream` so that you don't have to manually inject it into the stream. The only reason we didn't for `renderToString` / `renderToStaticMarkup` was to preserve legacy behavior but since this is a new API we can change that. If you're rendering a partial it doesn't matter. This is likely what you'd do for RSS feeds. The question is if you can reliably rely on the doctype being used while rendering e-mails since many clients are so quirky. However, if you're careful it also doesn't hurt so it seems best to include it.
1 parent 1e241f9 commit 0d1fdb5

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

packages/react-html/src/ReactFizzConfigHTML.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,7 @@ export const isPrimaryRenderer = false;
3636
// Disable Client Hooks
3737
export const supportsClientAPIs = false;
3838

39-
import {
40-
stringToChunk,
41-
stringToPrecomputedChunk,
42-
} from 'react-server/src/ReactServerStreamConfig';
43-
44-
// this chunk is empty on purpose because we do not want to emit the DOCTYPE
45-
// when markup is rendering HTML
46-
export const doctypeChunk: PrecomputedChunk = stringToPrecomputedChunk('');
39+
import {stringToChunk} from 'react-server/src/ReactServerStreamConfig';
4740

4841
export type {
4942
RenderState,
@@ -81,6 +74,7 @@ export {
8174
resetResumableState,
8275
completeResumableState,
8376
emitEarlyPreloads,
77+
doctypeChunk,
8478
} from 'react-dom-bindings/src/server/ReactFizzConfigDOM';
8579

8680
import escapeTextForBrowser from 'react-dom-bindings/src/server/escapeTextForBrowser';

packages/react-html/src/__tests__/ReactHTMLClient-test.js

+11
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ describe('ReactHTML', () => {
2828
expect(html).toBe('<div>hello world</div>');
2929
});
3030

31+
it('should prefix html tags with a doctype', async () => {
32+
const html = await ReactHTML.renderToMarkup(
33+
<html>
34+
<body>hello</body>
35+
</html>,
36+
);
37+
expect(html).toBe(
38+
'<!DOCTYPE html><html><head></head><body>hello</body></html>',
39+
);
40+
});
41+
3142
it('should error on useState', async () => {
3243
function Component() {
3344
const [state] = React.useState('hello');

packages/react-html/src/__tests__/ReactHTMLServer-test.js

+14
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ describe('ReactHTML', () => {
3838
expect(html).toBe('<div>hello world</div>');
3939
});
4040

41+
it('should prefix html tags with a doctype', async () => {
42+
const html = await ReactHTML.renderToMarkup(
43+
// We can't use JSX because that's client-JSX in our tests.
44+
React.createElement(
45+
'html',
46+
null,
47+
React.createElement('body', null, 'hello'),
48+
),
49+
);
50+
expect(html).toBe(
51+
'<!DOCTYPE html><html><head></head><body>hello</body></html>',
52+
);
53+
});
54+
4155
it('should error on useState', async () => {
4256
function Component() {
4357
const [state] = React.useState('hello');

0 commit comments

Comments
 (0)