Skip to content

Commit

Permalink
Revert "chore: remove route /css_config (#660)"
Browse files Browse the repository at this point in the history
This reverts commit f91a3b3.
  • Loading branch information
leandrocp committed Nov 20, 2024
1 parent 5ac6ee1 commit 4141c1b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/beacon/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ defmodule Beacon.Router do
live_session session_name, session_opts do
get "/__beacon_media__/:file_name", Beacon.Web.MediaLibraryController, :show, assigns: %{site: opts[:site]}

# TODO: css_config-:md5 caching
get "/__beacon_assets__/css_config", Beacon.Web.AssetsController, :css_config, assigns: %{site: opts[:site]}

get "/__beacon_assets__/css-:md5", Beacon.Web.AssetsController, :css, assigns: %{site: opts[:site]}
get "/__beacon_assets__/js-:md5", Beacon.Web.AssetsController, :js, assigns: %{site: opts[:site]}

Expand Down
10 changes: 10 additions & 0 deletions lib/beacon/runtime_css.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ defmodule Beacon.RuntimeCSS do
Beacon.Config.fetch!(site).css_compiler.config(site)
end

@doc """
Returns the URL to fetch the CSS config used to generate the site stylesheet.
"""
@spec asset_url(Site.t()) :: String.t()
def asset_url(site) do
%{endpoint: endpoint, router: router} = Beacon.Config.fetch!(site)
prefix = router.__beacon_scoped_prefix_for_site__(site)
endpoint.url() <> Beacon.Router.sanitize_path("#{prefix}/__beacon_assets__/css_config")
end

@doc false
def compile(site) when is_atom(site) do
Beacon.Config.fetch!(site).css_compiler.compile(site)
Expand Down
22 changes: 21 additions & 1 deletion lib/beacon/web/controllers/assets_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Beacon.Web.AssetsController do

import Plug.Conn

def init(asset) when asset in [:css, :js], do: asset
def init(asset) when asset in [:css_config, :css, :js], do: asset

def call(%{assigns: %{site: site}} = conn, asset) when asset in [:css, :js] do
{content, content_type} = content_and_type(site, asset)
Expand All @@ -22,11 +22,31 @@ defmodule Beacon.Web.AssetsController do
|> halt()
end

# TODO: encoding (compress) and caching
def call(%{assigns: %{site: site}} = conn, :css_config) do
{content, content_type} = content_and_type(site, :css_config)

# The static files are served for sites,
# and we need to disable csrf protection because
# serving script files is forbidden by the CSRFProtection plug.
conn = put_private(conn, :plug_skip_csrf_protection, true)

conn
|> put_resp_header("content-type", content_type)
|> put_resp_header("access-control-allow-origin", "*")
|> send_resp(200, content)
|> halt()
end

defp content_and_type(site, :css) do
{Beacon.RuntimeCSS.fetch(site), "text/css"}
end

defp content_and_type(_site, :js) do
{Beacon.RuntimeJS.fetch(), "text/javascript"}
end

defp content_and_type(site, :css_config) do
{Beacon.RuntimeCSS.config(site), "text/javascript"}
end
end

0 comments on commit 4141c1b

Please sign in to comment.