Rails 8 adds allow_browser to set minimum browser version
Nishant Goyal
Co-Founder of Plural Code Technologies | I assist you in creating customised software development solutions | 200+ Happy Clients Globally | Full Stack Development | 10+ Years of Mobile & Web App Development Experience
?? Hey LinkedIn! ?? Let's talk about something that's crucial for every website - browser compatibility! ?? You know, making sure your site looks and works as smooth as butter across all those different browsers! ??
?? Fun fact: Every browser has its own way of interpreting code, so testing for compatibility is like making sure your jokes land with every crowd! ??
?? Compatibility testing means we're checking out how our website looks on all the big players - Chrome, Firefox, Safari, even Internet Explorer! ?? We're leaving no browser behind! ??
?? And hey, with more and more folks surfing the web on their phones, compatibility with mobile platforms is super important too! ?? Let's make sure our sites are sleek and shiny on every screen size! ????
Before Rails 8
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
?? Rails 8 is bringing in some cool new features, including allow_browser! ?? Say goodbye to old-school browser struggles and hello to smoother sailing! ??
?? Setting allow_browser in your ApplicationController is as easy as pie! ?? Just a few lines of code and voila! You're restricting support to browsers that can handle all the latest and greatest web features! ??
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
With 'allow_browser', we're giving a warm welcome to Chrome and Opera lovers, while keeping Safari above 16.4+ and Firefox 121+ in the loop! ?? Sorry, Internet Explorer, you're not invited to this party! ??
?? But hey, if Safari isn't your cup of tea, no worries! ?? Just set 'safari' to false and voila! Problem solved! ??
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.