Skip to content

Add Spring.after_environment_load hook for post-Rails preloading#1

Closed
jahfer wants to merge 1 commit intomainfrom
add-after-environment-load-hook
Closed

Add Spring.after_environment_load hook for post-Rails preloading#1
jahfer wants to merge 1 commit intomainfrom
add-after-environment-load-hook

Conversation

@jahfer
Copy link
Copy Markdown

@jahfer jahfer commented Apr 30, 2026

Summary

Adds a Spring.after_environment_load hook that runs immediately after the Rails application environment is loaded in the Spring server process, but before GC.compact/Process.warmup and before the server enters its wait loop.

This hook enables applications to preload expensive test infrastructure (e.g. test helpers, fixture caches, datastore connections) into the Spring server so that forked test workers inherit that work via copy-on-write instead of paying it on every invocation.

API

# config/spring.rb
Spring.after_environment_load do
  require "platform/testing/test_helper"
end

The block receives no arguments and runs once per server boot. It is intentionally not called when Spring is disabled or when the environment is reloaded.

Motivation

In Shopify's monorepo, bin/test pays ~2.4s of require cost for platform/testing/test_helper on every test run even with Spring hot. By preloading this file into the Spring server, forked workers inherit the already-required code via CoW, reducing time-to-first-test significantly for inner-loop development.

Implementation

  • Spring::Configuration gains an @after_environment_load array and after_environment_load registration method (same pattern as after_fork).
  • Spring::Application#load_application calls the callbacks after require app_env returns.

@jahfer jahfer closed this Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant