Elixir 如何在Phoenix中动态生成和提供sitemap.xml?
我有一个带有sitemap.xml的应用程序。我需要每天动态修改一次。我知道如何为它生成内容。但是,我如何用新生成的sitempa.xml替换(!)我当前的sitempa.xml,或者更确切地说替换它的内容呢? 如果它是一个rails应用程序,我只会替换它的内容,它会工作,但对于phoenix它是不同的 我提供sitemap.xml的代码是标准代码:Elixir 如何在Phoenix中动态生成和提供sitemap.xml?,elixir,phoenix-framework,Elixir,Phoenix Framework,我有一个带有sitemap.xml的应用程序。我需要每天动态修改一次。我知道如何为它生成内容。但是,我如何用新生成的sitempa.xml替换(!)我当前的sitempa.xml,或者更确切地说替换它的内容呢? 如果它是一个rails应用程序,我只会替换它的内容,它会工作,但对于phoenix它是不同的 我提供sitemap.xml的代码是标准代码: defmodule MyApp.Endpoint do # ................ # Serve at "/" the st
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”创建一个特殊的路由/操作并在控制器中提供服务?我会从控制器/操作提供服务,并将该值缓存在内存中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
的值,您可以直接使用代理或ETS表或使用类似或的库来实现这一点。我会从控制器/操作提供它,并将值缓存在内存中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
的值,您可以直接使用代理或ETS表或使用像或之类的库来实现这一点。干净且简单。建议使用数千页的文件。干净简单。建议使用数千页的文件。