Rails 8.0 adds allow_browser to set minimum versions for application

railsFebruary 06, 2024Dotby Alkesh Ghorpade

Setting minimum browser versions for your Rails application can ensure a better user experience and take advantage of modern web features. While it isn't always mandatory, it offers several benefits that make it a worthwhile consideration for many web applications, especially Rails applications. Here's why:

  • Security:

    Older browsers often have unpatched security vulnerabilities that could expose your application and user data to attacks. Setting minimum versions ensures users are running browsers with the latest security fixes, reducing the risk of exploits.

  • Performance:

    Newer browsers are optimized for speed and efficiency. Users on older versions might experience lag or compatibility issues due to outdated rendering engines or lack of support for modern features. Enforcing minimum versions helps maintain a consistent and performant user experience for everyone.

  • Feature Compatibility:

    Modern web technologies constantly evolve, introducing new features and capabilities. Older browsers might need help to support these features, leading to broken functionalities or inconsistent rendering. Setting minimum versions ensures your application functions as intended by leveraging the latest web standards supported by modern browsers.

  • Developer Efficiency:

    Supporting older browser versions requires additional effort and resources for testing and implementing workarounds for compatibility issues. Setting a minimum browser version allows developers to leverage modern features and technologies, streamlining the development process.

Before Rails 8.0

Before Rails 8.0, several ways to handle the minimum browser version existed. One common approach is to use a gem like browser to detect the user's browser and version.

Here are the steps to set a minimum browser version using the gem.

  1. Add the browser gem to your Gemfile.
gem "browser"

Run bundle install to install the gem.

  1. Create a helper method in ApplicationHelper.

Create a method that checks the browser version in your app/helpers/application_helper.rb or any other relevant helper file.

module ApplicationHelper
  def supported_browser?
    browser = Browser.new(request.user_agent)
    minimum_chrome_version = 50
    minimum_firefox_version = 53
    minimum_safari_version = 8

    case browser.name
    when "Chrome"
      browser.version.to_i >= minimum_chrome_version
    when "Firefox"
      browser.version.to_i >= minimum_firefox_version
    when "Safari"
      browser.version.to_i >= minimum_safari_version
    else
      true
    end
  end
end

This method detects the user's browser and version using the browser gem. You can adjust the minimum versions according to your application's requirements.

  1. Implement in check in your views or controllers:

You can use this helper method to conditionally render content or display a message in your views or controllers.

<% if supported_browser? %>
  <%= yield %>
<% else %>
  <p>Sorry, your browser is not supported. Please upgrade to a newer version.</p>
<% end %>

This example shows a message for unsupported browsers, but you can customize this according to your needs.

You can also use the useragent gem and implement the code for the minimum browser version similarly.

In Rails 8.0

Rails 8.0 adds allow_browser to set minimum versions for your application.

You need to add the allow_browser method in your ApplicationController as below:

class ApplicationController < ActionController::Base
  allow_browser versions: :modern
end

Version modern is set to restrict support to browsers natively supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.

If you want to specify the browsers and their versions, you can set them as shown:

class ApplicationController < ActionController::Base
  allow_browser versions: { safari: 20, firefox: 110, ie: 9 }
end

The above versions will allow all Chrome and Opera versions but only Safari 20+, Firefox 110+ and Internet Explorer 9+ versions.

If your application does not support Firefox, you can disable it by setting firefox: false.

class ApplicationController < ActionController::Base
  allow_browser versions: { safari: 20, firefox: false, ie: 9 }
end

Adding the allow_browser in the ApplicationController will apply to all the actions defined in your application. You can restrict this feature to specific actions by passing the only or except option.

class ChatController < ApplicationController
  allow_browser versions: { chrome: 85, opera: 100, ie: false }, only: :show
end

On top of the ApplicationController browsers check, the ChatController#show action will verify that the Chrome version should not be less than 85 and Opera's version should not be less than 100. The ChatController#show action is disabled for Internet Explorer.

Unsupported browsers will receive a custom page (426.html) explaining the need to upgrade. The HTTP status code will be 426 Upgrade Required. The below page gets rendered.

allow browser

To know more about this feature, please refer to this PR.

You can refer the below video for more details.

Closing Remark

Could your team use some help with topics like this and others covered by ShakaCode's blog and open source? We specialize in optimizing Rails applications, especially those with advanced JavaScript frontends, like React. We can also help you optimize your CI processes with lower costs and faster, more reliable tests. Scraping web data and lowering infrastructure costs are two other areas of specialization. Feel free to reach out to ShakaCode's CEO, Justin Gordon, at justin@shakacode.com or schedule an appointment to discuss how ShakaCode can help your project!
Are you looking for a software development partner who can
develop modern, high-performance web apps and sites?
See what we've doneArrow right