phenixdigital / phoenix_storybook

A pluggable storybook for your Phoenix components.
MIT License
637 stars 48 forks source link

Internal assets aren't being found even that they exist #324

Closed zoedsoupe closed 8 months ago

zoedsoupe commented 11 months ago

I have an umbrella app which has many web apps of different domains. However, I would like to extract common components and styles to a separate application called :design_system.

To correctly forward and handle different paths request I also have a :proxy_web app that forwards to the respective endpoint based on the conn.path_info pattern.

Here's the ProxyWeb main part, the router:

defmodule ProxyWeb.Router do
  @behaviour Plug

  def init(opts), do: opts

  def call(conn, _opts) do
    endpoint = forward_conn(conn.path_info)
    endpoint.call(conn, [])
  end

  defp forward_conn(["api" | _]), do: PlataformaDigitalAPI.Endpoint
  defp forward_conn(["design-system" | _]), do: DesignSystem.Endpoint
  defp forward_conn(_), do: PlataformaDigital.Endpoint
end

Thats works fine, but phoenix_storybook has a strange behaviour.

In my :design_system app I define the router as:

defmodule DesignSystem.Router do
  use Phoenix.Router, helpers: false

  import PhoenixStorybook.Router

  scope "/" do
    storybook_assets("/design-system/storybook/assets")

    live_storybook("/design-system/storybook",
      backend_module: DesignSystem.Storybook,
      assets_path: "/design-system/storybook/assets"
    )
  end
end

So now I'm able to reach /design-system/storybook/assets/css/app.css for example, in the browser!

However, when I reach to /design-system/storybook the page reloads infinitely because the conn is halted on the AssetNotFoundController of phoenix_storybook

The partial log is

[debug] compiling storybook file: typography.story.exs
[debug] Processing with PhoenixStorybook.AssetNotFoundController.asset/2
  Parameters: %{"asset" => ["css", "fonts.css"]}
  Pipelines: [:storybook_assets]
[debug] Phoenix.Router halted in :storybook_assets/2
[debug] Processing with PhoenixStorybook.AssetNotFoundController.asset/2
  Parameters: %{"asset" => ["css", "app.css"]}
  Pipelines: [:storybook_assets]
[debug] Processing with PhoenixStorybook.AssetNotFoundController.asset/2
  Parameters: %{"asset" => ["js", "app.js"]}
  Pipelines: [:storybook_assets]
[debug] Phoenix.Router halted in :storybook_assets/2
[debug] Phoenix.Router halted in :storybook_assets/2

Sample Video

https://github.com/phenixdigital/phoenix_storybook/assets/44469426/e7669a9e-4e71-415a-b11c-3a5ef8f94d6d

The complete source code can be found at https://github.com/peapescarte/pescarte-plataforma/tree/feat/design-system-app

cblavier commented 9 months ago

hey Zoey, sorry I hadn't much time lately to work on the storybook, but I'm now tackling all open issues. Is your issue still relevant? If so, I can help this week

zoedsoupe commented 9 months ago

yes, it is!

cblavier commented 9 months ago

Ok, I’ll try to reproduce your error tomorrow!

cblavier commented 9 months ago

Hey Zoey, it took me a couple of hours to dig down to the issue, but the problem was you are not providing the storybook a LiveView endpoint.

I fixed it with:

diff --git a/apps/design_system/lib/design_system/endpoint.ex b/apps/design_system/lib/design_system/endpoint.ex
index 07356ee..62a1513 100644
--- a/apps/design_system/lib/design_system/endpoint.ex
+++ b/apps/design_system/lib/design_system/endpoint.ex
@@ -12,5 +12,7 @@ defmodule DesignSystem.Endpoint do
     key: "_design_system_key",
     signing_salt: "OBcQb8Zm"

+  socket("/design-system/live", Phoenix.LiveView.Socket)
+
   plug DesignSystem.Router
 end
diff --git a/apps/design_system/lib/design_system/router.ex b/apps/design_system/lib/design_system/router.ex
index 7bcca04..f404532 100644
--- a/apps/design_system/lib/design_system/router.ex
+++ b/apps/design_system/lib/design_system/router.ex
@@ -8,7 +8,8 @@ defmodule DesignSystem.Router do

     live_storybook("/design-system/storybook",
       backend_module: DesignSystem.Storybook,
-      assets_path: "/design-system/storybook/assets"
+      assets_path: "/design-system/storybook/assets",
+      live_socket_path: "/design-system/live"
     )
   end
 end

Let me know if it's working for you!

cblavier commented 8 months ago

I'm closing, feel free to re-open if you are still stuck with this issue.