定制 bnomei/kirby3-staticache 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bnomei/kirby3-staticache

最新稳定版本:1.2.3

Composer 安装命令:

composer require bnomei/kirby3-staticache

包简介

Kirby 3 plugin to cache html output statically on demand with headers

README 文档

README

ARCHIVED since v1.0.0 of official plugin was released on 1/17/23

Kirby3 Staticache

Release Downloads Twitter

Kirby 3 plugin to cache html output statically on demand with headers

Commercial Usage


Support open source!

This plugin is free but if you use it in a commercial project please consider to sponsor me or make a donation.
If my work helped you to make some cash it seems fair to me that I might get a little reward as well, right?

Be kind. Share a little. Thanks.

‐ Bruno
 
M O N E Y
Github sponsor Patreon Buy Me a Coffee Paypal dontation Hire me

Installation

  • unzip master.zip as folder site/plugins/kirby3-staticache or
  • git submodule add https://github.com/bnomei/kirby3-staticache.git site/plugins/kirby3-staticache or
  • composer require bnomei/kirby3-staticache

Setup Cache

Cache configuration

Staticache is a cache driver that can be activated for the page cache.

site/config/config.php

return [
  'cache' => [
    'pages' => [
      'active' => true,
      'type' => 'static'
    ]
  ],
  // other options
];

You can also use the cache ignore rules to skip pages that should not be cached.

site/config/config.php

return [
  'cache' => [
    'pages' => [
      'active' => true,
      'type' => 'static',
      'ignore' => function ($page) {
        return $page->template()->name() === 'blog';
      }
    ]
  ],
  // other options
];

Cache duration

Kirby will automatically purge the cache when changes are made in the Panel.

Setup Server

You can use any of the following ways to make the static cache load. Using one of the native server config rules will be a tiny bit faster than PHP. But if you need to preserve the headers sent using PHP is the only easy way to do it.

PHP

If you installed the plugin with composer you can use a helper method called staticache().

index.php

<?php    
    require __DIR__ . '/kirby/bootstrap.php';

    staticache(__DIR__ . '/static');
    
    echo (new Kirby)->render();

Otherwise you need to add these lines before your kirby render method.

index.php

<?php
    // static cache file
    $staticache = __DIR__ . '/static/' . (
        !empty($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] . '/' : ''
    ) . 'index.html';
    // load static cache headers
    if (file_exists($staticache) && file_exists($staticache . '.json')) {
        foreach (json_decode(file_get_contents($staticache . '.json'), true) as $header) {
            header($header);
        }
    }
    // load static cache html
    if (file_exists($staticache)) {
        die(file_get_contents($staticache));
    }

    require __DIR__ . '/kirby/bootstrap.php';
    
    echo (new Kirby)->render();

Apache htaccess rules

Add the following lines to your Kirby htaccess file, directly after the RewriteBase rule.

RewriteCond %{DOCUMENT_ROOT}/static/%{REQUEST_URI}/index.html -f [NC]
RewriteRule ^(.*) %{DOCUMENT_ROOT}/static/%{REQUEST_URI}/index.html [L]

Nginx

Standard php nginx config will have this location block for all requests

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

change it to add /static/$uri/index.html before last /index.php fallback.

    location / {
        try_files $uri $uri/ /static/$uri/index.html /index.php?$query_string;
    }

Settings

bnomei.staticache. Default Description
root function(){...} callback to set the root
message function(){...} callback to return a message appended to html output

Disclaimer

This plugin is provided "as is" with no guarantee. Use it at your own risk and always test it yourself before using it in a production environment. If you find any issues, please create a new issue.

License

MIT

It is discouraged to use this plugin in any project that promotes racism, sexism, homophobia, animal abuse, violence or any other form of hate speech.

Credits

统计信息

  • 总下载量: 144
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-05-20