How the dashboard's assets are served

Copy Markdown View Source

The dashboard needs two files from this package, dashboard.js and dashboard.css. Its layout always requests them from /__bb_assets__/ at the root of your site, wherever the dashboard itself is mounted.

Why they're served from the endpoint

bb_dashboard/2 does generate an asset route, but under the dashboard's own path: a dashboard mounted at /robot gets /robot/__bb_assets__/:file. The layout never requests that URL, so the route only matches when the dashboard is mounted at /.

Even then, the route sits behind whatever pipeline the dashboard's scope uses, usually :browser. Its protect_from_forgery plug refuses to serve JavaScript in answer to a <script> tag's request, as a guard against cross-site script inclusion, so dashboard.js fails to load.

Serving the assets with Plug.Static in the endpoint avoids both problems. The endpoint answers /__bb_assets__/ before the request reaches the router, so neither the dashboard's path nor its pipeline matters:

plug Plug.Static,
  at: "/__bb_assets__",
  from: {:bb_liveview, "priv/static"},
  gzip: false

mix igniter.install bb_liveview adds this plug, before your other Plug.Static.

Why not BB.LiveView.Plugs.Static

BB.LiveView.Plugs.Static serves the generated asset route. It takes the file name from that route's :file parameter, so it only works inside the router. Plugged into an endpoint, it finds no file name and answers every request with a 404.