assets-redirect 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 73ff04d3845b908b24a79cbee6bf3ecc257008445c6245babf29f631750c6ce0
4
+ data.tar.gz: 192ad5c188be1a7a373730717e877633298cab8cc7ef8501371ebee4ab34ad57
5
+ SHA512:
6
+ metadata.gz: cf6306a7a0622c8fdada4d7f07ccb6fb34d2f1c32b20626fd4d73f54e8ef82f8a18dacd36225b9b2fca12401d689f363300763e20269a25610bdeba42d7ef4ed
7
+ data.tar.gz: bd3477855c425146f750394c8ddccd9860259d8969b17ac521fa9ea51cc7ec4b2e9b6e7d8a2f33c2c8d6d1335842613ff2be119acf38eaca12243515a48de294
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.8
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Jacopo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # Assets::Redirect
2
+
3
+ A Rack middleware for [Rails](https://github.com/rails/rails) with asset pipeline and asset digest enabled. This middleware is used to redirect any not found request to static asset to the latest version with digest in its filename by reading the manifest file generated after you run `rake assets:precompile`
4
+
5
+ For example, if a browser is requesting this URL, and the image with the digest has been removed:
6
+
7
+ http://example.org/assets/dog-faa42cf2fd5db7e7290baa07109bc82b.png
8
+
9
+ They will get redirected to the current version pointed by the manifest:
10
+
11
+ http://example.org/assets/application-faa42cf2fd5db7e7290baa07109bc99b.png
12
+
13
+ This gem is designed to run on your staging or production environment, where you already precompile all your assets, turn on your asset digest, and turn of asset compilation. This is useful if you're having a static page or email which refers to static assets in the asset pipeline, in this case the asset with the old digest can be lost after a deploymeny, and is convenient to automatically show the current version instead of a 404.
14
+
15
+ This gem has been inspired from https://github.com/sikachu/sprockets-redirect, but with the difference that in this gem both digested/undigested links will automatically redirect to the latest digested version.
16
+
17
+ ## Requirements
18
+
19
+ - Application running on [Ruby on Rails](http://github.com/rails/rails) version >= 4.2.0.
20
+ - [Sprockets](https://github.com/rails/sprockets) or [Propshaft](https://github.com/rails/propshaft) as assets pipeline.
21
+
22
+ ## Installation
23
+
24
+ Install the gem and add to the application's Gemfile by executing:
25
+
26
+ $ bundle add assets-redirect
27
+
28
+ ## Usage
29
+
30
+ This middleware will be enabled by default if you set `config.assets.compile = false` and `config.assets.digest = true` in your configuration file.
31
+
32
+ ### Running inside Docker with Sprockets
33
+
34
+ If you run the app in a Docker container and are using Sprockets is suggested to keep the Sprockets cache (`tmp/cache/assets`) in the container, this allows faster redirect
35
+ lookups for `.js` and `.css` files.
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/basecamp/assets-redirect.
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "minitest/test_task"
3
+
4
+ Minitest::TestTask.create
5
+
6
+ task default: :test
@@ -0,0 +1,37 @@
1
+ require_relative "lib/assets/redirect/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "assets-redirect"
5
+ spec.version = Assets::Redirect::VERSION
6
+ spec.authors = ["Jacopo"]
7
+ spec.email = ["jacopo@37signals.com"]
8
+
9
+ spec.summary = "Redirect assets not found to the manifest-digested version for the logical_path."
10
+ spec.description = <<-EOS
11
+ Rack middleware which will redirect every 404 assets requests
12
+ to the current digested version for the logical_path referenced in the manifest.
13
+ Useful to automate redirects from deleted assets when the old link can't be updated, e.g. emails.
14
+ EOS
15
+
16
+ spec.homepage = "https://github.com/basecamp/assets_redirect"
17
+ spec.license = "MIT"
18
+ spec.required_ruby_version = ">= 2.7.8"
19
+
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = spec.homepage
22
+ spec.metadata["changelog_uri"] = %Q(#{spec.metadata["source_code_uri"]}/CHANGELOG.md)
23
+
24
+ # Specify which files should be added to the gem when it is released.
25
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
+ spec.files = Dir.chdir(__dir__) do
27
+ `git ls-files -z`.split("\x0").reject do |f|
28
+ (File.expand_path(f) == __FILE__) ||
29
+ f.start_with?(*%w[bin/ test/ .git .github Gemfile])
30
+ end
31
+ end
32
+ spec.bindir = "exe"
33
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ["lib"]
35
+
36
+ spec.add_dependency "rack"
37
+ end
@@ -0,0 +1,77 @@
1
+ require "rack"
2
+ require "rack/request"
3
+ require "rack/mime"
4
+
5
+ class Assets::Redirect::Base
6
+ def initialize(app, assets_pipeline, public_path: "/public", assets_prefix: "/assets", logger: nil)
7
+ @app = app
8
+ @assets_pipeline = assets_pipeline
9
+ @public_path = public_path
10
+ @assets_prefix = assets_prefix
11
+ @logger = logger
12
+ end
13
+
14
+ def call(env)
15
+ @request = Rack::Request.new(env)
16
+
17
+ if should_redirect?
18
+ redirect_to_resolved_version_from_manifest
19
+ else
20
+ @app.call(env)
21
+ end
22
+ end
23
+
24
+ private
25
+ def should_redirect?
26
+ Assets::Redirect.enabled && asset_not_found? && file_exists?(redirect_path)
27
+ end
28
+
29
+ def asset_not_found?
30
+ @request.path.start_with?(@assets_prefix) && !file_exists?(@request.path)
31
+ end
32
+
33
+ def file_exists?(path)
34
+ full_path = File.join(@public_path, path)
35
+ File.exist?(full_path) && !File.directory?(full_path)
36
+ end
37
+
38
+ def redirect_path
39
+ "#{@assets_prefix}/#{path_from_manifest}"
40
+ end
41
+
42
+ def path_from_manifest
43
+ raise NotImplementedError, "ovverride in subclasses"
44
+ end
45
+
46
+ def computed_assets_pipeline
47
+ @computed_assets_pipeline ||= @assets_pipeline.respond_to?(:call) ? @assets_pipeline.call : @assets_pipeline
48
+ end
49
+
50
+ def logical_path
51
+ strip_assets_prefix(@request.path).sub(/-[a-z0-9]{7,128}\./i, ".")
52
+ end
53
+
54
+ def strip_assets_prefix(path)
55
+ path&.sub(/\/?^#{@assets_prefix}\//, "")
56
+ end
57
+
58
+ def redirect_to_resolved_version_from_manifest
59
+ url = URI(@request.url)
60
+ url.path = redirect_path
61
+
62
+ headers = { "Location" => url.to_s,
63
+ "Content-Type" => Rack::Mime.mime_type(File.extname(path_from_manifest)),
64
+ "Cache-Control" => "no-cache; max-age=0" }
65
+
66
+ log "assets-redirect: Redirecting to #{redirect_path}"
67
+ [ 302, headers, [ redirect_message(url) ] ]
68
+ end
69
+
70
+ def redirect_message(location)
71
+ %[Redirecting to <a href="#{location}">#{location}</a>]
72
+ end
73
+
74
+ def log(msg)
75
+ @logger&.info(msg)
76
+ end
77
+ end
@@ -0,0 +1,9 @@
1
+ require_relative "base"
2
+
3
+ class Assets::Redirect::Propshaft < Assets::Redirect::Base
4
+ private
5
+ def path_from_manifest
6
+ # Propshaft include /assets to the resolved logical_path
7
+ strip_assets_prefix computed_assets_pipeline.resolver.resolve(logical_path)
8
+ end
9
+ end
@@ -0,0 +1,23 @@
1
+ if defined?(Rails)
2
+ class Assets::Redirect::Railtie < ::Rails::Railtie
3
+ initializer "insert_assets_redirect_middleware" do |app|
4
+ if !app.config.assets.compile && app.config.assets.digest
5
+ middleware_class = \
6
+ if defined?(Sprockets::Base)
7
+ Assets::Redirect::Sprockets
8
+ elsif defined?(Propshaft)
9
+ Assets::Redirect::Propshaft
10
+ else
11
+ raise "assets-redirect: Only Sprockets and Propshaft are supported."
12
+ end
13
+
14
+ app.middleware.insert 0,
15
+ middleware_class,
16
+ -> { Rails.application.assets },
17
+ public_path: app.config.paths["public"].first,
18
+ assets_prefix: app.config.assets.prefix,
19
+ logger: Rails.logger
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,8 @@
1
+ require_relative "base"
2
+
3
+ class Assets::Redirect::Sprockets < Assets::Redirect::Base
4
+ private
5
+ def path_from_manifest
6
+ computed_assets_pipeline[ logical_path ]&.digest_path
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ module Assets
2
+ module Redirect
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ require_relative "redirect/version"
2
+ require_relative "redirect/sprockets"
3
+ require_relative "redirect/propshaft"
4
+ require_relative "redirect/railtie"
5
+
6
+ module Assets::Redirect
7
+ @enabled = true
8
+ class << self; attr_accessor :enabled end
9
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: assets-redirect
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jacopo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-04-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: |2
28
+ Rack middleware which will redirect every 404 assets requests
29
+ to the current digested version for the logical_path referenced in the manifest.
30
+ Useful to automate redirects from deleted assets when the old link can't be updated, e.g. emails.
31
+ email:
32
+ - jacopo@37signals.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - ".ruby-version"
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - assets-redirect.gemspec
42
+ - lib/assets/redirect.rb
43
+ - lib/assets/redirect/base.rb
44
+ - lib/assets/redirect/propshaft.rb
45
+ - lib/assets/redirect/railtie.rb
46
+ - lib/assets/redirect/sprockets.rb
47
+ - lib/assets/redirect/version.rb
48
+ homepage: https://github.com/basecamp/assets_redirect
49
+ licenses:
50
+ - MIT
51
+ metadata:
52
+ homepage_uri: https://github.com/basecamp/assets_redirect
53
+ source_code_uri: https://github.com/basecamp/assets_redirect
54
+ changelog_uri: https://github.com/basecamp/assets_redirect/CHANGELOG.md
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 2.7.8
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.1.6
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Redirect assets not found to the manifest-digested version for the logical_path.
74
+ test_files: []