Swiftpack.co - Package - voronianski/vapor-checksum-assets

Vapor Checksum Assets

Swift Platforms Package Managers

Add checksums of .js/.css files to url querystring in Vapor applications for cache busting.

Installation

Via Swift Package Manager:

.Package(url: "https://github.com/voronianski/vapor-checksum-assets.git", majorVersion: 1)

Usage

Module has a minimal API:

import Vapor
import VaporChecksumAssets

let drop = Droplet()

// pass application instance
let cs = ChecksumAssetsManager(drop)

// files are looked up in ./Public folder
let js = try cs.add("/path/to/build.js")

// returns "/path/to/build.js?f2c175192bfd0508b30d72e020af74252b98dcc3"
print(js)

Example

Controller that uses ChecksumAssetsManager to versionify assets and pass urls to Leaf template:

// ./Sources/App/main.swift

import HTTP
import Vapor
import VaporChecksumAssets

class ClientController {
  let cs: ChecksumAssetsManager

  init() {
    cs = ChecksumAssetsManager(drop)
  }

  func index(_ req: Request) throws -> ResponseRepresentable {
    // it's easy to add separate methods for production and development
    return try serve()
  }

  func serve() throws -> View {
    let css = try cs.add("/build/app.css")
    let js = try cs.add("/build/app.js")

    return try drop.view.make("index", [
      "maincss": css,
      "mainjs": js
    ])
  }
}

let drop = Droplet()

let cc = ClientController()
drop.get("/", handler: cc.index)

drop.run()
<!-- ./Resources/Views/index.leaf -->

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Example template</title>
    <link rel="stylesheet" href="#(maincss)" type="text/css" />
  </head>

  <body>
    <script src="#(mainjs)"></script>
  </body>
</html>

MIT Licensed

Github

link
Stars: 2
Help us keep the lights on

Dependencies

Used By

Total: