Rack middlewares to make building and serving a Single Page App from a Ruby Rack app easy.
Usually used for serving something like an admin app from the same server as an API, simplifying infrastructure by removing unnecessary static app hosting.
Build your SPA JS app into a folder like /ui-dist:
cd ui
# Need dev deps to build
npm install --production=false
export NODE_ENV=production
npm run build
mv dist ../ui-dist
In config.ru, map your frontend path to the Rack app serving the content:
# This can move to some place else, like an apps.rb, to organize mounting multiple apps.
ui = Rack::Builder.new do
dw = Rack::DynamicConfigWriter.new(
"ui-dist/index.html",
)
env = {
"VITE_API_HOST" => '/',
"VITE_RELEASE" => "[email protected]",
"NODE_ENV" => 'production',
}.merge(Rack::DynamicConfigWriter.pick_env("VITE_"))
index_bytes = dw.as_string(env)
Rack::SpaApp.run_spa_app(self, "ui-dist", enforce_ssl: false, index_bytes:)
end
map "/ui" do
run ui.to_app
endSee demo for a working example (run it from demo folder with rackup demo or make demo from this folder).