Skip to content

Commit

Permalink
Add contenthash to js/css build files
Browse files Browse the repository at this point in the history
Should prevent caching issues, see #99 (comment)
  • Loading branch information
Earlopain committed Oct 20, 2023
1 parent e6b6695 commit 7d66cd7
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
40 changes: 40 additions & 0 deletions app/logical/esbuild_manifest.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module EsbuildManifest
FILE_LOCATION = Rails.public_path.join("build/manifest.json")

module_function

def [](entry)
raise StandardError, "Entrypoint '#{entry}' not found" if entrypoints[entry].blank? && !Rails.env.test?

"/#{entrypoints[entry]}"
end

def entrypoints
@entrypoints ||= parse
end

def parse
data = JSON.parse(FILE_LOCATION.read)
available_entrypoints = data["outputs"].select { |_k, v| v["entryPoint"].present? }
result = {}
available_entrypoints.each do |entrypoint_path, entrypoint_data|
relative_path = relative_to_public(entrypoint_path)
entrypoint_name = Pathname.new(entrypoint_data["entryPoint"]).basename
result[entrypoint_name.to_s] = relative_path.to_s
result[entrypoint_name.sub_ext(".css").to_s] = relative_to_public(entrypoint_data["cssBundle"]).to_s if entrypoint_data["cssBundle"]
end
result
rescue Errno::ENOENT, JSON::ParserError
{}
end

def reset_cache
@entrypoints = nil
end

def relative_to_public(input)
Rails.root.join(input).relative_path_from(Rails.public_path)
end
end
4 changes: 2 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<%= csp_meta_tag %>

<link rel="icon" type="image/png" href="/logo.png">
<%= stylesheet_link_tag "/build/application", media: "all" %>
<%= javascript_include_tag "/build/application" %>
<%= stylesheet_link_tag EsbuildManifest["application.css"], media: "all" %>
<%= javascript_include_tag EsbuildManifest["application.ts"] %>
</head>

<body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@
Listen.to(Rails.root.join("app/logical/sites/definitions"), only: /.*\.yml/) do
Sites.reset_cache
end.start

Listen.to(EsbuildManifest::FILE_LOCATION.dirname, only: /#{EsbuildManifest::FILE_LOCATION.basename}$/) do
EsbuildManifest.reset_cache
end.start
end
8 changes: 7 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ services:
environment:
<<: *common-env
RAILS_SERVER_EXEC: bin/rails server -p 9000 -b 0.0.0.0
ESBUILD_EXEC: esbuild app/typescript/application.ts --target=chrome111,firefox111,safari16 --bundle --sourcemap --outdir=public/build --loader:.png=file --watch=forever --color=true
ESBUILD_EXEC: >-
esbuild app/typescript/application.ts
--target=chrome111,firefox111,safari16
--bundle --sourcemap
--outdir=public/build --loader:.png=file
--entry-names=[name]-[hash] --metafile=public/build/manifest.json
--watch=forever --color=true
GOOD_JOB_EXEC: bundle exec good_job --queues="scraping:1;e6_iqdb:1;variant_generation:5;default:1;submission_download:5"
volumes:
- .:/app
Expand Down
26 changes: 26 additions & 0 deletions test/logical/esbuild_manifest_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require "test_helper"

class EsbuildManifestTest < ActiveSupport::TestCase
test "it correctly parses the entrypoints" do
manifest = <<~JSON
{
"outputs": {
"public/build/application-QTMXSKWO.js": {
"entryPoint": "app/typescript/application.ts",
"cssBundle": "public/build/application-D737BFOQ.css"
},
"public/build/application-D737BFOQ.css": {}
}
}
JSON
expected = {
"application.ts" => "build/application-QTMXSKWO.js",
"application.css" => "build/application-D737BFOQ.css",
}
stub_const(EsbuildManifest, :FILE_LOCATION, StringIO.new(manifest)) do
assert_equal(expected, EsbuildManifest.parse)
end
end
end

0 comments on commit 7d66cd7

Please sign in to comment.