GithubHelp home page GithubHelp logo

tango-tango / phoenix_etag Goto Github PK

View Code? Open in Web Editor NEW

This project forked from michalmuskala/phoenix_etag

0.0 0.0 0.0 32 KB

ETag support for phoenix

License: Other

Erlang 31.99% Elixir 68.01%

phoenix_etag's Introduction

PhoenixETag

Conditional request (ETag & modified-since) support for Phoenix.

Based heavily on code from hex_web.

Usage

The library provides a replacement function for Phoenix.Controller.render/1-4 called PhoenixETag.render_if_stale/1-4 accepting exactly the same arguments. When called the function expects the view to implement an additional callback: stale_checks/2 similar to render/2 that is responsible for returning the etag value and/or last modified value for the current resource.

Additional helper PhoenixETag.schema_etag/1 is provided for generating etag values of a single or multiple schema structs.

# controller

def show(conn, %{"id" => id}) do
  data = MyApp.load_data(id)
  PhoenixETag.render_if_stale(conn, :show, data: data)
end

# view

def stale_checks("show." <> _format, %{data: data}) do
  [etag: PhoenixETag.schema_etag(data), 
   last_modified: PhoenixETag.schema_last_modified(data)]
end

Both the etag and last_modified values are optional. The first one will add an etag header to the response and perform a stale check against the if-none-match header. The second one will add a last-modified header to the response and perform a stale check against the if-modified-since header. If the headers indicate cache is fresh a 304 Not Modified response is triggered, and rendering of the response is aborted. If headers indicate cache is stale, render proceeds as normal, except the extra headers are added to the response.

Installation

If available in Hex, the package can be installed by adding phoenix_etag to your list of dependencies in mix.exs:

def deps do
  [{:phoenix_etag, "~> 0.1"}]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/phoenix_etag.

phoenix_etag's People

Contributors

michalmuskala avatar nathanalderson avatar michalzalecki avatar wojtekmach avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.