Skip to content

Rack middlewares to make building and serving a Single Page App from a Ruby Rack app easy.

License

Notifications You must be signed in to change notification settings

lithictech/rack-spa

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Gem Version Status

rack-spa

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" => "ui@1.0.0",
    "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
end

See demo for a working example (run it from demo folder with rackup demo or make demo from this folder).

About

Rack middlewares to make building and serving a Single Page App from a Ruby Rack app easy.

Resources

License

Stars

Watchers

Forks

Packages

No packages published