Skip to content

Commit 45d3866

Browse files
committed
Add public_origin attribute to Configuration::DevServerSettings
This is an _optional_ configuration attribute which will be used to generate the URLs in asset tags. If this isn't set, the asset origin defaults to `"http://#{dev_server.host}:#{dev_server.port}"`, so `public_origin` only needs to be set if that default doesn't work. In essence, this setting allows different origins to be used when the Rails backend connects to the dev server to get the manifest and when the browser requests assets from the dev server. (Often, though, the same origin can be used, which is why this setting is optional.)
1 parent f2b2d4d commit 45d3866

File tree

5 files changed

+24
-2
lines changed

5 files changed

+24
-2
lines changed

Diff for: README.md

+10
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,23 @@ config.external_asset_pipeline.dev_server.host = 'localhost'
9696
config.external_asset_pipeline.dev_server.port = 9000
9797
```
9898

99+
You may _optionally_ also configure the `dev_server.public_origin` setting,
100+
which will be used to generate the URLs in your asset tags. If this isn't set,
101+
the asset origin defaults to `"http://#{dev_server.host}:#{dev_server.port}"`,
102+
so `public_origin` only needs to be set if that default doesn't work <sup>1</sup>.
103+
99104
If the dev server is enabled but not running (i.e. we can't establish a
100105
connection to that port), the app will automatically fall back to returning
101106
assets from disk.
102107
103108
See an example in
104109
[`examples/demo_app/config/environments/development.rb`](./examples/demo_app/config/environments/development.rb)
105110
111+
<sup><sup>1</sup> For example, if you're running everything in docker containers
112+
then the origin from which the browser requests assets may be different than the
113+
origin that the rails container uses to request the manifest from the dev server
114+
container.</sup>
115+
106116
[`webpack-dev-server`]: https://github.com/webpack/webpack-dev-server
107117

108118
## Development

Diff for: lib/external_asset_pipeline/configuration.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class DevServerSettings
1919
attr_accessor :connect_timeout,
2020
:enabled,
2121
:host,
22-
:port
22+
:port,
23+
:public_origin
2324

2425
def initialize
2526
@connect_timeout = 0.01

Diff for: lib/external_asset_pipeline/dev_server.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def get(path)
1313
end
1414

1515
def origin
16-
"http://#{@config.host}:#{@config.port}"
16+
@config.public_origin || "http://#{@config.host}:#{@config.port}"
1717
end
1818

1919
def running?

Diff for: test/external_asset_pipeline/configuration_test.rb

+3
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,21 @@ def test_dev_server_settings
4545
assert_nil config.dev_server.enabled
4646
assert_nil config.dev_server.host
4747
assert_nil config.dev_server.port
48+
assert_nil config.dev_server.public_origin
4849

4950
config.configure do |c|
5051
c.dev_server.connect_timeout = 0.5
5152
c.dev_server.enabled = true
5253
c.dev_server.host = 'localhost'
5354
c.dev_server.port = 9000
55+
c.dev_server.public_origin = 'http://example-app.test'
5456
end
5557

5658
assert_equal 0.5, config.dev_server.connect_timeout
5759
assert_equal true, config.dev_server.enabled
5860
assert_equal 'localhost', config.dev_server.host
5961
assert_equal 9000, config.dev_server.port
62+
assert_equal 'http://example-app.test', config.dev_server.public_origin
6063
end
6164

6265
def test_logger

Diff for: test/external_asset_pipeline/dev_server_test.rb

+8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ def test_origin
3535
dev_server = DevServer.new(config)
3636

3737
assert_equal "http://#{config.host}:9666", dev_server.origin
38+
39+
config.public_origin = 'http://myapp.test:4444'
40+
41+
assert_equal 'http://myapp.test:4444', dev_server.origin
42+
43+
config.public_origin = nil
44+
45+
assert_equal "http://#{config.host}:9666", dev_server.origin
3846
end
3947

4048
def test_running

0 commit comments

Comments
 (0)