Skip to content

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

DEV: Slightly defer loading Discourse stylesheets (#17078)

Joe2f66eb59c20a
diff --git a/app/assets/javascripts/discourse/app/index.html b/app/assets/javascripts/discourse/app/index.html
index 0dde7cb0ab4..4a5295d14ea 100644
--- a/app/assets/javascripts/discourse/app/index.html
+++ b/app/assets/javascripts/discourse/app/index.html
@@ -20,6 +20,13 @@
{{content-for "head"}}
</head>
<body>
+ <discourse-assets>
+ <discourse-assets-stylesheets>
+ <bootstrap-content key="discourse-stylesheets">
+ {{content-for "discourse-stylesheets"}}
+ </discourse-assets-stylesheets>
+ </discourse-assets>
+
<bootstrap-content key="body">
{{content-for "body"}}
diff --git a/app/assets/javascripts/discourse/lib/bootstrap-json/index.js b/app/assets/javascripts/discourse/lib/bootstrap-json/index.js
index 46c891ecc6a..98fc1ed882b 100644
--- a/app/assets/javascripts/discourse/lib/bootstrap-json/index.js
+++ b/app/assets/javascripts/discourse/lib/bootstrap-json/index.js
@@ -64,26 +64,6 @@ function head(buffer, bootstrap, headers, baseURL) {
});
buffer.push(`<meta id="data-discourse-setup"${setupData} />`);
- (bootstrap.stylesheets || []).forEach((s) => {
- let attrs = [];
- if (s.media) {
- attrs.push(`media="${s.media}"`);
- }
- if (s.target) {
- attrs.push(`data-target="${s.target}"`);
- }
- if (s.theme_id) {
- attrs.push(`data-theme-id="${s.theme_id}"`);
- }
- if (s.class) {
- attrs.push(`class="${s.class}"`);
- }
- let link = `<link rel="stylesheet" type="text/css" href="${
- s.href
- }" ${attrs.join(" ")}>`;
- buffer.push(link);
- });
-
if (bootstrap.preloaded.currentUser) {
const user = JSON.parse(bootstrap.preloaded.currentUser);
let { admin, staff } = user;
@@ -119,6 +99,28 @@ function beforeScriptLoad(buffer, bootstrap) {
);
}
+function discourseStylesheets(buffer, bootstrap) {
+ (bootstrap.stylesheets || []).forEach((s) => {
+ let attrs = [];
+ if (s.media) {
+ attrs.push(`media="${s.media}"`);
+ }
+ if (s.target) {
+ attrs.push(`data-target="${s.target}"`);
+ }
+ if (s.theme_id) {
+ attrs.push(`data-theme-id="${s.theme_id}"`);
+ }
+ if (s.class) {
+ attrs.push(`class="${s.class}"`);
+ }
+ let link = `<link rel="stylesheet" type="text/css" href="${
+ s.href
+ }" ${attrs.join(" ")}>`;
+ buffer.push(link);
+ });
+}
+
function body(buffer, bootstrap) {
buffer.push(bootstrap.theme_html.header);
buffer.push(bootstrap.html.header);
@@ -162,6 +164,7 @@ const BUILDERS = {
"before-script-load": beforeScriptLoad,
head,
body,
+ "discourse-stylesheets": discourseStylesheets,
"hidden-login-form": hiddenLoginForm,
preloaded,
"body-footer": bodyFooter,
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index 0ab67a126c0..3c6f20cbad4 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -9,8 +9,6 @@
<%= render partial: "layouts/head" %>
<%= discourse_csrf_tags %>
- <%= render partial: "common/discourse_stylesheet" %>
-
<%- if SiteSetting.enable_escaped_fragments? %>
<meta name="fragment" content="!">
<%- end %>
@@ -73,6 +71,12 @@
</head>
<body class="<%= body_classes %>">
+ <discourse-assets>
+ <discourse-assets-stylesheets>
+ <%= render partial: "common/discourse_stylesheet" %>
+ </discourse-assets-stylesheets>
+ </discourse-assets>
+
<%- if allow_plugins? %>
<%= build_plugin_html 'server:after-body-open' %>
<%- end -%>