joeyates

joeyates

phoenix_static - add static pages to Phoenix apps at compile time

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

Links

First Post!

Eiji

Eiji

First thing that came to my mind:

I think a comparison of those 2 projects should be made.

For more information see:

Where Next?

Popular in Libraries Top

tompave
Hello there, I would like to share a feature toggles library (AKA feature flags) I’ve been working on. The main package is FunWithFlags...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
blatyo
https://www.conduitframework.com/ The best overview for how things are tied together is this presentation. Modules and functions are pre...
New
riverrun
I’ve just released version 3 of Comeonin, a password hashing library. The following small changes have been made: changes to the NIF c...
New
tfwright
After working on it for a couple of months and using it in production for most of that time, today I’ve released LiveAdmin, a LiveView ba...
New
aditya7iyengar
Rummage.Ecto and Rummage.Phoenix provide ways to perform Searching, Sorting and Pagination over Ecto queries and Phoenix collections. Fo...
New
zorbash
I created Kitto a framework for dashboards inspired by Dashing. [demo] The distributed characteristics of Elixir and the low memory foo...
New
ahamez
Hi everyone, I’ve been working on this protobuf library for 3 years. We use it in the company I work for, EasyMile, to communicate with ...
New
bryanjos
Hi, I just published version 0.23.0 of Elixirscript. Most of the changes are around JavaScript interop now that Elixirscript uses the ...
New
maltoe
Hello! Came here to announce ChromicPDF, a pet project PDF generator I’ve been working on for the past few months. Why another PDF gener...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New

Sub Categories:

We're in Beta

About us Mission Statement