I’ve released phoenix_static, which facilitates adding static pages from external sources to a Phoenix project.
An Example
An example use case of mine is a bilingual (Italian/English) website which has interactive content, in LiveViews, but also a lot of descriptive content. Handling that content with Gettext becomes tedious very fast. The content is now hosted on an instance of PayloadCMS and is merged into the site at release time, and in development mode.
To get the project working (as explained below) I implemented a module that fetches content and returns the latest update timestamp both as GraphQL calls.
Usage
Add the library to your dependencies:
defp deps do
[
{:phoenix_static, "~> 0.4.1"}
]
end
Next, you’ll need to implement a module that implements the PhoenixStatic.Source behaviour, which is basically two functions: one that fetches all content and another that returns the last modification time of the content.
list_pages/0
returns a list of Page structs, containing the following data:
- action - the action name that should be generated in the specified Phoenix controller,
- assigns - any assigns like
:page_title
that may bee needed by your layout, - content - the HTML content of the page - which will get wrapped in the layout,
- metadata - any route metadata you want to set,
The implementation will look something like this:
defmodule MyAppWeb.Static do
@behaviour PhoenixStatic.Source
@impl true
def list_pages() do
# Fetch content from your CMS or other source
{:ok, content}
end
@impl true
def last_modified() do
# Return the last modification time of the content
{:ok, last_modified_time}
end
end
Currently, the behaviour doesn’t handle pagination of page results, so that may be a limitation of you have gigabytes of static pages!
You’ll need to make sure that last_modified/0
is as fast as possible, as it gets called on every page load in development mode!
Now that you have the Source module, you just need to hook it into your router, controller and view:
defmodule MyAppWeb.Router do
...
use PhoenixStatic.Routes, pipelines: [:browser]
...
end
defmodule MyAppWeb.MyPageController do
use MyAppWeb, :controller
use PhoenixStatic.Actions
...
end
defmodule MyAppWeb.MyPageHTML do
use MyAppWeb, :html
use PhoenixStatic.Views
...
end
And, finally, you need to configure it in your config/config.exs
file:
config :phoenix_static,
source: MyAppWeb.Static,
controller: MyAppWeb.MyPageController,
view: MyAppWeb.MyPageHTML