Rails 8 adds allow_browser to set minimum browser version
source link: https://blog.saeloun.com/2024/03/18/rails-8-adds-allow-browser-to-set-minimum-versions/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Rails 8 adds allow_browser to set minimum browser version
Rails 8 adds allow_browser to set minimum browser version
Mar 18, 2024
Jijo Bose
I'm a Ruby on Rails and React Native enthusiast with over four years of experience creating innovative software solutions. I work well in teams and am always up for challenging projects. I enjoy playing chess and gaming in my free time.
Browser compatibility is critical for ensuring that a website displays and performs properly across several web browsers. Every browser renders code differently, thus compatibility testing is critical for reaching a larger audience. It involves evaluating how a website appears in several browsers such as Chrome, Firefox, Safari, and Internet Explorer.
As the number of mobile users grows, interoperability with mobile platforms becomes increasingly important.
Before
Before Rails 8, browser compatibility was detected using the browser gem
gem "browser"
To detect whether a browser can be considered as modern or not, we create a method that abstracts our versioning constraints.
def modern_browser?(browser)
[
browser.chrome?(">= 65"),
browser.safari?(">= 10"),
browser.firefox?(">= 52"),
browser.ie?(">= 11") && !browser.compatibility_view?,
browser.edge?(">= 15"),
browser.opera?(">= 50"),
browser.facebook?
&& browser.safari_webapp_mode?
&& browser.webkit_full_version.to_i >= 602
].any?
end
After
Rails 8 will introduce allow_browser to help us specify the minimum browser versions required for the application
We can set the allow_browser to our ApplicationController as below to restrict support to browsers that natively supports webp images, web push, badges, import maps, CSS nesting + :has
class ApplicationController < ActionController::Base
allow_browser versions: :modern
end
We can also customize the allow_browser method to specify browser versions as the example below
class ApplicationController < ActionController::Base
allow_browser versions: { safari: 16.4, firefox: 121, ie: false }
end
In the above method, allow_browser will allow all versions of Chrome and Opera and only safari above 16.4+ and Firefox 121+ but no versions of “Internet explorer” (ie) is allowed
If the application does not support Safari, then we can disable it by setting safari as false
class ApplicationController < ActionController::Base
allow_browser versions: { safari: false }
end
A browser that is blocked will by default be served the file in public/426.html with a HTTP status code of 426 Upgrade Required.
Share this post!
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK