feat: add shop storefront with optimized theme CSS
- Create LoadTheme plug for loading theme settings - Add shop layout with inline CSS injection (~400 bytes vs 17KB full) - Create ShopLive.Home at / using shared ShopComponents - Wire up CSS cache invalidation when theme settings change - Replace Phoenix welcome page with themed shop home page The shop layout injects only the active theme CSS variables inline, achieving a 97% reduction compared to the full variants file used by the theme editor. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
39
lib/simpleshop_theme_web/plugs/load_theme.ex
Normal file
39
lib/simpleshop_theme_web/plugs/load_theme.ex
Normal file
@@ -0,0 +1,39 @@
|
||||
defmodule SimpleshopThemeWeb.Plugs.LoadTheme do
|
||||
@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
|
||||
|
||||
alias SimpleshopTheme.Settings
|
||||
alias SimpleshopTheme.Theme.{CSSGenerator, CSSCache}
|
||||
|
||||
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()
|
||||
css = CSSGenerator.generate(settings)
|
||||
CSSCache.put(css)
|
||||
{settings, css}
|
||||
end
|
||||
|
||||
conn
|
||||
|> assign(:theme_settings, theme_settings)
|
||||
|> assign(:generated_css, generated_css)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user