Skip to content

Commit

Permalink
Export stream functions only for node bundles (#1681)
Browse files Browse the repository at this point in the history
* Add export for ReactOnRails for Node.js which supports streaming

* adding default export from ReactOnRails module in ReactOnRails.node
  • Loading branch information
AbanoubGhadban authored Jan 16, 2025
1 parent 09c4246 commit 42b1830
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 8 deletions.
7 changes: 7 additions & 0 deletions node_package/src/ReactOnRails.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ReactOnRails from './ReactOnRails';
import streamServerRenderedReactComponent from './streamServerRenderedReactComponent';

ReactOnRails.streamServerRenderedReactComponent = streamServerRenderedReactComponent;

export * from './ReactOnRails';
export { default } from './ReactOnRails';
8 changes: 8 additions & 0 deletions node_package/src/ReactOnRails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@ ctx.ReactOnRails = {
return serverRenderReactComponent(options);
},

/**
* Used by server rendering by Rails
* @param options
*/
streamServerRenderedReactComponent() {
throw new Error('streamServerRenderedReactComponent is only supported when using a bundle built for Node.js environments');
},

/**
* Used by Rails to catch errors in rendering
* @param options
Expand Down
1 change: 1 addition & 0 deletions node_package/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export interface ReactOnRails {
): RenderReturnType;
getComponent(name: string): RegisteredComponent;
serverRenderReactComponent(options: RenderParams): null | string | Promise<RenderResult>;
streamServerRenderedReactComponent(options: RenderParams): Readable;
handleError(options: ErrorOptions): string | undefined;
buildConsoleReplay(): string;
registeredComponents(): Map<string, RegisteredComponent>;
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
"name": "react-on-rails",
"version": "14.1.0",
"description": "react-on-rails JavaScript for react_on_rails Ruby gem",
"main": "node_package/lib/ReactOnRails.js",
"exports": {
".": {
"node": "./node_package/lib/ReactOnRails.node.js",
"default": "./node_package/lib/ReactOnRails.js"
}
},
"directories": {
"doc": "docs"
},
Expand Down
7 changes: 4 additions & 3 deletions spec/dummy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-helmet": "^6.1.0",
"react-on-rails": "file:.yalc/react-on-rails",
"react-on-rails": "link:.yalc/react-on-rails",
"react-proptypes": "^1.0.0",
"react-redux": "^8.0.2",
"react-router-dom": "^5.2.0",
Expand Down Expand Up @@ -72,12 +72,13 @@
"fs": false
},
"scripts": {
"yalc-postinstall": "yalc link react-on-rails",
"preinstall": "yarn run link-source && yalc add --link react-on-rails",
"link-source": "cd ../.. && yarn run build && yalc publish",
"lint": "cd ../.. && yarn run lint",
"format": "cd ../.. && yarn start format",
"test": "yarn run build:test && yarn run lint && rspec",
"build:test": "rm -rf public/webpack/test && yarn build:rescript && RAILS_ENV=test NODE_ENV=test bin/shakapacker",
"build:dev": "rm -rf public/webpack/development && yarn build:rescript && RAILS_ENV=development NODE_ENV=development bin/shakapacker --watch",
"build:dev": "rm -rf public/webpack/development && yarn build:rescript && RAILS_ENV=development NODE_ENV=development bin/shakapacker",
"build:dev:server": "rm -rf public/webpack/development && yarn build:rescript && RAILS_ENV=development NODE_ENV=development bin/shakapacker --watch",
"build:dev:watch": "rescript build -w && RAILS_ENV=development NODE_ENV=development bin/shakapacker --watch",
"build:clean": "yarn build:rescript && rm -rf public/webpack || true",
Expand Down
7 changes: 3 additions & 4 deletions spec/dummy/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6451,10 +6451,9 @@ react-is@^18.0.0:
resolved "https://registry.yarnpkg.com/react-is/-/react-is-18.2.0.tgz#199431eeaaa2e09f86427efbb4f1473edb47609b"
integrity sha512-xWGDIW6x921xtzPkhiULtthJHoJvBbF3q26fzloPCK0hsvxtPVelvftw3zjbHWSkR2km9Z+4uxbDDK/6Zw9B8w==

"react-on-rails@file:.yalc/react-on-rails":
version "14.0.0"
dependencies:
"@babel/runtime-corejs3" "^7.12.5"
"react-on-rails@link:.yalc/react-on-rails":
version "0.0.0"
uid ""

react-proptypes@^1.0.0:
version "1.0.0"
Expand Down

0 comments on commit 42b1830

Please sign in to comment.