Coder Social home page Coder Social logo

aerni / statamic-dynamic-cache Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 102 KB

Supercharge Statamic's static cache by dynamically excluding URLs that shouldn't be cached statically

License: MIT License

PHP 100.00%
statamic statamic-v3 statamic-3 statamic-addon

statamic-dynamic-cache's Introduction


This addon has been archived in favor of the long-awaited nocache tag, which was introduced in Statamic v3.3.20.


Statamic Packagist version Packagist Total Downloads License

Dynamic Cache

If you ever used Statamic's static caching with the full strategy, you know that it doesn't play well with forms and dynamic listings like sort="random". This is where Dynamic Cache steps in. It dynamically updates the exclude and invalidation rules array in your static_caching.php config based on a boolean in your entries' content.

Dynamic Cache is a lifesaver for sites with complex page builders based on Replicator and Bard. Your page builder might have dozens of components and only one requiring dynamic functionality. Without this addon, you'd have to do without full static caching because you'd never know which page actually included that one component that doesn't work when cached statically.

Features

  • Adds your entries' URLs to the static caching exclude array
  • Populates the invalidation rules array
  • Updates the config whenever you save or delete an entry or change the structure of a collection
  • Artisan Command to manually trigger a config update

Note: This addon currently only works with collection entries.

Installation

Install the addon using Composer:

composer require aerni/dynamic-cache

Publish the config of the package:

php please vendor:publish --tag=dynamic-cache-config

The following config will be published to config/dynamic-cache.php.

return [

    /*
    |--------------------------------------------------------------------------
    | Field Handle
    |--------------------------------------------------------------------------
    |
    | Define the name of the field handle you would like to use.
    | Default: 'exclude_from_static_cache'
    |
    */

    'handle' => 'exclude_from_static_cache',

];

Configuration

You may change the field's handle that is used to check if an entry should be excluded from the static cache. The handle defaults to exclude_from_static_cache.

Basic Usage

Dynamic Cache will look for exclude_from_static_cache: true in your entries. The best way to add this value to your content is by creating a Fieldset with a Hidden Fieldtype and adding it to your Blueprints where necessary.

If you use a page builder, I suggest adding the Fieldset to every Replicator or Bard set that requires dynamic functionality. This way, the entry will only be excluded from the static cache if the component in question is present. If it is not, the entry will be cached statically.

Fieldset with Hidden Fieldtype:

title: 'Exclude From Static Cache'
fields:
  -
    handle: exclude_from_static_cache
    field:
      display: 'Exclude From Static Cache'
      type: hidden
      icon: hidden
      listable: hidden
      replicator_preview: false
      default: true

Alternatively, you can also use a Toggle Fieldtype to manually turn the static cache on and off. Just note that this kind of defeats the dynamic problem this addon is trying to solve โ€ฆ

Fieldset with Toggle Fieldtype:

title: 'Exclude From Static Cache'
fields:
  -
    handle: exclude_from_static_cache
    field:
      display: 'Exclude From Static Cache'
      type: toggle
      icon: toggle
      listable: hidden

Manual Config Changes

You are free to manually change the exclude and rules array in the static_caching.php config. Dynamic Cache is smart enough to merge your manual changes.

Invalidation Rules

This addon will not generate any invalidation rules if the config is set to rules => 'all'.

Commands

You may update the config with the following command:

php artisan dynamic-cache:update

This is useful if you change your entries in your code editor rather than in the Control Panel.

Git Automation

This addon supports Statamic's Git integration. Simply add the following paths to the paths array in your git.php config. Also, make sure that the addon's storage path is tracked by git.

'paths' => [
    config_path('statamic'),
    storage_path('statamic/addons/dynamic-cache'),
]

statamic-dynamic-cache's People

Contributors

aerni avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

statamic-dynamic-cache's Issues

Setting STATAMIC_GIT_ENABLED to false in .env causes Exception

I found that when setting STATAMIC_GIT_ENABLED to false in the .env file, a "Statamic Git integration is currently disabled" Exception occurs.

Replication Steps

  1. install Dynamic Cache via composer whilst STATAMIC_GIT_ENABLED = true
  2. Use Dynamic Cache functionality (? maybe required or not)
  3. set STATAMIC_GIT_ENABLED = false
  4. Attempt to access pages or CP results in error (see below). Artisan/please seems to break too.

Screenshot 2022-02-14 at 16 09 15

static_caching.php is updated after git commits when paired with git functionality

Hi Michael,

I'm not 100% sure about whether to open an issue for this so apologies if its not appropriate. I noticed that when using statamics git commit/push functionality alongside your addon (which is super helpful by the way, thank you), the automatic changes made to static_caching.php occur after git commits/pushes changes. I would think that these changes should occur before by default.

Adam

v1.1.1 causes composer install issue

Just have one more issue. Composer install v1.1.1 causes a database connection [] not configured exception if no database is configured. In my case, my site has no database.

Replication steps

  1. Set DB_CONNECTION in .env to empty or null
  2. Upgrade Dynamic Cache addon via CP or composer to v1.1.1
  3. See produced error below

Screenshot 2022-02-14 at 22 18 31

Question: Is this necessary for half caching?

Is a solution like this is required for forms and dynamic sections (e.g. filtered entries) when using the half caching method? You mention full caching specifically in the docs.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.