2017-09-29 10 views
1

私はsitemap.xmlを持つアプリケーションを持っています。私は1日1回動的に変更する必要があります。私はそれのためのコンテンツを生成する方法を知っています。しかし、私は実際に私の現在のsitempa.xmlを新しい生成されたsitempa.xml(あるいはその内容)に置き換えることができますか? レールアプリの場合は、内容を置き換えてもうまくいくが、phoenixの場合は違う。フェニックスでsitemap.xmlを動的に生成して提供するにはどうすればよいですか?

sitemap.xmlとを提供するための私のコードは標準的なものである:

defmodule MyApp.Endpoint do 
    # ................ 

    # Serve at "/" the static files from "priv/static" directory. 
    # 
    # You should set gzip to true if you are running phoenix.digest 
    # when deploying your static files in production. 
    plug Plug.Static, 
    at: "/", from: :my_app, gzip: false, 
    only: ~w(css fonts images js favicon.ico robots.txt sitemap.xml) 

私は「sitemap.xmlと」のための特別なルート/アクションを作成し、コントローラにそれを提供するべきでしょうか?

答えて

3

私はコントローラー/アクションからそれを提供し、その値をメモリーに24時間キャッシュします。

# Router 
get "/sitemap.xml", SitemapController, :index 

# Controller 
defmodule MyApp.SitemapController do 
    use MyApp.Web, :controller 

    def index(conn, _params) do 
    xml = ... # generate the sitemap/fetch from cache 
    conn 
    |> put_resp_header("content-type", "application/xml") 
    |> send_resp(200, xml) 
    end 
end 

xmlの値をキャッシュするには、AgentまたはETS表と直接これを行うか、または cachex con_cacheのようなライブラリを使用することができます。

関連する問題