wai-asset-path (empty) → 1.0.0
raw patch · 3 files changed
+113/−0 lines, 3 filesdep +basedep +textdep +vault
Dependencies added: base, text, vault, wai
Files
- LICENSE +21/−0
- Network/Wai/Middleware/AssetPath.hs +63/−0
- wai-asset-path.cabal +29/−0
+ LICENSE view
@@ -0,0 +1,21 @@+The MIT License (MIT)++Copyright (c) 2020 digitally induced GmbH++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.
+ Network/Wai/Middleware/AssetPath.hs view
@@ -0,0 +1,63 @@+module Network.Wai.Middleware.AssetPath+( assetPathMiddleware+, assetPathFromEnvMiddleware+, assetPath+, assetVersion+) where++import Prelude+import qualified Data.Text as Text+import Data.Text (Text)+import Network.Wai+import qualified Data.Vault.Lazy as Vault+import System.IO.Unsafe (unsafePerformIO)+import System.Environment++-- | Middleware used for cache busting of static files+-- You need to provide an asset version and optionally a base url.+--+-- The base url is useful if you use a CDN to host your static files+assetPathMiddleware :: Text -> Maybe Text -> Middleware+assetPathMiddleware assetVersion assetBaseUrl next req respond =+ next req { vault = Vault.insert assetPathVaultKey (assetVersion, assetBaseUrl) req.vault } respond++-- | Reads the asset version and base url from env variables.+--+-- > assetPathFromEnvMiddleware "IHP_ASSET_VERSION" "IHP_ASSET_BASEURL"+--+-- If the asset version env var is undefined, it will fallback to the static string @"dev"@+--+-- The base url env variable is optional.+assetPathFromEnvMiddleware :: Text -> Text -> IO Middleware+assetPathFromEnvMiddleware versionEnv baseUrlEnv = do+ version <- maybe "dev" Text.pack <$> lookupEnv (Text.unpack versionEnv)+ baseUrl <- fmap Text.pack <$> lookupEnv (Text.unpack baseUrlEnv)+ pure (assetPathMiddleware version baseUrl)++assetPathVaultKey :: Vault.Key (Text, Maybe Text)+assetPathVaultKey = unsafePerformIO Vault.newKey+{-# NOINLINE assetPathVaultKey #-}++renderAssetPath :: Text -> Maybe Text -> Text -> Text+renderAssetPath version Nothing path =+ Text.concat [ "/static", path, "?v=", version ]+renderAssetPath version (Just baseUrl) path =+ Text.concat [ baseUrl, path, "?v=", version ]++-- | Adds a cache buster to a asset path+--+-- >>> assetPath request "/keyhandlers.js"+-- "/keyhandlers.js?v=9be8995c-7055-43d9-a1b2-43e05c210271"+assetPath :: Request -> Text -> Text+assetPath request path =+ case Vault.lookup assetPathVaultKey request.vault of+ Just (version, baseUrl) -> renderAssetPath version baseUrl path+ Nothing -> path++-- | Returns the assetVersion+--+-- >>> assetVersion request+-- "9be8995c-7055-43d9-a1b2-43e05c210271"+assetVersion :: Request -> Maybe Text+assetVersion request =+ fst <$> Vault.lookup assetPathVaultKey request.vault
+ wai-asset-path.cabal view
@@ -0,0 +1,29 @@+cabal-version: 2.2+name: wai-asset-path+version: 1.0.0+synopsis: assetPath function for WAI+description: Asset versioning for static files in WAI+license: MIT+license-file: LICENSE+author: digitally induced GmbH+maintainer: support@digitallyinduced.com+bug-reports: https://github.com/digitallyinduced/ihp/issues+category: Web+build-type: Simple++source-repository head+ type: git+ location: https://github.com/digitallyinduced/ihp.git++library+ default-language: GHC2021+ default-extensions:+ OverloadedRecordDot+ DuplicateRecordFields+ DisambiguateRecordFields+ BlockArguments+ OverloadedStrings+ ghc-options: -Werror=incomplete-patterns -Werror=unused-imports -Werror=missing-fields+ build-depends: base >= 4.17.0 && < 4.22, text, wai, vault+ hs-source-dirs: .+ exposed-modules: Network.Wai.Middleware.AssetPath