2026-02-18 21:23:15 +00:00
|
|
|
defmodule BerrypodWeb.Plugs.LoadTheme do
|
2026-01-17 16:19:35 +00:00
|
|
|
@moduledoc """
|
|
|
|
|
Plug that loads theme settings and generated CSS for public shop pages.
|
|
|
|
|
|
|
|
|
|
This plug:
|
|
|
|
|
1. Checks the ETS cache for pre-generated CSS
|
|
|
|
|
2. Falls back to generating CSS from theme settings on cache miss
|
|
|
|
|
3. Assigns both `theme_settings` and `generated_css` to the connection
|
|
|
|
|
|
|
|
|
|
The generated CSS contains only the active theme values (not all variants),
|
|
|
|
|
making it much smaller than the full theme-layer2-attributes.css file used
|
|
|
|
|
by the theme editor for live preview switching.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import Plug.Conn
|
|
|
|
|
|
2026-02-18 21:23:15 +00:00
|
|
|
alias Berrypod.Settings
|
|
|
|
|
alias Berrypod.Theme.{CSSGenerator, CSSCache}
|
2026-01-17 16:19:35 +00:00
|
|
|
|
|
|
|
|
def init(opts), do: opts
|
|
|
|
|
|
|
|
|
|
def call(conn, _opts) do
|
|
|
|
|
{theme_settings, generated_css} =
|
|
|
|
|
case CSSCache.get() do
|
|
|
|
|
{:ok, css} ->
|
|
|
|
|
{Settings.get_theme_settings(), css}
|
|
|
|
|
|
|
|
|
|
:miss ->
|
|
|
|
|
settings = Settings.get_theme_settings()
|
2026-02-20 18:39:41 +00:00
|
|
|
css = CSSGenerator.generate(settings, &BerrypodWeb.Endpoint.static_path/1)
|
2026-01-17 16:19:35 +00:00
|
|
|
CSSCache.put(css)
|
|
|
|
|
{settings, css}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
conn
|
|
|
|
|
|> assign(:theme_settings, theme_settings)
|
|
|
|
|
|> assign(:generated_css, generated_css)
|
|
|
|
|
end
|
|
|
|
|
end
|