Skip to content
Merged
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog],
and this project adheres to [Semantic Versioning].

## Unreleased

### Changed

- `RailsVite.digest` is computed once outside development and test, instead of hashing the manifest file on every call (#57) ([@thomasklemm])

## rails_vite@0.3.0 / rails-vite-plugin@0.3.0 - 2026-10-11

### Added
Expand Down Expand Up @@ -128,6 +134,7 @@ and this project adheres to [Semantic Versioning].
[@cole-robertson]: https://github.com/cole-robertson
[@madogiwa0124]: https://github.com/madogiwa0124
[@janko]: https://github.com/janko
[@thomasklemm]: https://github.com/thomasklemm

[Keep a Changelog]: https://keepachangelog.com/en/1.0.0/
[Semantic Versioning]: https://semver.org/spec/v2.0.0.html
10 changes: 9 additions & 1 deletion lib/rails_vite/manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ def path_for(name)
end

def digest
Digest::MD5.file(@path).hexdigest
if Rails.env.local?
load_digest
else
@digest ||= load_digest
end
rescue Errno::ENOENT
NO_MANIFEST_DIGEST
end
Expand All @@ -47,6 +51,10 @@ def data
end
end

def load_digest
Digest::MD5.file(@path).hexdigest
end

def load_manifest
JSON.parse(File.read(@path))
rescue Errno::ENOENT
Expand Down
52 changes: 52 additions & 0 deletions test/manifest_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,58 @@ def test_digest_returns_no_manifest_when_missing
assert_equal "no-manifest", manifest.digest
end

def test_digest_is_cached_outside_local_environment
Rails.stub(:env, ActiveSupport::EnvironmentInquirer.new("production")) do
digest = @manifest.digest
File.write(@manifest_path, "{}")

assert_equal digest, @manifest.digest
refute_equal Digest::MD5.file(@manifest_path).hexdigest, @manifest.digest
end
end

def test_digest_reloads_in_local_environment
%w[development test].each do |env|
Rails.stub(:env, ActiveSupport::EnvironmentInquirer.new(env)) do
File.write(@manifest_path, JSON.generate(SAMPLE_MANIFEST))
digest = @manifest.digest
File.write(@manifest_path, "{}")

refute_equal digest, @manifest.digest
assert_equal Digest::MD5.hexdigest("{}"), @manifest.digest
end
end
end

def test_missing_digest_is_not_cached
%w[production development test].each do |env|
Rails.stub(:env, ActiveSupport::EnvironmentInquirer.new(env)) do
File.delete(@manifest_path)
manifest = RailsVite::Manifest.new(@manifest_path)
assert_equal "no-manifest", manifest.digest
File.write(@manifest_path, "{}")

assert_equal Digest::MD5.hexdigest("{}"), manifest.digest
end
end
end

def test_reset_clears_cached_digest
Rails.stub(:env, ActiveSupport::EnvironmentInquirer.new("production")) do
RailsVite.reset!
RailsVite.config.manifest_path = @manifest_path
digest = RailsVite.digest
File.write(@manifest_path, "{}")
assert_equal digest, RailsVite.digest

RailsVite.reset!
RailsVite.config.manifest_path = @manifest_path
assert_equal Digest::MD5.hexdigest("{}"), RailsVite.digest
ensure
RailsVite.reset!
end
end

def test_data_reloads_in_local_environment
# Rails.env is "test" which is local?, so manifest reloads each time
assert Rails.env.local?, "Expected test env to be local"
Expand Down
Loading