Skip to content

A Statamic (v3) addon that automates downloading CSS and JS assets from CDNs so that they can be self hosted.

License

Notifications You must be signed in to change notification settings

rosswintle/statamic-asset-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Statamic 3 Asset Cache

A Statamic (v3) addon that automates downloading CSS and JS assets from CDNs so that they can be self hosted.

This is a Statamic tag based on my Laravel Asset Cache package.

Installation

Use the following versions (I think - pre v3.x may not have been accurate):

  • v3.x for Statamic 3.3/Laravel 9 on PHP 8.0/8.1
  • v2.x for Statamic 3.1/3.2/Laravel 8 on PHP 7/8.0
  • v1.x for Statamic 3.0/Laravel 8 on PHP 7

Install the package with composer:

composer require rosswintle/statamic-asset-cache

Cached assets are stored in and served from the public file storage "disk". You will need to have symlinked your public/storage directory to storage/app/public as per the Laravel docs using:

php artisan storage:link

Be sure to do this in ALL environments: local, staging, and production, if you haven't already.

Usage

The Statamic tag is:

{{ asset_cache package='<npm package name>' version='<version constraint>' file='<file path and name>' }}
  • package is the name of an npm package (currently only npm is supported via jsdelivr.net)
  • version is a version constraint such as 1.9.0. Semantic versioning is assumed. You can use 1.9 to get the latest 1.9.x version as per the jsdelivr docs but this is not recommended in production environments
  • file is the path and filename (with extension) for the asset that you want relative to the package's root. For example dist/alpine.js

The tag outputs the URL of the locally-cached file from the package.

Example:

<script defer src="{{ asset_cache package='alpinejs' version='1.9' file='dist/alpine.min.js' }}"></script>

You can also use this for CSS:

<link rel="stylesheet" href="{{ asset_cache package='tailwindcss' version='1.1.4' file='dist/tailwind.min.css' }}">

What does this do?

Using the tag:

  • Downloads the asset from jsdelivr.net
  • Caches it in your site's public directory
  • Returns the URL of the cached, local asset

What problem does this solve?

It saves you having to manually download assets and include them in your project if you want to host them locally.

There are various reasons why you may want to do this, such as avoiding having your users tracked, to avoid depending on third-party CDNs and there are possible performance benefits too.

I'm also on a mission to ditch npm and build process from simple projects, so this bit of automation seemed useful.

If you dare specify an imprecise version constraint such as just 1.9 you can also get latest releases of dependencies without having to do anything! But all the big CDN's advise against this as it can break your site so use with caution and avoid in production environments!!

Compatibility

This package was built for and tested with Statamic 3 (beta) and Laravel 6.x, but should work on newer versions.

Limitations

This is my first public package and Statamic addon. All sorts of things could be wrong! Please be gentle.

More limitations are given in the package's readme

Roadmap

See the package's roadmap

Credits

About

A Statamic (v3) addon that automates downloading CSS and JS assets from CDNs so that they can be self hosted.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages