Skip to content

Commit f8f3e22

Browse files
committed
Unify ReactOnServer.node and .full
1 parent eba5b39 commit f8f3e22

9 files changed

+34
-56
lines changed

CHANGELOG.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@ Please follow the recommendations outlined at [keepachangelog.com](http://keepac
1818
### [Unreleased]
1919
Changes since the last non-beta release.
2020

21-
#### Breaking
22-
- Reduced bundle size [PR 1697](https://github.com/shakacode/react_on_rails/pull/1697) by [Romex91](https://github.com/Romex91)
23-
- Migrated from CJS to ESM for more compact modules (~1KB improvement). **Breaking change:** Dropped CJS support. All projects running `require('react-on-rails')` will need to update to ESM `import ReactOnRails from 'react-on-rails'`.
24-
- Add export option 'react-on-rails/client' to avoid shipping server-rendering code to browsers (~14KB improvement).
25-
2621
#### Fixed
2722
- Fix obscure errors by introducing FULL_TEXT_ERRORS [PR 1695](https://github.com/shakacode/react_on_rails/pull/1695) by [Romex91](https://github.com/Romex91).
23+
- Remove server-side-only functions from client bundles [PR 1697](https://github.com/shakacode/react_on_rails/pull/1697) by [Romex91](https://github.com/Romex91).
2824

2925
### [14.1.1] - 2025-01-15
3026

knip.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const config: KnipConfig = {
44
// ! at the end means files are used in production
55
workspaces: {
66
'.': {
7-
entry: ['node_package/src/ReactOnRails.ts!', 'node_package/src/ReactOnRails.node.ts!'],
7+
entry: ['node_package/src/ReactOnRails.server.ts!'],
88
project: ['node_package/src/**/*.[jt]s!', 'node_package/tests/**/*.[jt]s'],
99
babel: {
1010
config: ['node_package/babel.config.js'],

node_package/src/ReactOnRails.client.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ ctx.ReactOnRails = {
240240
* @param options
241241
*/
242242
serverRenderReactComponent(): null | string | Promise<RenderResult> {
243-
throw new Error('serverRenderReactComponent is not available in "react-on-rails/client". Import "react-on-rails" server-side.');
243+
throw new Error('serverRenderReactComponent is not available in "react-on-rails.client". Import "react-on-rails" server-side.');
244244
},
245245

246246
/**
@@ -256,7 +256,7 @@ ctx.ReactOnRails = {
256256
* @param options
257257
*/
258258
handleError(): string | undefined {
259-
throw new Error('handleError is not available in "react-on-rails/client". Import "react-on-rails" server-side.');
259+
throw new Error('handleError is not available in "react-on-rails.client". Import "react-on-rails" server-side.');
260260
},
261261

262262
/**

node_package/src/ReactOnRails.full.ts

-28
This file was deleted.

node_package/src/ReactOnRails.node.ts

-7
This file was deleted.
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import handleError from './handleError';
2+
import serverRenderReactComponent from './serverRenderReactComponent';
3+
import streamServerRenderedReactComponent from './streamServerRenderedReactComponent';
4+
5+
import ReactOnRails from './ReactOnRails.client';
6+
7+
if (typeof window !== 'undefined') {
8+
console.log("This file shouldn't be loaded in the browser, your configuration may be wrong");
9+
}
10+
11+
ReactOnRails.handleError = handleError;
12+
13+
ReactOnRails.serverRenderReactComponent = serverRenderReactComponent;
14+
15+
ReactOnRails.streamServerRenderedReactComponent = streamServerRenderedReactComponent;
16+
17+
export * from "./types";
18+
export default ReactOnRails;

node_package/src/types/index.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,23 @@ type RenderFunctionResult = ReactComponent | ServerRenderResult | Promise<string
4747
/**
4848
* Render functions are used to create dynamic React components or server-rendered HTML with side effects.
4949
* They receive two arguments: props and railsContext.
50-
*
50+
*
5151
* @param props - The component props passed to the render function
5252
* @param railsContext - The Rails context object containing environment information
5353
* @returns A string, React component, React element, or a Promise resolving to a string
54-
*
54+
*
5555
* @remarks
5656
* To distinguish a render function from a React Function Component:
5757
* 1. Ensure it accepts two parameters (props and railsContext), even if railsContext is unused, or
5858
* 2. Set the `renderFunction` property to `true` on the function object.
59-
*
59+
*
6060
* If neither condition is met, it will be treated as a React Function Component,
6161
* and ReactDOMServer will attempt to render it.
62-
*
62+
*
6363
* @example
6464
* // Option 1: Two-parameter function
6565
* const renderFunction = (props, railsContext) => { ... };
66-
*
66+
*
6767
* // Option 2: Using renderFunction property
6868
* const anotherRenderFunction = (props) => { ... };
6969
* anotherRenderFunction.renderFunction = true;
@@ -82,7 +82,6 @@ export type { // eslint-disable-line import/prefer-default-export
8282
ReactComponent,
8383
AuthenticityHeaders,
8484
RenderFunction,
85-
RenderFunctionResult,
8685
Store,
8786
StoreGenerator,
8887
CreateReactOutputResult,
@@ -131,7 +130,7 @@ export interface ErrorOptions {
131130
serverSide: boolean;
132131
}
133132

134-
export type RenderingError = Pick<Error, 'message' | 'stack'>;
133+
type RenderingError = Pick<Error, 'message' | 'stack'>;
135134

136135
export interface RenderResult {
137136
html: string | null;

package-scripts.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ scripts:
2525
# 3. Check if the project is built now;
2626
# 4. If it failed, print an error message (still follow https://docs.npmjs.com/cli/v8/using-npm/scripts#best-practices).
2727
script: >
28-
[ -f node_package/lib/ReactOnRails.full.js ] ||
28+
[ -f node_package/lib/ReactOnRails.server.js ] ||
2929
(npm run build >/dev/null 2>&1 || true) &&
30-
[ -f node_package/lib/ReactOnRails.full.js ] ||
30+
[ -f node_package/lib/ReactOnRails.server.js ] ||
3131
{ echo 'Building react-on-rails seems to have failed!'; }
3232
3333
format:

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
"description": "react-on-rails JavaScript for react_on_rails Ruby gem",
55
"exports": {
66
".": {
7-
"node": "./node_package/lib/ReactOnRails.node.js",
8-
"default": "./node_package/lib/ReactOnRails.full.js"
9-
},
10-
"./client": "./node_package/lib/ReactOnRails.client.js"
7+
"react-server": "./node_package/lib/ReactOnRails.server.js",
8+
"browser": "./node_package/lib/ReactOnRails.client.js",
9+
"default": "./node_package/lib/ReactOnRails.server.js"
10+
}
1111
},
1212
"directories": {
1313
"doc": "docs"

0 commit comments

Comments
 (0)