Skip to content

Commit 37f4534

Browse files
committed
Address PR #112 review comments for basic-v16-rsc
1 parent dae8efa commit 37f4534

24 files changed

Lines changed: 4536 additions & 456 deletions

File tree

demos/basic-v16-rsc/.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
../../packages/shakacode_demo_common/config/.eslintrc.js
1+
../../packages/shakacode_demo_common/configs/.eslintrc.js

demos/basic-v16-rsc/.github/workflows/ci.yml

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#
2+
# NOTE:
3+
# This workflow is a template for running this demo as a standalone repo.
4+
# GitHub Actions in this monorepo are only discovered from the root
5+
# `.github/workflows/` directory.
6+
#
17
name: CI
28

39
on:
@@ -10,7 +16,7 @@ jobs:
1016
lint-ruby:
1117
runs-on: ubuntu-latest
1218
steps:
13-
- uses: actions/checkout@v3
19+
- uses: actions/checkout@v4
1420
- name: Set up Ruby
1521
uses: ruby/setup-ruby@v1
1622
with:
@@ -22,11 +28,11 @@ jobs:
2228
lint-javascript:
2329
runs-on: ubuntu-latest
2430
steps:
25-
- uses: actions/checkout@v3
31+
- uses: actions/checkout@v4
2632
- name: Set up Node
27-
uses: actions/setup-node@v3
33+
uses: actions/setup-node@v4
2834
with:
29-
node-version: '18'
35+
node-version: '20'
3036
cache: 'npm'
3137
- name: Install dependencies
3238
run: npm ci
@@ -51,16 +57,16 @@ jobs:
5157
- 5432:5432
5258

5359
steps:
54-
- uses: actions/checkout@v3
60+
- uses: actions/checkout@v4
5561
- name: Set up Ruby
5662
uses: ruby/setup-ruby@v1
5763
with:
5864
ruby-version: '3.2'
5965
bundler-cache: true
6066
- name: Set up Node
61-
uses: actions/setup-node@v3
67+
uses: actions/setup-node@v4
6268
with:
63-
node-version: '18'
69+
node-version: '20'
6470
cache: 'npm'
6571
- name: Install dependencies
6672
run: |
@@ -73,22 +79,13 @@ jobs:
7379
run: |
7480
bundle exec rails db:create
7581
bundle exec rails db:schema:load
82+
- name: Build JavaScript bundles
83+
env:
84+
RAILS_ENV: test
85+
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/test
86+
run: RAILS_ENV=test bin/shakapacker
7687
- name: Run tests
7788
env:
7889
RAILS_ENV: test
7990
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/test
8091
run: bundle exec rails test
81-
82-
test-javascript:
83-
runs-on: ubuntu-latest
84-
steps:
85-
- uses: actions/checkout@v3
86-
- name: Set up Node
87-
uses: actions/setup-node@v3
88-
with:
89-
node-version: '18'
90-
cache: 'npm'
91-
- name: Install dependencies
92-
run: npm ci
93-
- name: Run Jest tests
94-
run: npm test

demos/basic-v16-rsc/README.md

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,49 @@
1-
# README
1+
# basic-v16-rsc
22

3-
This README would normally document whatever steps are necessary to get the
4-
application up and running.
3+
React on Rails Pro demo for React Server Components (RSC) streaming.
54

6-
Things you may want to cover:
5+
## Requirements
76

8-
* Ruby version
7+
- Ruby 3.2+
8+
- Node 20+ (this app uses `npm@11`)
9+
- PostgreSQL
910

10-
* System dependencies
11+
## Setup
1112

12-
* Configuration
13+
```bash
14+
# Install dependencies
15+
bundle install
16+
npm ci
1317

14-
* Database creation
18+
# Database setup
19+
bin/rails db:create
20+
bin/rails db:migrate
21+
```
1522

16-
* Database initialization
23+
## Run
1724

18-
* How to run the test suite
25+
```bash
26+
# First run only: generate packs before starting dev processes
27+
ruby bin/shakapacker-precompile-hook
1928

20-
* Services (job queues, cache servers, search engines, etc.)
29+
# Start Rails + renderer + bundler processes
30+
bin/dev
31+
```
2132

22-
* Deployment instructions
33+
Open: `http://localhost:3000/hello_server`
2334

24-
* ...
35+
## Test
36+
37+
```bash
38+
# Rails tests
39+
bin/rails test
40+
41+
# Lint
42+
npm run lint
43+
bundle exec rubocop
44+
```
45+
46+
## Notes
47+
48+
- This demo includes a Node renderer secured by `RENDERER_PASSWORD`.
49+
- In production-like environments, set `RENDERER_PASSWORD` explicitly.

demos/basic-v16-rsc/app/javascript/src/HelloServer/components/HelloServer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
// For more information, see:
1212
// https://www.shakacode.com/react-on-rails-pro/docs/react-server-components/
1313

14-
import React from 'react';
1514
import LikeButton from './LikeButton';
1615

