Rails 8.0 introduces a powerful feature called allow_browser
, which allows developers to specify minimum browser versions for their applications. This brings several advantages worth considering.
By enforcing minimum browser versions, you mitigate unaddressed security vulnerabilities, enhance operational efficiency, and ensure seamless compatibility with contemporary web functionalities. This approach safeguards user data with up-to-date security patches, optimizes performance, and promotes a unified user experience across your application. Moreover, specifying a minimum browser version empowers developers to prioritize modern technologies, simplifying development workflows and minimizing ongoing maintenance tasks.
Implementing allow_browser
in Rails 8.0
With Rails 8.0, setting minimum browser versions is straightforward using the allow_browser
method in your ApplicationController
. Here’s how you can implement it:
Fundamental Setup
To specify the lowest supported browser versions, include the allow_browser
method within your ApplicationController
:
1
2
3
class ApplicationController < ActionController::Base
allow_browser versions: :modern
end
Using the :modern
configuration ensures compatibility only with browsers that have built-in support for features such as webp images, web push notifications, badges, import maps, CSS nesting, and the CSS :has
selector.
Custom Configuration
If you prefer to specify the minimum versions for individual browsers, you can customize it as follows:
1
2
3
class ApplicationController < ActionController::Base
allow_browser versions: { edge: 88, chrome: 90, opera: 75 }
end
This setup permits all versions of Safari and Firefox but restricts support to Edge 88+, Chrome 90+, and Opera 75+.
Disabling Specific Browsers
You can also disable support for certain browsers if needed. For example, to disable Firefox:
1
2
3
class ApplicationController < ActionController::Base
allow_browser versions: { safari: 20, firefox: false, ie: 9 }
end
Adding allow_browser
to the ApplicationController
applies this configuration globally to all actions. However, you can restrict it to specific actions using the only
or except
options.
Restricting to Specific Actions
To apply this configuration to specific controller actions, use:
1
2
3
class ChatController < ApplicationController
allow_browser versions: { chrome: 85, opera: false, ie: 9 }, only: :show
end
In this example, the show
action of the ChatController
checks that the Chrome version is at least 85 and the Opera version is at least 9. The show
action does not support Opera.
Custom Page for Unsupported Browsers
Unsupported browsers will be directed to a custom page (426.html) explaining the need to upgrade. The HTTP status code will be 426 Upgrade Required.