discourse/app/models/javascript_cache.rb
David Taylor be3d6a56ce
DEV: Introduce minification and source maps for Theme JS (#18646)
Theme javascript is now minified using Terser, just like our core/plugin JS bundles. This reduces the amount of data sent over the network.

This commit also introduces sourcemaps for theme JS. Browser developer tools will now be able show each source file separately when browsing, and also in backtraces.

For theme test JS, the sourcemap is inlined for simplicity. Network load is not a concern for tests.
2022-10-18 18:20:10 +01:00

57 lines
1.3 KiB
Ruby

# frozen_string_literal: true
class JavascriptCache < ActiveRecord::Base
belongs_to :theme_field
belongs_to :theme
validate :content_cannot_be_nil
before_save :update_digest
def url
"#{GlobalSetting.cdn_url}#{Discourse.base_path}#{path}"
end
def local_url
"#{Discourse.base_path}#{path}"
end
private
def path
"/theme-javascripts/#{digest}.js?__ws=#{Discourse.current_hostname}"
end
def update_digest
self.digest = Digest::SHA1.hexdigest(content) if content_changed?
end
def content_cannot_be_nil
errors.add(:content, :empty) if content.nil?
end
end
# == Schema Information
#
# Table name: javascript_caches
#
# id :bigint not null, primary key
# theme_field_id :bigint
# digest :string
# content :text not null
# created_at :datetime not null
# updated_at :datetime not null
# theme_id :bigint
# source_map :text
#
# Indexes
#
# index_javascript_caches_on_digest (digest)
# index_javascript_caches_on_theme_field_id (theme_field_id)
# index_javascript_caches_on_theme_id (theme_id)
#
# Foreign Keys
#
# fk_rails_... (theme_field_id => theme_fields.id) ON DELETE => cascade
# fk_rails_... (theme_id => themes.id) ON DELETE => cascade
#