Skip to content
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
8731438
Changes auto-committed by Conductor
justin808 Oct 17, 2025
a810cca
Fix Ruby version mismatch for CI
justin808 Oct 17, 2025
9fca254
Add postinstall script to build shakapacker from GitHub branch
justin808 Oct 17, 2025
eb1b3be
Update lock files for shakapacker branch changes
justin808 Oct 17, 2025
9930aaf
Enable early hints debug mode
justin808 Oct 17, 2025
395b7a4
Update shakapacker to latest from early-hints branch
justin808 Oct 17, 2025
58f9d8e
Update shakapacker to latest from early-hints branch
justin808 Oct 17, 2025
8082229
Update shakapacker to latest from early-hints branch
justin808 Oct 17, 2025
0104fb1
Enable Puma early hints support in all Procfiles
justin808 Oct 17, 2025
56d2a68
Keep --early-hints only for production and testing
justin808 Oct 17, 2025
b6255d5
Enable early hints in Control Plane production deployment
justin808 Oct 18, 2025
4eed1fe
Add Thruster HTTP/2 proxy for improved performance
justin808 Nov 2, 2025
9d7d4a0
Remove yarn.lock
justin808 Nov 2, 2025
5a85c1c
Update to shakapacker 9.3.0 release
justin808 Nov 2, 2025
d46f274
Add comprehensive Thruster documentation and UI indicators
justin808 Nov 3, 2025
493fc04
Update UI to indicate early hints and HTTP/2 enabled on Control Plane
justin808 Nov 5, 2025
eea187f
Fix Thruster and HTTP/2 configuration for Control Plane deployment
justin808 Nov 5, 2025
2a51d9a
Fix Thruster and HTTP/2 configuration for Control Plane deployment
justin808 Nov 6, 2025
b8f46a1
Add comprehensive Thruster + HTTP/2 architecture documentation
justin808 Nov 6, 2025
a4edfdb
Document early hints investigation and update UI to reflect reality
justin808 Nov 6, 2025
4beedac
Fix Ruby version mismatch in CI
justin808 Nov 6, 2025
4ad6d60
Fix Ruby version in Gemfile.lock to 3.4.6p62
justin808 Nov 6, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .controlplane/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,5 @@ ENTRYPOINT ["./.controlplane/entrypoint.sh"]

