1:- module(bc_bust, [
    2    bc_bust_token/1 % -Atomic
    3]).

Client-side cache busting

This module provides client-side cache busting support by rewriting request paths that contain specifically formatted prefix. Adds cache_token global to simple-template. */

   13:- use_module(library(http/http_wrapper)).   14:- use_module(library(st/st_expr)).   15:- use_module(library(dcg/basics)).   16:- use_module(bc_router).
 bc_bust_token(-Token) is det
Retrieves the token for cache busting. Currently it is set to the server start timestamp.
   24bc_bust_token(Token):-
   25    start_time(Token).
   26
   27% Used for storing the server start time.
   28% Start time is used for cache busting.
   29
   30:- dynamic(start_time/1).   31
   32set_start_time:-
   33    get_time(FloatTime),
   34    Time is round(FloatTime),
   35    retractall(start_time(_)),
   36    asserta(start_time(Time)).
   37    
   38:- set_start_time.   39
   40% Registers rewrite hook to remove
   41% the cache busting prefix.
   42
   43http:request_expansion(RequestIn, RequestOut):-
   44    select(path(PathIn), RequestIn, Request),
   45    atom_codes(PathIn, CodesIn),
   46    phrase(cache_busting_prefix, CodesIn, OutCodes),
   47    atom_codes(PathOut, OutCodes),
   48    RequestOut = [path(PathOut),cache_token(true)|Request],
   49    debug(bc_bust, 'Rewrote cache bust prefixed path ~p', [PathIn]).
   50
   51cache_busting_prefix -->
   52    "/t-", integer(_).
   53
   54% Add token value to simple-template
   55% global values.
   56
   57:- bc_bust_token(Token),
   58    st_set_global(cache_token, Token).