Last active
January 16, 2024 05:10
-
-
Save nikushi/4447f11f12b24db50b42608feeb848e2 to your computer and use it in GitHub Desktop.
Rails HMR with rack-proxy and webpack-dev-server, not using Webpacker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/webpack_dev_server.rb | |
if Rails.development? | |
Rails.application.config.middleware.use WebpackDevServerProxy, dev_server_host: "localhost:9000" | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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