Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cachebusters to CSS and JS #228

Merged
merged 1 commit into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions support/shake/1lab-shake.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ executable shake
HTML.Emit
, Shake.AgdaCompile
, Shake.Diagram
, Shake.Digest
, Shake.Git
, Shake.KaTeX
, Shake.LinkGraph
Expand Down
2 changes: 2 additions & 0 deletions support/shake/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import Shake.LinkGraph
import Shake.Markdown
import Shake.Modules
import Shake.Diagram
import Shake.Digest
import Shake.KaTeX
import Shake.Git
import Shake.Utils
Expand All @@ -48,6 +49,7 @@ import Timer
rules :: Rules ()
rules = do
agdaRules
digestRules
gitRules
katexRules
moduleRules
Expand Down
29 changes: 29 additions & 0 deletions support/shake/app/Shake/Digest.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{-# LANGUAGE BlockArguments, GeneralizedNewtypeDeriving, TypeFamilies #-}

-- | Compute a truncated hash of a file, useful for computing cache-busters
-- (or other unique ids) for a file.
module Shake.Digest (digestRules, getFileDigest) where

import qualified Data.ByteString.Lazy as LazyBS
import Data.Digest.Pure.SHA
import Data.Typeable

import Development.Shake.Classes (Hashable, Binary, NFData)
import Development.Shake

newtype FileDigest = FileDigest FilePath
deriving (Show, Typeable, Eq, Hashable, Binary, NFData)

type instance RuleResult FileDigest = String

-- | Shake rules required for computing file digest.
digestRules :: Rules ()
digestRules = versioned 1 do
_ <- addOracle \(FileDigest f) -> do
need [f]
take 8 . showDigest . sha256 <$> liftIO (LazyBS.readFile f)
pure ()

-- | Compute a short digest of a file.
getFileDigest :: FilePath -> Action String
getFileDigest = askOracle . FileDigest
17 changes: 15 additions & 2 deletions support/shake/app/Shake/Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import Shake.LinkReferences
import Shake.SearchData
import Shake.Highlights
import Shake.Options
import Shake.Digest
import Shake.KaTeX
import Shake.Git

Expand Down Expand Up @@ -198,8 +199,18 @@ buildMarkdown modname input output = do
need dependencies

baseUrl <- getBaseUrl
digest <- do
cssDigest <- getFileDigest "_build/html/css/default.css"
startJsDigest <- getFileDigest "_build/html/start.js"
mainJsDigest <- getFileDigest "_build/html/main.js"
pure . Context . Map.fromList $
[ ("css", toVal (Text.pack cssDigest))
, ("start-js", toVal (Text.pack startJsDigest))
, ("main-js", toVal (Text.pack mainJsDigest))
]

text <- liftIO $ either (fail . show) pure =<<
runIO (renderMarkdown authors references modname baseUrl markdown)
runIO (renderMarkdown authors references modname baseUrl digest markdown)

let tags = foldEquations False (parseTags text)
tags <- renderHighlights tags
Expand Down Expand Up @@ -343,8 +354,9 @@ renderMarkdown :: PandocMonad m
-> [Val Text] -- ^ List of references
-> String -- ^ Name of the current module
-> String -- ^ Base URL
-> Context Text -- ^ Digests of the various files.
-> Pandoc -> m Text
renderMarkdown authors references modname baseUrl markdown = do
renderMarkdown authors references modname baseUrl digest markdown = do
template <- getTemplate templateName >>= runWithPartials . compileTemplate templateName
>>= either (throwError . PandocTemplateError . Text.pack) pure
let
Expand All @@ -358,6 +370,7 @@ renderMarkdown authors references modname baseUrl markdown = do
, ("authors", toVal authors')
, ("reference", toVal references)
, ("base-url", toVal (Text.pack baseUrl))
, ("digest", toVal digest)
]

options = def { writerTemplate = Just template
Expand Down
6 changes: 3 additions & 3 deletions support/web/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<title>$pagetitle$ - 1Lab</title>

<link rel="stylesheet" href="$base-url$/css/default.css" />
<link rel="stylesheet" href="$base-url$/css/default.css?v=$digest.css$" />
<link rel="stylesheet" href="$base-url$/css/katex.min.css" />

<meta name="twitter:card" content="summary" />
Expand All @@ -29,8 +29,8 @@
<meta name="description" content="A formalised, explorable online resource for Homotopy Type Theory." />
$endif$

<script src="$base-url$/start.js"></script>
<script defer src="$base-url$/main.js"></script>
<script src="$base-url$/start.js?v=$digest.start-js$"></script>
<script defer src="$base-url$/main.js?v=$digest.main-js$"></script>

<noscript>
<style>
Expand Down