Skip to content

Commit cdc7f58

Browse files
committed
Rework how we get the hash for a missing JS file
We had `assets/index-foo.js` in the dynamic config (read from the manifest), which needs to be absolute/rooted. Instead of reading the manifest, we can just grab the src of the JS module to get the file to HEAD.
1 parent 52146dd commit cdc7f58

File tree

4 files changed

+2
-26
lines changed

4 files changed

+2
-26
lines changed

adminapp/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<script type="text/javascript" data-csp="ok">
2828
// See commit this code was added in for an explanation.
2929
(function() {
30-
const hashedIndexJs = (window.sumaDynamicEnv || {}).VITE_HASHED_INDEX_JS;
30+
const hashedIndexJs = (document.querySelector('script[src*="assets/index"]') || {}).src
3131
if (!hashedIndexJs) {
3232
return;
3333
}

lib/rack/dynamic_config_writer.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,6 @@ def emplace(keys_and_values)
4848

4949
protected def prepare
5050
return if File.exist?(@index_html_backup)
51-
52-
# Parse the manifest so we can get the hashed index JS path for further index.html templating.
53-
begin
54-
File.open(Pathname.new(@index_html_path).dirname + ".vite" + "manifest.json") do |f|
55-
@manifest = Yajl::Parser.parse(f)
56-
if (hashed_js = @manifest.dig("index.html", "file"))
57-
@additional_kvps["VITE_HASHED_INDEX_JS"] = hashed_js
58-
end
59-
end
60-
rescue Errno::ENOENT
61-
# Ignore missing manifest
62-
end
63-
6451
FileUtils.move(@index_html_path, @index_html_backup)
6552
end
6653

spec/rack/dynamic_config_writer_spec.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
require "rack/dynamic_config_writer"
44

5-
# rubocop:disable Layout/LineLength
65
RSpec.describe Rack::DynamicConfigWriter do
76
include_context "uses temp dir"
87

@@ -64,15 +63,6 @@
6463
dcw.emplace({})
6564
expect(File.read(index)).to eq("<html><head><script>globals.x={}</script></head><body></body></html>")
6665
end
67-
68-
it "includes the build JS asset file path from the manifest" do
69-
File.write(index, "")
70-
File.write(manifest, {"index.html" => {"file" => "assets/index-C5Z8u35m.js"}}.to_json)
71-
72-
dcw = described_class.new(index, global_assign: "globals.x")
73-
dcw.emplace({})
74-
expect(File.read(index)).to eq("<html><head><script>globals.x={\"VITE_HASHED_INDEX_JS\":\"assets/index-C5Z8u35m.js\"}</script></head><body></body></html>")
75-
end
7666
end
7767

7868
describe "pick_env" do
@@ -86,4 +76,3 @@
8676
end
8777
end
8878
end
89-
# rubocop:enable Layout/LineLength

webapp/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
<script type="text/javascript" data-csp="ok">
5252
// See commit this code was added in for an explanation.
5353
(function() {
54-
const hashedIndexJs = (window.sumaDynamicEnv || {}).VITE_HASHED_INDEX_JS;
54+
const hashedIndexJs = (document.querySelector('script[src*="assets/index"]') || {}).src
5555
if (!hashedIndexJs) {
5656
return;
5757
}

0 commit comments

Comments
 (0)