FEATURE: Add stylesheets to bootstrap.json endpoint

FEATURE: Add stylesheets to bootstrap.json endpoint

This allows API consumers (such as Ember CLI) to dynamically get a list of styles to embed.

diff --git a/app/controllers/bootstrap_controller.rb b/app/controllers/bootstrap_controller.rb
index dca7007..e9e0e82 100644
--- a/app/controllers/bootstrap_controller.rb
+++ b/app/controllers/bootstrap_controller.rb
@@ -15,15 +15,60 @@ class BootstrapController < ApplicationController
       preload_current_user_data
     end
 
+    @stylesheets = []
+    add_scheme(scheme_id, 'all')
+    add_scheme(dark_scheme_id, '(prefers-color-scheme: dark)')
+    if rtl?
+      add_style(mobile_view? ? :mobile_rtl : :desktop_rtl)
+    else
+      add_style(mobile_view? ? :mobile : :desktop)
+    end
+    add_style(:admin) if staff?
+    Discourse.find_plugin_css_assets(
+      include_official: allow_plugins?,
+      include_unofficial: allow_third_party_plugins?,
+      mobile_view: mobile_view?,
+      desktop_view: !mobile_view?,
+      request: request
+    ).each do |file|
+      add_style(file)
+    end
+    add_style(mobile_view? ? :mobile_theme : :desktop_theme) if theme_ids.present?
+
     bootstrap = {
       theme_ids: theme_ids,
       title: SiteSetting.title,
       current_homepage: current_homepage,
-      locale_script: "#{Discourse.base_url}#{locale}",
+      locale_script: locale,
+      stylesheets: @stylesheets,
       setup_data: client_side_setup_data,
       preloaded: @preloaded
     }
 
     render_json_dump(bootstrap: bootstrap)
   end
+
+private
+  def add_scheme(scheme_id, media)
+    return if scheme_id.to_i == -1
+    theme_id = theme_ids&.first
+
+    if style = Stylesheet::Manager.color_scheme_stylesheet_details(scheme_id, media, theme_id)
+      @stylesheets << { href: style[:new_href], media: media }
+    end
+  end
+
+  def add_style(target)
+    if styles = Stylesheet::Manager.stylesheet_details(target, 'all', theme_ids)
+      styles.each do |style|
+        @stylesheets << {
+          href: style[:new_href],
+          media: 'all',
+          theme_id: style[:theme_id],
+          target: style[:target]
+        }
+      end
+    end
+  end
+
 end
diff --git a/app/views/common/_discourse_stylesheet.html.erb b/app/views/common/_discourse_stylesheet.html.erb
index a0fa07f..700e28c 100644
--- a/app/views/common/_discourse_stylesheet.html.erb
+++ b/app/views/common/_discourse_stylesheet.html.erb
@@ -1,4 +1,4 @@
-<%= discourse_color_scheme_stylesheets() %>
+<%= discourse_color_scheme_stylesheets %>
 
 <%- if rtl? %>
   <%= discourse_stylesheet_link_tag(mobile_view? ? :mobile_rtl : :desktop_rtl) %>
diff --git a/spec/requests/bootstrap_controller_spec.rb b/spec/requests/bootstrap_controller_spec.rb
index 6495eb4..9b17254 100644
--- a/spec/requests/bootstrap_controller_spec.rb
+++ b/spec/requests/bootstrap_controller_spec.rb
@@ -15,6 +15,8 @@ describe BootstrapController do
     expect(bootstrap).to be_present
     expect(bootstrap['title']).to be_present
     expect(bootstrap['setup_data']['base_url']).to eq(Discourse.base_url)
+    expect(bootstrap['stylesheets']).to be_present
+
     preloaded = bootstrap['preloaded']
     expect(preloaded['site']).to be_present
     expect(preloaded['siteSettings']).to be_present

GitHub sha: 0f9a58e0

1 Like