Skip to content

A platform for community discussion. Free, open, simple.

DEV: Deprecate inline script tags for templates & initializers (#32773)

David Taylor5cf6ebf70c1d
diff --git a/app/models/theme.rb b/app/models/theme.rb
index 319ffe2346e..af0072b3a2b 100644
--- a/app/models/theme.rb
+++ b/app/models/theme.rb
@@ -6,7 +6,7 @@ require "json_schemer"
class Theme < ActiveRecord::Base
include GlobalPath
- BASE_COMPILER_VERSION = 87
+ BASE_COMPILER_VERSION = 88
class SettingsMigrationError < StandardError
end
diff --git a/app/models/theme_field.rb b/app/models/theme_field.rb
index 922336d73f4..08781888236 100644
--- a/app/models/theme_field.rb
+++ b/app/models/theme_field.rb
@@ -123,6 +123,7 @@ class ThemeField < ActiveRecord::Base
errors << I18n.t("themes.errors.optimized_link") if contains_optimized_link?(html)
js_compiler = ThemeJavascriptCompiler.new(theme_id, self.theme.name)
+ deprecated_template_names = []
doc = Nokogiri::HTML5.fragment(html)
@@ -144,6 +145,7 @@ class ThemeField < ActiveRecord::Base
"discourse/templates/#{name.delete_prefix("/")}",
hbs_template,
)
+ deprecated_template_names << name
end
rescue ThemeJavascriptCompiler::CompileError => ex
js_compiler.append_js_error("discourse/templates/#{name}", ex.message)
@@ -153,6 +155,28 @@ class ThemeField < ActiveRecord::Base
node.remove
end
+ if deprecated_template_names.present?
+ js = <<~JS
+ import deprecated from "discourse/lib/deprecated";
+
+ export default {
+ initialize(){
+ const names = #{deprecated_template_names.to_json};
+ names.forEach((name) => {
+ deprecated(
+ `[${name}] adding templates to a theme using <script type='text/x-handlebars'> is deprecated. Move to dedicated .hbs or .gjs files.`,
+ {
+ id: "discourse.script-tag-hbs",
+ url: "https://meta.discourse.org/t/366482",
+ }
+ )
+ });
+ }
+ }
+ JS
+ js_compiler.append_module(js, "discourse/initializers/script-tag-hbs-deprecations", "js")
+ end
+
doc
.css('script[type="text/discourse-plugin"]')
.each_with_index do |node, index|
@@ -165,12 +189,20 @@ class ThemeField < ActiveRecord::Base
begin
js = <<~JS
import { withPluginApi } from "discourse/lib/plugin-api";
+ import deprecated from "discourse/lib/deprecated";
export default {
name: #{initializer_name.inspect},
after: "inject-objects",
initialize() {
+ deprecated(
+ "Adding JS code using <script type='text/x-handlebars'> is deprecated. Move this code to a dedicated JavaScript file.",
+ {
+ id: "discourse.script-tag-discourse-plugin",
+ url: "https://meta.discourse.org/t/366482",
+ }
+ )
withPluginApi(#{version.inspect}, (api) => {
#{node.inner_html}
});