module ActionController::AllowBrowser::ClassMethods
def allow_browser(versions:, block: -> { render file: Rails.root.join("public/406-unsupported-browser.html"), layout: false, status: :not_acceptable }, **options)
allow_browser versions: { opera: 104, chrome: 119 }, only: :show
# In addition to the browsers blocked by ApplicationController, also block Opera below 104 and Chrome below 119 for the show action.
class MessagesController < ApplicationController
end
allow_browser versions: { safari: 16.4, firefox: 121, ie: false }
# All versions of Chrome and Opera will be allowed, but no versions of "internet explorer" (ie). Safari needs to be 16.4+ and Firefox 121+.
class ApplicationController < ActionController::Base
end
end
render file: Rails.root.join("public/custom-error.html"), status: :not_acceptable
def handle_outdated_browser
private
allow_browser versions: :modern, block: :handle_outdated_browser
# Allow only browsers natively supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has
class ApplicationController < ActionController::Base
end
allow_browser versions: :modern
# Allow only browsers natively supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has
class ApplicationController < ActionController::Base
Examples:
being blocked using the `browser_block.action_controller` event name.
You can use `ActiveSupport::Notifications` to subscribe to events of browsers
features you use.
You can use https://caniuse.com to check for browser versions supporting the
includes Safari 17.2+, Chrome 120+, Firefox 121+, Opera 106+.
images, web push, badges, import maps, CSS nesting, and CSS :has. This
`:modern` as the set to restrict support to browsers natively supporting webp
In addition to specifically named browser versions, you can also pass
Acceptable".
public/406-unsupported-browser.html with a HTTP status code of "406 Not
A browser that's blocked will by default be served the file in
aren't reporting a user-agent header, will be allowed access.
versions specified. This means that all other browsers, as well as agents that
or named set passed to `versions:` will be blocked if they're below the
some, as limited by `only:` or `except:`). Only browsers matched in the hash
Specify the browser versions that will be allowed to access all actions (or
def allow_browser(versions:, block: -> { render file: Rails.root.join("public/406-unsupported-browser.html"), layout: false, status: :not_acceptable }, **options) before_action -> { allow_browser(versions: versions, block: block) }, **options end