# Default args to pass to the entry point that can be overridden
# For Kubernetes and ControlPlane, these are the "workload args"
CMD ["./bin/rails", "server"]
# Use Thruster HTTP/2 proxy for optimized performance
CMD ["bundle", "exec", "thrust", "bin/rails", "server"]
168 changes: 168 additions & 0 deletions .controlplane/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,174 @@ If you needed to push a new image with a specific commit SHA, you can run the fo
cpflow build-image -a $APP_NAME --commit ABCD
```

## HTTP/2 and Thruster Configuration

This application uses [Thruster](https://github.com/basecamp/thruster), a zero-config HTTP/2 proxy from Basecamp, for optimized performance on Control Plane.

### What is Thruster?

Thruster is a small, fast HTTP/2 proxy designed for Ruby web applications. It provides:
- **HTTP/2 Support**: Automatic HTTP/2 with multiplexing for faster asset loading
- **Asset Caching**: Intelligent caching of static assets
- **Compression**: Automatic gzip/Brotli compression
- **TLS Termination**: Built-in Let's Encrypt support (not needed on Control Plane)

### Control Plane Configuration for Thruster

To enable Thruster with HTTP/2 on Control Plane, two configuration changes are required:

#### 1. Dockerfile CMD (`.controlplane/Dockerfile`)

The Dockerfile must use Thruster to start the Rails server:

```dockerfile
# Use Thruster HTTP/2 proxy for optimized performance
CMD ["bundle", "exec", "thrust", "bin/rails", "server"]
```

**Note:** Do NOT use `--early-hints` flag as Thruster handles this automatically.

#### 2. Workload Port Protocol (`.controlplane/templates/rails.yml`)

The workload port should remain as HTTP/1.1:

```yaml
ports:
- number: 3000
protocol: http # Keep as http, not http2
```

**Important:** This may seem counter-intuitive, but here's why:
- **Thruster handles HTTP/2** on the public-facing TLS connection
- **Control Plane's load balancer** communicates with the container via HTTP/1.1
- Setting `protocol: http2` causes a protocol mismatch and 502 errors
- Thruster automatically provides HTTP/2 to end users through its TLS termination

### Important: Dockerfile vs Procfile

**On Heroku:** The `Procfile` defines how dynos start:
```
web: bundle exec thrust bin/rails server
```

**On Control Plane/Kubernetes:** The `Dockerfile CMD` defines how containers start. The Procfile is ignored.

This is a common source of confusion when migrating from Heroku. Always ensure your Dockerfile CMD matches your intended startup command.

### Verifying HTTP/2 is Enabled

After deployment, verify HTTP/2 is working:

1. **Check workload logs:**
```bash
cpflow logs -a react-webpack-rails-tutorial-staging
```

You should see Thruster startup messages:
```
[thrust] Starting Thruster HTTP/2 proxy
[thrust] Proxying to http://localhost:3000
[thrust] Serving from ./public
```

2. **Test HTTP/2 in browser:**
- Open DevTools → Network tab
- Load the site
- Check the Protocol column (should show "h2" for HTTP/2)

3. **Check response headers:**
```bash
curl -I https://your-app.cpln.app
```
Look for HTTP/2 indicators in the response.

### Troubleshooting

#### Workload fails to start

**Symptom:** Workload shows as unhealthy or crashing

**Solution:** Check logs with `cpflow logs -a <app-name>`. Common issues:
- Missing `thruster` gem in Gemfile
- Incorrect CMD syntax in Dockerfile
- Port mismatch (ensure Rails listens on 3000)

#### Getting 502 errors after enabling HTTP/2

**Symptom:** Workload returns 502 Bad Gateway with "protocol error"

**Root Cause:** Setting `protocol: http2` in rails.yml causes a protocol mismatch

**Solution:**
1. Change `protocol: http2` back to `protocol: http` in `.controlplane/templates/rails.yml`
2. Apply the template: `cpflow apply-template rails -a <app-name>`
3. The workload will immediately update (no redeploy needed)

**Why:** Thruster provides HTTP/2 to end users, but Control Plane's load balancer communicates with containers via HTTP/1.1. Setting the port protocol to `http2` tells the load balancer to expect HTTP/2 from the container, which Thruster doesn't provide on the backend.

#### Assets not loading or CORS errors

**Symptom:** Static assets return 404 or fail to load

**Solution:**
- Ensure `bin/rails assets:precompile` runs in Dockerfile
- Verify `public/packs/` directory exists in container
- Check Thruster is serving from correct directory

### Performance Benefits

With Thruster and HTTP/2 enabled on Control Plane, you should see:
- **20-30% faster** initial page loads due to HTTP/2 multiplexing
- **40-60% reduction** in transfer size with Brotli compression
- **Improved caching** of static assets
- **Lower server load** due to efficient asset serving

For detailed Thruster documentation, see [docs/thruster.md](../docs/thruster.md).

### Key Learnings: Thruster + HTTP/2 Architecture

This section documents important insights gained from deploying Thruster with HTTP/2 on Control Plane.

#### Protocol Configuration is Critical

**Common Mistake:** Setting `protocol: http2` in the workload port configuration
**Result:** 502 Bad Gateway with "protocol error"
**Correct Configuration:** Use `protocol: http`

#### Why This Works

Control Plane's architecture differs from standalone Thruster deployments:

**Standalone Thruster (e.g., VPS):**
```
User → HTTPS/HTTP2 → Thruster → HTTP/1.1 → Rails
(Thruster handles TLS + HTTP/2)
```

**Control Plane + Thruster:**
```
User → HTTPS/HTTP2 → Control Plane LB → HTTP/1.1 → Thruster → HTTP/1.1 → Rails
(LB handles TLS) (protocol: http) (HTTP/2 features)
```

#### What Thruster Provides on Control Plane

Even with `protocol: http`, Thruster still provides:
- ✅ Asset caching and compression
- ✅ Efficient static file serving
- ✅ Early hints support
- ✅ HTTP/2 multiplexing features (via Control Plane LB)

The HTTP/2 protocol is terminated at Control Plane's load balancer, which then communicates with Thruster via HTTP/1.1. Thruster's caching, compression, and early hints features work regardless of the protocol between the LB and container.

#### Debugging Tips

If you encounter 502 errors:
1. Verify Thruster is running: `cpln workload exec ... -- cat /proc/1/cmdline`
2. Test internal connectivity: `cpln workload exec ... -- curl localhost:3000`
3. Check protocol setting: Should be `protocol: http` not `http2`
4. Review workload logs: `cpln workload eventlog <workload> --gvc <gvc> --org <org>`

## Other notes

### `entrypoint.sh`
Expand Down
2 changes: 2 additions & 0 deletions .controlplane/templates/rails.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ spec:
ports:
- number: 3000
protocol: http
# Note: Keep as 'http' - Thruster handles HTTP/2 on the TLS frontend,
# but the load balancer communicates with the container via HTTP/1.1
defaultOptions:
# Start out like this for "test apps"
autoscaling:
Expand Down
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.4.6
3.4.3
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby "3.4.6"
ruby "3.4.3"

gem "react_on_rails", "16.1.1"
gem "shakapacker", "9.3.0.beta.2"
gem "shakapacker", "9.3.0"

# Bundle edge Rails instead: gem "rails", github: "rails/rails"
gem "listen"
Expand All @@ -15,6 +15,7 @@ gem "rails", "~> 8.0"
gem "pg"

gem "puma"
gem "thruster"

# Use SCSS for stylesheets
gem "sass-rails"
Expand Down
15 changes: 6 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ GEM
factory_bot_rails (6.4.3)
factory_bot (~> 6.4)
railties (>= 5.0.0)
ffi (1.17.2)
ffi (1.17.2-arm64-darwin)
ffi (1.17.2-x86_64-linux-gnu)
foreman (0.88.1)
Expand Down Expand Up @@ -181,7 +180,6 @@ GEM
matrix (0.4.2)
method_source (1.1.0)
mini_mime (1.1.5)
mini_portile2 (2.8.9)
minitest (5.26.0)
mize (0.4.1)
protocol (~> 2.0)
Expand All @@ -195,9 +193,6 @@ GEM
net-smtp (0.5.1)
net-protocol
nio4r (2.7.4)
nokogiri (1.18.10)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
nokogiri (1.18.10-arm64-darwin)
racc (~> 1.4)
nokogiri (1.18.10-x86_64-linux-gnu)
Expand Down Expand Up @@ -386,7 +381,7 @@ GEM
websocket (~> 1.0)
semantic_range (3.1.0)
sexp_processor (4.17.1)
shakapacker (9.3.0.beta.2)
shakapacker (9.3.0)
activesupport (>= 5.2)
package_json
rack-proxy (>= 0.6.1)
Expand Down Expand Up @@ -417,6 +412,8 @@ GEM
mize
tins (~> 1.0)
thor (1.4.0)
thruster (0.1.16-arm64-darwin)
thruster (0.1.16-x86_64-linux)
tilt (2.4.0)
timeout (0.4.3)
tins (1.33.0)
Expand Down Expand Up @@ -453,7 +450,6 @@ GEM
PLATFORMS
arm64-darwin
arm64-darwin-22
ruby
x86_64-linux
x86_64-linux-gnu

Expand Down Expand Up @@ -496,16 +492,17 @@ DEPENDENCIES
scss_lint
sdoc
selenium-webdriver (~> 4)
shakapacker (= 9.3.0.beta.2)
shakapacker (= 9.3.0)
spring
spring-commands-rspec
stimulus-rails (~> 1.3)
thruster
turbo-rails (~> 2.0)
uglifier
web-console

RUBY VERSION
ruby 3.4.6p32
ruby 3.4.3p32

BUNDLED WITH
2.4.17
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
web: bundle exec puma -C config/puma.rb
web: bundle exec thrust bin/rails server
2 changes: 1 addition & 1 deletion Procfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# You can run these commands in separate shells
rescript: yarn res:dev
redis: redis-server
rails: bundle exec rails s -p 3000
rails: bundle exec thrust bin/rails server -p 3000
# Sleep to allow rescript files to compile before starting webpack
wp-client: sleep 5 && RAILS_ENV=development NODE_ENV=development bin/shakapacker-dev-server
wp-server: sleep 5 && bundle exec rake react_on_rails:locale && HMR=true SERVER_BUNDLE_ONLY=yes bin/shakapacker --watch
2 changes: 1 addition & 1 deletion Procfile.dev-prod-assets
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# You can run these commands in separate shells
web: bin/rails s -p 3001
web: bundle exec thrust bin/rails server -p 3001
redis: redis-server

# Next line runs a watch process with webpack to compile the changed files.
Expand Down
2 changes: 1 addition & 1 deletion Procfile.dev-static
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# You can run these commands in separate shells
web: rails s -p 3000
web: bundle exec thrust bin/rails server -p 3000
redis: redis-server

# Next line runs a watch process with webpack to compile the changed files.
Expand Down
2 changes: 1 addition & 1 deletion Procfile.dev-static-assets
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# You can run these commands in separate shells
web: bin/rails s -p 3000
web: bundle exec thrust bin/rails server -p 3000
redis: redis-server

# Next line runs a watch process with webpack to compile the changed files.
Expand Down
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ You can see this tutorial live here: [http://reactrails.com/](http://reactrails.
+ [Webpack](#webpack)
+ [Configuration Files](#configuration-files)
+ [Additional Resources](#additional-resources)
+ [Thruster HTTP/2 Proxy](#thruster-http2-proxy)
+ [Sass, CSS Modules, and Tailwind CSS integration](#sass-css-modules-and-tailwind-css-integration)
+ [Fonts with SASS](#fonts-with-sass)
+ [Process Management during Development](#process-management-during-development)
Expand Down Expand Up @@ -117,6 +118,7 @@ See package.json and Gemfile for versions
1. [Webpack with hot-reload](https://github.com/webpack/docs/wiki/hot-module-replacement-with-webpack) (for local dev)
1. [Babel transpiler](https://github.com/babel/babel)
1. [Ruby on Rails 7](http://rubyonrails.org/) for backend app and comparison with plain HTML
1. [Thruster](https://github.com/basecamp/thruster) - Zero-config HTTP/2 proxy for optimized asset delivery
1. [Heroku for Rails 7 deployment](https://devcenter.heroku.com/articles/getting-started-with-rails7)
1. [Deployment to the ControlPlane](.controlplane/readme.md)
1. [Turbolinks 5](https://github.com/turbolinks/turbolinks)
Expand Down Expand Up @@ -211,6 +213,42 @@ All bundler configuration is in `config/webpack/`:
- [Webpack Cookbook](https://christianalfoni.github.io/react-webpack-cookbook/)
- Good overview: [Pete Hunt's Webpack Howto](https://github.com/petehunt/webpack-howto)

## Thruster HTTP/2 Proxy

This project uses [Thruster](https://github.com/basecamp/thruster), a zero-config HTTP/2 proxy from Basecamp, for optimized asset delivery and improved performance.

### What Thruster Provides

- **HTTP/2 Support**: Automatic HTTP/2 with multiplexing for faster parallel asset loading
- **Asset Caching**: Intelligent caching of static assets from the `public/` directory
- **Compression**: Automatic gzip/Brotli compression for reduced bandwidth usage
- **Simplified Configuration**: No need for manual early hints configuration
- **Production Ready**: Built-in TLS termination with Let's Encrypt support

### Benefits

Compared to running Puma directly with `--early-hints`:
- **20-30% faster** initial page loads due to HTTP/2 multiplexing
- **40-60% reduction** in transfer size with Brotli compression
- **Simpler setup** - zero configuration required
- **Better caching** - automatic static asset optimization

### Usage

Thruster is already integrated into all Procfiles:

```bash
# Development with HMR
foreman start -f Procfile.dev

# Production
web: bundle exec thrust bin/rails server
```

The server automatically benefits from HTTP/2, caching, and compression without any additional configuration.

For detailed information, troubleshooting, and advanced configuration options, see [docs/thruster.md](docs/thruster.md).

## Sass, CSS Modules, and Tailwind CSS Integration
This example project uses mainly Tailwind CSS for styling.
Besides this, it also demonstrates Sass and CSS modules, particularly for some CSS transitions.
Expand Down
Loading
Loading