Skip to content

Commit ffec9ec

Browse files
sebmarkbageeps1lon
andauthored
Add new package with renderToMarkup export (#30105)
Name of the package is tbd (straw: `react-html`). It's a new package separate from `react-dom` though and can be used as a standalone package - e.g. also from a React Native app. ```js import {renderToMarkup} from '...'; const html = await renderToMarkup(<Component />); ``` The idea is that this is a helper for rendering HTML that is not intended to be hydrated. It's primarily intended to support a subset of HTML that can be used as embedding and not served as HTML documents from HTTP. For example as e-mails or in RSS/Atom feeds or other distributions. It's a successor to `renderToStaticMarkup`. A few differences: - This doesn't support "Client Components". It can only use the Server Components subset. No useEffect, no useState etc. since it will never be hydrated. Use of those are errors. - You also can't pass Client References so you can't use components marked with `"use client"`. - Unlike `renderToStaticMarkup` this does support async so you can suspend and use data from these components. - Unlike `renderToReadableStream` this does not support streaming or Suspense boundaries and any error rejects the promise. Since there's no feasible way to "client render" or patch up the document. - Form Actions are not supported since in an embedded environment there's no place to post back to across versions. You can render plain forms with fixed URLs though. - You can't use any resource preloading like `preload()` from `react-dom`. ## Implementation This first version in this PR only supports Server Components since that's the thing that doesn't have an existing API. Might add a Client Components version later that errors. We don't want to maintain a completely separate implementation for this use case so this uses the `dom-legacy` build dimension to wire up a build that encapsulates a Flight Server -> Flight Client -> Fizz stream to render Server Components that then get SSR:ed. There's no problem to use a Flight Client in a Server Component environment since it's already supported for Server-to-Server. Both of these use a bundler config that just errors for Client References though since we don't need any bundling integration and this is just a standalone package. Running Fizz in a Server Component environment is a problem though because it depends on "react" and it needs the client version. Therefore, for this build we embed the client version of "react" shared internals into the build. It doesn't need anything to be able to use those APIs since you can't call the client APIs anyway. One unfortunate thing though is that since Flight currently needs to go to binary and back, we need TextEncoder/TextDecoder to be available but this shouldn't really be necessary. Also since we use the legacy stream config, large strings that use byteLengthOfChunk errors atm. This needs to be fixed before shipping. I'm not sure what would be the best layering though that isn't unnecessarily burdensome to maintain. Maybe some kind of pass-through protocol that would also be useful in general - e.g. when Fizz and Flight are in the same process. --------- Co-authored-by: Sebastian Silbermann <[email protected]>
1 parent 3bee073 commit ffec9ec

16 files changed

+627
-20
lines changed

packages/react-client/src/forks/ReactFlightClientConfig.dom-bun.js

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export * from 'react-client/src/ReactFlightClientStreamConfigWeb';
1111
export * from 'react-client/src/ReactClientConsoleConfigPlain';
1212
export * from 'react-dom-bindings/src/shared/ReactFlightClientConfigDOM';
1313

14-
export type Response = any;
1514
export opaque type ModuleLoading = mixed;
1615
export opaque type SSRModuleMap = mixed;
1716
export opaque type ServerManifest = mixed;

packages/react-client/src/forks/ReactFlightClientConfig.dom-legacy.js

+77-15
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,82 @@
77
* @flow
88
*/
99

10-
export * from 'react-client/src/ReactFlightClientStreamConfigWeb';
11-
export * from 'react-client/src/ReactClientConsoleConfigBrowser';
12-
export * from 'react-dom-bindings/src/shared/ReactFlightClientConfigDOM';
13-
14-
export type Response = any;
15-
export opaque type ModuleLoading = mixed;
16-
export opaque type SSRModuleMap = mixed;
17-
export opaque type ServerManifest = mixed;
10+
import type {Thenable} from 'shared/ReactTypes';
11+
12+
export * from 'react-html/src/ReactHTMLLegacyClientStreamConfig.js';
13+
export * from 'react-client/src/ReactClientConsoleConfigPlain';
14+
15+
export type ModuleLoading = null;
16+
export type SSRModuleMap = null;
17+
export opaque type ServerManifest = null;
1818
export opaque type ServerReferenceId = string;
19-
export opaque type ClientReferenceMetadata = mixed;
20-
export opaque type ClientReference<T> = mixed; // eslint-disable-line no-unused-vars
21-
export const resolveClientReference: any = null;
22-
export const resolveServerReference: any = null;
23-
export const preloadModule: any = null;
24-
export const requireModule: any = null;
25-
export const prepareDestinationForModule: any = null;
19+
export opaque type ClientReferenceMetadata = null;
20+
export opaque type ClientReference<T> = null; // eslint-disable-line no-unused-vars
21+
22+
export function prepareDestinationForModule(
23+
moduleLoading: ModuleLoading,
24+
nonce: ?string,
25+
metadata: ClientReferenceMetadata,
26+
) {
27+
throw new Error(
28+
'renderToMarkup should not have emitted Client References. This is a bug in React.',
29+
);
30+
}
31+
32+
export function resolveClientReference<T>(
33+
bundlerConfig: SSRModuleMap,
34+
metadata: ClientReferenceMetadata,
35+
): ClientReference<T> {
36+
throw new Error(
37+
'renderToMarkup should not have emitted Client References. This is a bug in React.',
38+
);
39+
}
40+
41+
export function resolveServerReference<T>(
42+
config: ServerManifest,
43+
id: ServerReferenceId,
44+
): ClientReference<T> {
45+
throw new Error(
46+
'renderToMarkup should not have emitted Server References. This is a bug in React.',
47+
);
48+
}
49+
50+
export function preloadModule<T>(
51+
metadata: ClientReference<T>,
52+
): null | Thenable<T> {
53+
return null;
54+
}
55+
56+
export function requireModule<T>(metadata: ClientReference<T>): T {
57+
throw new Error(
58+
'renderToMarkup should not have emitted Client References. This is a bug in React.',
59+
);
60+
}
61+
2662
export const usedWithSSR = true;
63+
64+
type HintCode = string;
65+
type HintModel<T: HintCode> = null; // eslint-disable-line no-unused-vars
66+
67+
export function dispatchHint<Code: HintCode>(
68+
code: Code,
69+
model: HintModel<Code>,
70+
): void {
71+
// Should never happen.
72+
}
73+
74+
export function preinitModuleForSSR(
75+
href: string,
76+
nonce: ?string,
77+
crossOrigin: ?string,
78+
) {
79+
// Should never happen.
80+
}
81+
82+
export function preinitScriptForSSR(
83+
href: string,
84+
nonce: ?string,
85+
crossOrigin: ?string,
86+
) {
87+
// Should never happen.
88+
}

packages/react-html/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# `react-html`
2+
3+
This package provides the ability to render standalone HTML from Server Components for use in embedded contexts such as e-mails and RSS/Atom feeds. It cannot use Client Components and does not hydrate. It is intended to be paired with the generic React package, which is shipped as `react` to npm.
4+
5+
## Installation
6+
7+
```sh
8+
npm install react react-html
9+
```
10+
11+
## Usage
12+
13+
```js
14+
import { renderToMarkup } from 'react-html';
15+
import EmailTemplate from './my-email-template-component.js'
16+
17+
async function action(email, name) {
18+
"use server";
19+
// ... in your server, e.g. a Server Action...
20+
const htmlString = await renderToMarkup(<EmailTemplate name={name} />);
21+
// ... send e-mail using some e-mail provider
22+
await sendEmail({ to: email, contentType: 'text/html', body: htmlString });
23+
}
24+
```
25+
26+
Note that this is an async function that needs to be awaited - unlike the legacy `renderToString` in `react-dom`.
27+
28+
## API
29+
30+
### `react-html`
31+
32+
See https://react.dev/reference/react-html

packages/react-html/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
throw new Error(
4+
'react-html is not supported outside a React Server Components environment.',
5+
);

packages/react-html/npm/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
throw new Error(
4+
'react-html is not supported outside a React Server Components environment.'
5+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
if (process.env.NODE_ENV === 'production') {
4+
module.exports = require('./cjs/react-html.react-server.production.js');
5+
} else {
6+
module.exports = require('./cjs/react-html.react-server.development.js');
7+
}

packages/react-html/package.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "react-html",
3+
"version": "19.0.0",
4+
"private": true,
5+
"description": "React package generating embedded HTML markup such as e-mails using Server Components.",
6+
"main": "index.js",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/facebook/react.git",
10+
"directory": "packages/react-html"
11+
},
12+
"keywords": [
13+
"react"
14+
],
15+
"license": "MIT",
16+
"bugs": {
17+
"url": "https://github.com/facebook/react/issues"
18+
},
19+
"homepage": "https://react.dev/",
20+
"peerDependencies": {
21+
"react": "^19.0.0"
22+
},
23+
"files": [
24+
"LICENSE",
25+
"README.md",
26+
"index.js",
27+
"react-html.react-server.js",
28+
"cjs/"
29+
],
30+
"exports": {
31+
".": {
32+
"react-server": "./react-html.react-server.js",
33+
"default": "./index.js"
34+
},
35+
"./src/*": "./src/*",
36+
"./package.json": "./package.json"
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
export * from './src/ReactHTMLServer';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
// TODO: The legacy one should not use binary.
11+
12+
export type StringDecoder = TextDecoder;
13+
14+
export function createStringDecoder(): StringDecoder {
15+
return new TextDecoder();
16+
}
17+
18+
const decoderOptions = {stream: true};
19+
20+
export function readPartialStringChunk(
21+
decoder: StringDecoder,
22+
buffer: Uint8Array,
23+
): string {
24+
return decoder.decode(buffer, decoderOptions);
25+
}
26+
27+
export function readFinalStringChunk(
28+
decoder: StringDecoder,
29+
buffer: Uint8Array,
30+
): string {
31+
return decoder.decode(buffer);
32+
}

0 commit comments

Comments
 (0)