1716
interface HelloServerProps {
@@ -24,7 +23,8 @@ interface GreetingData {
2423
facts: string[];
2524
}
2625

27-
// Simulate an async data fetch (replace with a real API call or DB query)
26+
// Simulate an async data fetch (replace with a real API call or DB query).
27+
// This delay is intentionally artificial for demo purposes.
2828
async function fetchGreeting(name: string): Promise<GreetingData> {
2929
// In a real app, you could do:
3030
// const data = await db.query('SELECT greeting FROM greetings WHERE name = ?', [name]);

demos/basic-v16-rsc/app/views/layouts/application.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!DOCTYPE html>
2-
<html>
2+
<html lang="en">
33
<head>
44
<title><%= content_for(:title) || "Basic V16 Rsc" %></title>
55
<meta name="viewport" content="width=device-width,initial-scale=1">
@@ -18,7 +18,7 @@
1818
<link rel="apple-touch-icon" href="/icon.png">
1919

2020
<%# Includes all stylesheet files in app/assets/stylesheets %>
21-
<%= stylesheet_link_tag :app %>
21+
<%= stylesheet_link_tag :application %>
2222
<%= javascript_pack_tag "application" %>
2323
</head>
2424

demos/basic-v16-rsc/bin/shakapacker-precompile-hook

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,19 @@ exit 0 if ENV["SHAKAPACKER_SKIP_PRECOMPILE_HOOK"] == "true"
2424
ENV["REACT_ON_RAILS_SKIP_VALIDATION"] = "true"
2525

2626
require_relative "../config/environment"
27+
begin
28+
require "rainbow"
29+
rescue LoadError
30+
nil
31+
end
2732

2833
begin
29-
puts Rainbow("🔄 Running React on Rails precompile hook...").cyan
34+
start_msg = "🔄 Running React on Rails precompile hook..."
35+
puts defined?(Rainbow) ? Rainbow(start_msg).cyan : start_msg
3036
ReactOnRails::PacksGenerator.instance.generate_packs_if_stale
3137
rescue StandardError => e
32-
warn Rainbow("❌ Error in precompile hook: #{e.message}").red
38+
error_msg = "❌ Error in precompile hook: #{e.message}"
39+
warn defined?(Rainbow) ? Rainbow(error_msg).red : error_msg
3340
warn e.backtrace.first(5).join("\n")
3441
exit 1
3542
end

demos/basic-v16-rsc/bin/switch-bundler

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class BundlerSwitcher
9191
end
9292

9393
puts "✅ Removed #{@target_bundler == 'rspack' ? 'webpack' : 'rspack'} dependencies"
94-
File.write(package_json_path, JSON.pretty_generate(package_json))
94+
File.write(package_json_path, "#{JSON.pretty_generate(package_json)}\n")
9595
end
9696

9797
def install_dependencies

demos/basic-v16-rsc/client/node-renderer.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
const path = require('path');
2+
23
const { reactOnRailsProNodeRenderer } = require('react-on-rails-pro-node-renderer');
34

45
const { env } = process;
6+
const parsedConcurrency =
7+
env.NODE_RENDERER_CONCURRENCY != null ? Number(env.NODE_RENDERER_CONCURRENCY) : undefined;
58

69
const config = {
710
serverBundleCachePath: path.resolve(__dirname, '../.node-renderer-bundles'),
811
port: Number(env.RENDERER_PORT) || 3800,
912
logLevel: env.RENDERER_LOG_LEVEL || 'info',
1013

1114
// See value in /config/initializers/react_on_rails_pro.rb
12-
password: env.RENDERER_PASSWORD || 'devPassword',
15+
password: (() => {
16+
if (!env.RENDERER_PASSWORD && env.NODE_ENV === 'production') {
17+
throw new Error('RENDERER_PASSWORD must be set in production');
18+
}
19+
return env.RENDERER_PASSWORD || 'devPassword';
20+
})(),
1321

1422
// Number of Node.js worker threads for SSR rendering
1523
// Set NODE_RENDERER_CONCURRENCY env var to override (e.g., for production tuning)
16-
workersCount: env.NODE_RENDERER_CONCURRENCY != null ? Number(env.NODE_RENDERER_CONCURRENCY) : 3,
24+
workersCount: parsedConcurrency ?? 3,
1725

1826
// If set to true, `supportModules` enables the server-bundle code to call a default set of NodeJS modules
1927
// that get added to the VM context: { Buffer, process, setTimeout, setInterval, clearTimeout, clearInterval }.
@@ -34,7 +42,7 @@ const config = {
3442
// Renderer detects a total number of CPUs on virtual hostings like Heroku or CircleCI instead
3543
// of CPUs number allocated for current container. This results in spawning many workers while
3644
// only 1-2 of them really needed.
37-
if (env.CI) {
45+
if (env.CI && env.NODE_RENDERER_CONCURRENCY == null) {
3846
config.workersCount = 2;
3947
}
4048

demos/basic-v16-rsc/config/initializers/cypress_on_rails.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
c.install_folder = File.expand_path("#{__dir__}/../../e2e")
55
# WARNING!! CypressOnRails can execute arbitrary ruby code
66
# please use with extra caution if enabling on hosted servers or starting your local server on 0.0.0.0
7-
c.use_middleware = !Rails.env.production?
7+
c.use_middleware = Rails.env.development? || Rails.env.test?
88
# c.use_vcr_middleware = !Rails.env.production?
99
# # Use this if you want to use use_cassette wrapper instead of manual insert/eject
1010
# # c.use_vcr_use_cassette_middleware = !Rails.env.production?

demos/basic-v16-rsc/config/initializers/react_on_rails.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,16 @@
4040
# - Simpler configuration (no additional setup needed)
4141
# - Handled automatically by Shakapacker
4242
#
43-
# ALTERNATIVE APPROACH: Uncomment below AND configure ReactOnRails::TestHelper
43+
# ALTERNATIVE APPROACH: Keep the setting below enabled and configure ReactOnRails::TestHelper
4444
# - Provides explicit control over test asset compilation timing
4545
# - Requires adding ReactOnRails::TestHelper to spec/rails_helper.rb
4646
# - See: https://github.com/shakacode/react_on_rails/blob/master/docs/building-features/testing-configuration.md
4747
#
4848
config.build_test_command = "RAILS_ENV=test bin/shakapacker"
4949

50+
################################################################################
51+
# Component Loading
52+
################################################################################
5053
config.auto_load_bundle = true
5154
config.components_subdirectory = "ror_components"
5255
################################################################################

0 commit comments

Comments
 (0)