propshaft 1.2.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d3a28b3767740acb1008b833da642398c7f8a7a5d89e2f30b9226ba0cba780d7
4
- data.tar.gz: df4afbce73980e2e35eedd33612cb6ef41c8811d75e1f68438d5e8d34582e7e5
3
+ metadata.gz: 88560fd134482bd4d44518bde9a6813be01d538f16409be833e36664a3c998bc
4
+ data.tar.gz: df495a737f633966e4aa8f7837400353ec2bc9166142e38a94b7d50350c2e092
5
5
  SHA512:
6
- metadata.gz: 24b9c7e548f64d5368c1749bbd9b797fc6dfc5f0ba14c6e0953ae1c551550324515981e9950f87b90c8ce4bf252c03d7bb793d79f7157664fc6762c5ab4f20af
7
- data.tar.gz: f0f88680bd1a415552f96d3f3e5ce639b718bc588c0701e7594d12293e2d6258ac5bc1cf78c8afb46098ee62639b65b81df505482599fce3abb3d797e34a7f55
6
+ metadata.gz: f27ba3bd99cbbaeb643f466c8d37051a988809c1415ccdec2eff5577506eeeed8f4c772167e8afe461b349bb4b4e75aa22bedde7c256d023796929296d1c00e1
7
+ data.tar.gz: 571d8052e3b16c7d779c55433d27c268f0ceb387213ca4b0220a200dc30885e15accc855075e4fbcf95ea71c887ff071bfa25caaa8330f0e9a5c87c33c10076f
@@ -34,8 +34,11 @@ class Propshaft::Assembly
34
34
  end
35
35
  end
36
36
 
37
- def server
38
- Propshaft::Server.new(self)
37
+ def prefix
38
+ @prefix ||= begin
39
+ prefix = config.prefix || "/"
40
+ prefix.end_with?("/") ? prefix : "#{prefix}/"
41
+ end
39
42
  end
40
43
 
41
44
  def processor
@@ -42,14 +42,6 @@ module Propshaft
42
42
  Pathname.new(File.join(app.config.paths["public"].first, app.config.assets.prefix))
43
43
  config.assets.manifest_path ||= config.assets.output_path.join(".manifest.json")
44
44
 
45
- app.assets = Propshaft::Assembly.new(app.config.assets)
46
-
47
- if config.assets.server
48
- app.routes.prepend do
49
- mount app.assets.server, at: app.assets.config.prefix
50
- end
51
- end
52
-
53
45
  ActiveSupport.on_load(:action_view) do
54
46
  include Propshaft::Helper
55
47
  end
@@ -71,6 +63,13 @@ module Propshaft
71
63
  end
72
64
  end
73
65
 
66
+ initializer "propshaft.assets_middleware", group: :all do |app|
67
+ app.assets = Propshaft::Assembly.new(app.config.assets)
68
+ if config.assets.server
69
+ app.middleware.insert_after ::ActionDispatch::Static, Propshaft::Server, app.assets
70
+ end
71
+ end
72
+
74
73
  rake_tasks do
75
74
  load "propshaft/railties/assets.rake"
76
75
  end
@@ -2,30 +2,39 @@ require "rack/utils"
2
2
  require "rack/version"
3
3
 
4
4
  class Propshaft::Server
5
- def initialize(assembly)
5
+ def initialize(app, assembly)
6
+ @app = app
6
7
  @assembly = assembly
7
8
  end
8
9
 
9
10
  def call(env)
10
11
  execute_cache_sweeper_if_updated
11
- path, digest = extract_path_and_digest(env)
12
-
13
- if (asset = @assembly.load_path.find(path)) && asset.fresh?(digest)
14
- compiled_content = asset.compiled_content
15
-
16
- [
17
- 200,
18
- {
19
- Rack::CONTENT_LENGTH => compiled_content.length.to_s,
20
- Rack::CONTENT_TYPE => asset.content_type.to_s,
21
- VARY => "Accept-Encoding",
22
- Rack::ETAG => "\"#{asset.digest}\"",
23
- Rack::CACHE_CONTROL => "public, max-age=31536000, immutable"
24
- },
25
- [ compiled_content ]
26
- ]
12
+
13
+ path = env["PATH_INFO"]
14
+ method = env["REQUEST_METHOD"]
15
+
16
+ if (method == "GET" || method == "HEAD") && path.start_with?(@assembly.prefix)
17
+ path, digest = extract_path_and_digest(path)
18
+
19
+ if (asset = @assembly.load_path.find(path)) && asset.fresh?(digest)
20
+ compiled_content = asset.compiled_content
21
+
22
+ [
23
+ 200,
24
+ {
25
+ Rack::CONTENT_LENGTH => compiled_content.length.to_s,
26
+ Rack::CONTENT_TYPE => asset.content_type.to_s,
27
+ VARY => "Accept-Encoding",
28
+ Rack::ETAG => "\"#{asset.digest}\"",
29
+ Rack::CACHE_CONTROL => "public, max-age=31536000, immutable"
30
+ },
31
+ method == "HEAD" ? [] : [ compiled_content ]
32
+ ]
33
+ else
34
+ [ 404, { Rack::CONTENT_TYPE => "text/plain", Rack::CONTENT_LENGTH => "9" }, [ "Not found" ] ]
35
+ end
27
36
  else
28
- [ 404, { Rack::CONTENT_TYPE => "text/plain", Rack::CONTENT_LENGTH => "9" }, [ "Not found" ] ]
37
+ @app.call(env)
29
38
  end
30
39
  end
31
40
 
@@ -34,10 +43,11 @@ class Propshaft::Server
34
43
  end
35
44
 
36
45
  private
37
- def extract_path_and_digest(env)
38
- full_path = Rack::Utils.unescape(env["PATH_INFO"].to_s.sub(/^\//, ""))
46
+ def extract_path_and_digest(path)
47
+ path = path.delete_prefix(@assembly.prefix)
48
+ path = Rack::Utils.unescape(path)
39
49
 
40
- Propshaft::Asset.extract_path_and_digest(full_path)
50
+ Propshaft::Asset.extract_path_and_digest(path)
41
51
  end
42
52
 
43
53
  if Gem::Version.new(Rack::RELEASE) < Gem::Version.new("3")
@@ -1,3 +1,3 @@
1
1
  module Propshaft
2
- VERSION = "1.2.1"
2
+ VERSION = "1.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: propshaft
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson