All Projects → johannschopplich → kirby-hashed-assets

johannschopplich / kirby-hashed-assets

Licence: MIT license
🛷 File name hashes support for css() and js() helpers. Without rewrite rules!

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to kirby-hashed-assets

laravel-mix-kirby
Laravel Mix helper for Kirby
Stars: ✭ 23 (+53.33%)
Mutual labels:  kirby, kirby-plugin
Kirby Imageset
A flexible, responsive image component for Kirby 2, featuring lazy-loading, fancy placeholders and much more.
Stars: ✭ 122 (+713.33%)
Mutual labels:  kirby, kirby-plugin
Kirby Typography
Typographic enhancements for your Kirby-driven website.
Stars: ✭ 80 (+433.33%)
Mutual labels:  kirby, kirby-plugin
kirby-highlight
Themeable server-side syntax highlighting for Kirby
Stars: ✭ 14 (-6.67%)
Mutual labels:  kirby, kirby-plugin
Kirby Visual Markdown
Visual Markdown Editor for Kirby CMS 2
Stars: ✭ 171 (+1040%)
Mutual labels:  kirby, kirby-plugin
Kirby Calendar Plugin
A plugin for the Kirby CMS to easily implement an event calendar.
Stars: ✭ 76 (+406.67%)
Mutual labels:  kirby, kirby-plugin
Kirby Imagekit
Asynchronous thumbnail creation and optimization for Kirby 2
Stars: ✭ 109 (+626.67%)
Mutual labels:  kirby, kirby-plugin
Kirby Sri
Subresource integrity hashing & cache-busting static assets for Kirby
Stars: ✭ 9 (-40%)
Mutual labels:  kirby, kirby-plugin
kirby-highlighter
🌐 Server-side syntax highlighting for the Kirby code block & KirbyText
Stars: ✭ 18 (+20%)
Mutual labels:  kirby, kirby-plugin
Kirby Focus
Better image cropping for Kirby CMS
Stars: ✭ 170 (+1033.33%)
Mutual labels:  kirby, kirby-plugin
Embed
Media embed plugin for Kirby 2 CMS
Stars: ✭ 73 (+386.67%)
Mutual labels:  kirby, kirby-plugin
django-manifest-loader
Simplifies webpack configuration with Django
Stars: ✭ 105 (+600%)
Mutual labels:  manifest, assets
Panel Bar
panelBar for Kirby 2 CMS
Stars: ✭ 70 (+366.67%)
Mutual labels:  kirby, kirby-plugin
kirby-podcast
A KirbyCMS-Podcast-Plugin
Stars: ✭ 22 (+46.67%)
Mutual labels:  kirby, kirby-plugin
Select A Structure
A select field for Kirby populated by a structure field.
Stars: ✭ 44 (+193.33%)
Mutual labels:  kirby, kirby-plugin
Field Multiselect
Multiselect field for Kirby 2 CMS
Stars: ✭ 84 (+460%)
Mutual labels:  kirby, kirby-plugin
search-for-kirby
Kirby 3 plugin for adding a search index (sqlite or Algolia).
Stars: ✭ 42 (+180%)
Mutual labels:  kirby, kirby-plugin
kirby-copy-files
Clone page dashboard widget for Kirby panel
Stars: ✭ 12 (-20%)
Mutual labels:  kirby, kirby-plugin
Kirby Autogit
⬢ Saves every change made via Kirby Panel to a Git repository
Stars: ✭ 147 (+880%)
Mutual labels:  kirby, kirby-plugin
Kirby Uniform
A versatile Kirby plugin to handle web form actions.
Stars: ✭ 214 (+1326.67%)
Mutual labels:  kirby, kirby-plugin

Kirby Hashed Assets

Enhances Kirby's css() and js() helpers to support hashed filenames. Pass your normal paths (e.g. …main.js) – the plugin will lookup hashed assets and transform the path automatically (e.g. …main.20201226.js). That way you can even keep asset paths identical in development and production environment!

Key Features

  • 🛷 Cache bust assets without query strings
  • 🎢 No need for web server rewrite rules!
  • Supports manifest.json
  • 🎿 Supports manually hashed file names
  • ☃️ Create preload links with hashedUrl() helper

Projects Using the Hashed Assets Plugin

Requirements

  • PHP 8.0+
  • Kirby 3.7+

Installation

Download

Download and copy this repository to /site/plugins/kirby-hashed-assets.

Git Submodule

git submodule add https://github.com/johannschopplich/kirby-hashed-assets.git site/plugins/kirby-hashed-assets

Composer

composer require johannschopplich/kirby-hashed-assets

Usage

Automatic Hashing With manifest.json

For file hashing this plugin uses the hashup npm package.

hashup is a tiny CLI tool with two objectives in mind for your freshly build assets:

  1. Rename or rather hash (hence the name) the assets.
  2. Generate a manifest.json for them.

You don't even have to install it to your devDependencies, since npx will fetch it once on the fly. Add hashup to your build pipeline by adding it your package.json scripts (recommended), for example:

{
  "scripts": {
    "clean": "rm -rf public/assets/{css,js}",
    "build": "npm run clean && <...> && npx -y hashup"
  }
}

Now, pass asset paths to Kirby's asset helpers like you normally do:

<?= js('assets/js/main.js') ?>
// `<script src="https://example.com/assets/js/main.9ad649fd.js"></script>

If a corresponding hashed file is found in the manifest.json, it will be used and rendered.

For template-specific assets, use @template (instead of @auto):

<?= js('@template') ?>
// `<script src="https://example.com/assets/js/templates/home.92c6b511.js"></script>`

⚠️ If no template file exists, https://example.com/@template will be echoed. This will lead to HTTP errors and blocked content since the requested file doesn't exist and the error page's HTML will be returned.

If you are unsure if a template file exists, use the following helpers:

  • cssTpl()
  • jsTpl()

They will echo a link tag, respectively script tag, only if a template file for current page's template is present.

Manual Hashing

For smaller websites you may prefer no build chain at all, but still want to utilize some form of asset hashing. In this use-case you can rename your files manually.

Take an imaginary main.js for example. Just include it like you normally would in one of your snippets:

<?= js('assets/js/main.js') ?>

Now rename the file in the format of main.{hash}.js. You may use the current date, e.g.: main.20201226.js, which will output:

<script src="https://example.com/assets/js/main.20201226.js"></script>

Voilà, without changing the asset path the hashed file will be found and rendered in your template!

Hashed Filenames for Preloading Links

You can use the global hashedUrl() helper to lookup a file like you normally would with the css() or js() helpers. While the latter return a link or respectively script tag, the hashedUrl() helper will only return a URL which you can use in any context.

<link rel="preload" href="<?= hashedUrl('assets/css/templates/default.css') ?>" as="style">
// <link rel="preload" href="/assets/css/templates/default.1732900e.css" as="style">

Since all evergreen browsers finally support JavaScript modules natively, you may prefer preloading modules:

<link rel="modulepreload" href="<?= hashedUrl('assets/js/templates/home.js') ?>">
// <link rel="preload" href="/assets/js/templates/home.92c6b511.js">

License

MIT License © 2021-2023 Johann Schopplich

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].