Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### Unreleased

* enhancements
* Load the routes on demand when `Devise.mappings` is read while the application is initializing, and stop reloading all routes before eager loading by default: `config.reload_routes` now defaults to `false`. Right after eager loading, Rails precomputes the action methods of every controller, and that is far slower when the routes' url helpers are already in each controller's ancestors; Rails 8 also loaded the routes a second time afterwards. Applications with many controllers and routes boot noticeably faster. Set `config.reload_routes = true` to keep the previous behavior.

### 5.0.4 - 2026-05-08

* security fixes
Expand Down
40 changes: 31 additions & 9 deletions lib/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,25 +263,47 @@ module Test
mattr_accessor :clean_up_csrf_token_on_authentication
@@clean_up_csrf_token_on_authentication = true

# When false, Devise will not attempt to reload routes on eager load.
# This can reduce the time taken to boot the app but if your application
# requires the Devise mappings to be loaded during boot time the application
# won't boot properly.
# When true, Devise reloads the application routes before eager loading, so
# that the mappings declared by +devise_for+ exist while the application code
# is loaded. This is not needed since +Devise.mappings+ loads the routes on
# demand, and it slows down booting: right after eager loading Rails computes
# the action methods of every controller, which is far slower once the routes'
# url helpers are in each controller's ancestors, and Rails 8 loads the routes
# a second time afterwards. Defaults to false.
mattr_accessor :reload_routes
@@reload_routes = true
@@reload_routes = false

# PRIVATE CONFIGURATION

# Store scopes mappings.
@@mappings = {}
@@routes_loaded_while_initializing = false
def self.mappings
# Starting from Rails 8.0, routes are lazy-loaded by default in test and development environments.
# However, Devise's mappings are built during the routes loading phase.
# To ensure it works correctly, we need to load the routes first before accessing @@mappings.
Rails.application.try(:reload_routes_unless_loaded)
# Devise's mappings are built while the routes load, so the routes must be loaded before
# the mappings are read. Starting from Rails 8.0, routes are lazy-loaded by default in test
# and development environments, and when eager loading Rails loads the application code
# before the routes.
load_routes_unless_loaded
@@mappings
end

# Loads the application routes if they have not been loaded yet.
#
# Once the application is initialized, Rails (8.0+) loads lazy routes on demand. While it is
# still initializing (eager loading), routes are loaded here only if the mappings are still
# empty, once, as `reload_routes = true` does unconditionally before eager loading.
def self.load_routes_unless_loaded # :nodoc:
app = Rails.application
return unless app

if app.initialized?
app.try(:reload_routes_unless_loaded)
elsif @@mappings.empty? && !@@routes_loaded_while_initializing
@@routes_loaded_while_initializing = true
app.reload_routes!
end
end

# OmniAuth configurations.
mattr_reader :omniauth_configs
@@omniauth_configs = {}
Expand Down
12 changes: 7 additions & 5 deletions lib/generators/templates/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@
# from the server. You can disable this option at your own risk.
# config.clean_up_csrf_token_on_authentication = true

# When false, Devise will not attempt to reload routes on eager load.
# This can reduce the time taken to boot the app but if your application
# requires the Devise mappings to be loaded during boot time the application
# won't boot properly.
# config.reload_routes = true
# When true, Devise reloads the application routes before eager loading, so
# that the mappings declared by `devise_for` exist while the application code
# is loaded. This is not needed since `Devise.mappings` loads the routes on
# demand, and it slows down booting: right after eager loading Rails computes
# the action methods of every controller, which is far slower once the routes'
# url helpers are in each controller's ancestors.
# config.reload_routes = false

# ==> Configuration for :database_authenticatable
# For bcrypt, this is the cost for hashing the password and defaults to 12. If
Expand Down
32 changes: 32 additions & 0 deletions test/devise_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ def self.yield_and_restore
ensure
@@warden_config, @@warden_config_blocks = c, b
end

def self.swap_mappings(mappings)
m, loaded = @@mappings, @@routes_loaded_while_initializing
@@mappings, @@routes_loaded_while_initializing = mappings, false
yield
ensure
@@mappings, @@routes_loaded_while_initializing = m, loaded
end
end

class DeviseTest < ActiveSupport::TestCase
Expand All @@ -24,6 +32,30 @@ class DeviseTest < ActiveSupport::TestCase
assert_not_equal ::BCrypt::Password.new(hash), hash
end

test 'mappings load the routes once while the application is initializing' do
Devise.swap_mappings({}) do
Rails.application.stubs(:initialized?).returns(false)
Rails.application.expects(:reload_routes!).once

Devise.mappings
Devise.mappings
end
end

test 'mappings do not load the routes while initializing when they are already declared' do
Devise.mappings # make sure the routes, and therefore the mappings, are loaded
Rails.application.stubs(:initialized?).returns(false)
Rails.application.expects(:reload_routes!).never

assert_includes Devise.mappings.keys, :user
end

test 'mappings load lazy routes once the application is initialized' do
Rails.application.expects(:reload_routes_unless_loaded).once

Devise.mappings
end

test 'model options can be configured through Devise' do
swap Devise, allow_unconfirmed_access_for: 113, pepper: "foo" do
assert_equal 113, Devise.allow_unconfirmed_access_for
Expand Down
Loading