bnomei / kirby3-staticache
此包已被弃用且不再维护。未建议替代包。
Kirby 3 插件,可在请求时静态缓存 HTML 输出并添加头部信息
1.2.3
2022-11-15 16:21 UTC
Requires
- php: >=7.4.0
- getkirby/composer-installer: ^1.1
Requires (Dev)
- getkirby/cms: ^3.5
- php-coveralls/php-coveralls: ^2.4
- phpunit/phpunit: ^9.5
Suggests
- bnomei/kirby3-feeds: Adds a xml sitemap which will be automatically be included in the robots.txt
README
自官方插件于 2023 年 1 月 17 日的 v1.0.0 版本发布以来已存档
Kirby3 Staticache
Kirby 3 插件,可在请求时静态缓存 HTML 输出并添加头部信息
商业用途
支持开源!
此插件免费,但如果您在商业项目中使用它,请考虑赞助我或进行捐赠。
如果我的工作帮助您赚了钱,我认为我应该得到一点回报,对吧?
善良。分享一点。谢谢。
- Bruno
M | O | N | E | Y |
---|---|---|---|---|
Github 赞助 | Patreon | 请我喝咖啡 | Paypal 捐赠 | 雇佣我 |
安装
- 解压 master.zip 到文件夹
site/plugins/kirby3-staticache
或 git submodule add https://github.com/bnomei/kirby3-staticache.git site/plugins/kirby3-staticache
或composer require bnomei/kirby3-staticache
设置缓存
缓存配置
Staticache 是一个可以为页面缓存激活的缓存驱动程序。
site/config/config.php
return [ 'cache' => [ 'pages' => [ 'active' => true, 'type' => 'static' ] ], // other options ];
您还可以使用缓存 忽略规则 来跳过不应缓存的页面。
site/config/config.php
return [ 'cache' => [ 'pages' => [ 'active' => true, 'type' => 'static', 'ignore' => function ($page) { return $page->template()->name() === 'blog'; } ] ], // other options ];
缓存持续时间
Kirby 将在面板中做出更改时自动清除缓存。
设置服务器
您可以使用以下任何一种方法来加载静态缓存。使用本地服务器配置规则之一将比 PHP 快一点。但如果您需要保留使用 PHP 发送的头部信息,那么这是唯一简单的方法。
PHP
如果您使用 composer 安装了插件,则可以使用名为 staticache()
的辅助方法。
index.php
<?php require __DIR__ . '/kirby/bootstrap.php'; staticache(__DIR__ . '/static'); echo (new Kirby)->render();
否则,您需要在 kirby 渲染方法之前添加以下行。
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 规则
将以下行添加到您的 Kirby htaccess 文件中,直接在 RewriteBase 规则之后。
RewriteCond %{DOCUMENT_ROOT}/static/%{REQUEST_URI}/index.html -f [NC]
RewriteRule ^(.*) %{DOCUMENT_ROOT}/static/%{REQUEST_URI}/index.html [L]
Nginx
标准的 php nginx 配置将为所有请求具有以下 location 块
location / {
try_files $uri $uri/ /index.php?$query_string;
}
将其更改为在最后一个 /index.php
回退之前添加 /static/$uri/index.html
。
location / {
try_files $uri $uri/ /static/$uri/index.html /index.php?$query_string;
}
设置
bnomei.staticache. | 默认 | 描述 |
---|---|---|
root | function(){...} |
设置根的回调 |
message | function(){...} |
返回附加到 HTML 输出的消息的回调 |
免责声明
本插件提供“现状”并无保证。请自行承担使用风险,并在将其用于生产环境前自行测试。如果您发现任何问题,请创建新问题。
许可证
不建议在推广种族主义、性别歧视、恐同症、动物虐待、暴力或其他任何形式仇恨言论的项目中使用此插件。
鸣谢
- 基于Bastian Allgeier的创意
- 扩展版本由Bruno Meilick开发