【问题标题】:Serving a Single Page App using Phoenix Framework causes two get requests使用 Phoenix 框架为单页应用程序提供服务会导致两个 get 请求
【发布时间】:2018-08-18 08:34:15
【问题描述】:

我正在尝试让 phoenix 应用程序服务于 React SPA,并遵循以下建议:When accessing front-end SPA-links from browser, back-end fires finding no route

在遵循上述解决方案之前,我已经成功设置了它,所以我知道它在过去是有效的。

这是我路由器的相关位:

pipeline :browser do
  plug(:accepts, ["html"])
end

...
scope "/" do
  pipe_through(:browser)
  get("/*anything", TexWeb.StaticController, :index)
end

我的第一个客户端路由是/start。当我去那里时,我发现我在日志中收到了两个 get 请求:

Generated tex app
[info] Running TexWeb.Endpoint with Cowboy using http://0.0.0.0:4000
[info] GET /start
[debug] Processing with TexWeb.StaticController.index/2
  Parameters: %{"anything" => ["start"]}
  Pipelines: [:browser]
[info] Sent 200 in 141µs
[info] GET /static/js/main.8e39f2e0.js
[debug] Processing with TexWeb.StaticController.index/2
  Parameters: %{"anything" => ["static", "js", "main.8e39f2e0.js"]}
Pipelines: [:browser]
[info] Sent 200 in 183µs

我已将完全相同的 SPA 代码复制到另一个 phoenix 1.3 实例,它成功加载了 start 路由。有趣的是,这个日志看起来像:

[info] GET /start
[debug] Processing with TexappWeb.StaticController.index/2
  Parameters: %{"anything" => ["start"]}
  Pipelines: [:browser]
[info] Sent 200 in 17ms
[info] GET /
[debug] Processing with TexappWeb.StaticController.index/2
  Parameters: %{"anything" => []}
  Pipelines: [:browser]
[info] Sent 200 in 129µs

我很确定这条线:

Parameters: %{"anything" => ["static", "js", "main.8e39f2e0.js"]}

是我的问题的原因。

但是我从哪里开始调试呢?

【问题讨论】:

    标签: single-page-application phoenix-framework


    【解决方案1】:

    忘记在endpoint.ex中更改这一行

    发件人:

    plug(Plug.Static, at: "/", from: :tex, gzip: false, only: ~w(css fonts images js favicon.ico robots.txt))
    

    收件人:

    plug(Plug.Static, at: "/", from: :tex, gzip: false)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      • 2011-11-21
      • 2016-04-05
      • 1970-01-01
      • 2021-04-07
      • 1970-01-01
      相关资源
      最近更新 更多