Skip to content

Instantly share code, notes, and snippets.

@nikushi
Last active January 16, 2024 05:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikushi/4447f11f12b24db50b42608feeb848e2 to your computer and use it in GitHub Desktop.
Save nikushi/4447f11f12b24db50b42608feeb848e2 to your computer and use it in GitHub Desktop.
Rails HMR with rack-proxy and webpack-dev-server, not using Webpacker
# config/initializers/webpack_dev_server.rb
if Rails.development?
Rails.application.config.middleware.use WebpackDevServerProxy, dev_server_host: "localhost:9000"
end
# lib/webpack_dev_server_proxy.rb
# A proxy to forward requests to GET assets to webpack-dev-server behind Rails server.
class WebpackDevServerProxy < Rack::Proxy
def initialize(app = nil, opts = {})
super
@dev_server_host = opts[:dev_server_host]
end
def perform_request(env)
if env['PATH_INFO'].start_with?('/assets/pack/') # Specify asset paths to proxy
env['HTTP_HOST'] = @dev_server_host
super
else
@app.call(env)
end
end
end
# in config/initializers/webpack_manifest.rb
WebpackManifest::Rails.configuration do |c|
c.cache = !Rails.env.development?
c.manifest = if Rails.env.development?
'http://localhost:9000/assets/pack/manifest.json'
else
Rails.root.join('public', 'assets', 'manifest.json')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment