Skip to content

Commit cbdced2

Browse files
Merge pull request #15 from getkirby/develop
1.0.0
2 parents d971d8b + aaa8b36 commit cbdced2

File tree

11 files changed

+1345
-101
lines changed

11 files changed

+1345
-101
lines changed

.editorconfig

100755100644
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
# This file is for unifying the coding style for different editors and IDEs
22
# editorconfig.org
33

4+
# PHP PSR-12 Coding Standards
5+
# https://www.php-fig.org/psr/psr-12/
6+
7+
root = true
8+
49
[*]
510
charset = utf-8
6-
indent_style = space
7-
indent_size = 2
811
end_of_line = lf
9-
insert_final_newline = true
12+
indent_style = tab
13+
indent_size = 2
1014
trim_trailing_whitespace = true
1115

1216
[*.php]
1317
indent_size = 4
18+
insert_final_newline = true
1419

15-
[*.md,*.txt]
16-
trim_trailing_whitespace = false
17-
insert_final_newline = false
20+
[*.yml]
21+
indent_style = space
1822

19-
[composer.json]
20-
indent_size = 4
23+
[*.md]
24+
trim_trailing_whitespace = false

.gitattributes

100755100644
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# Note: You need to uncomment the lines you want to use; the other lines can be deleted
1+
# git
2+
.gitattributes export-ignore
3+
.github/ export-ignore
4+
.gitignore export-ignore
25

3-
# Git
4-
# .gitattributes export-ignore
5-
# .gitignore export-ignore
6-
7-
# Tests
8-
# /.coveralls.yml export-ignore
9-
# /.travis.yml export-ignore
10-
# /phpunit.xml.dist export-ignore
11-
# /tests/ export-ignore
6+
# tests
7+
.php-cs-fixer.dist.php export-ignore
8+
phpmd.xml.dist export-ignore
9+
phpunit.xml.dist export-ignore
10+
psalm.xml.dist export-ignore
11+
tests/ export-ignore

.github/FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
custom: ['https://getkirby.com/buy']

.gitignore

100755100644
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
# OS files
22
.DS_Store
33

4-
# npm modules
5-
/node_modules
4+
# cs fixer
5+
.php-cs-fixer.cache
6+
7+
# editors
8+
*.sublime-project
9+
*.sublime-workspace
10+
/.idea
11+
12+
# tests
13+
.phpunit.result.cache
14+
/tests/coverage
615

716
# Composer files
817
/vendor

.php-cs-fixer.dist.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->in(__DIR__);
5+
6+
$config = new PhpCsFixer\Config();
7+
return $config
8+
->setRules([
9+
'@PSR12' => true,
10+
'align_multiline_comment' => ['comment_type' => 'phpdocs_like'],
11+
'array_indentation' => true,
12+
'array_syntax' => ['syntax' => 'short'],
13+
'cast_spaces' => ['space' => 'none'],
14+
// 'class_keyword_remove' => true, // replaces static::class with 'static' (won't work)
15+
'combine_consecutive_issets' => true,
16+
'combine_consecutive_unsets' => true,
17+
'combine_nested_dirname' => true,
18+
'concat_space' => ['spacing' => 'one'],
19+
'declare_equal_normalize' => ['space' => 'single'],
20+
'dir_constant' => true,
21+
'function_typehint_space' => true,
22+
'include' => true,
23+
'logical_operators' => true,
24+
'lowercase_cast' => true,
25+
'lowercase_static_reference' => true,
26+
'magic_constant_casing' => true,
27+
'magic_method_casing' => true,
28+
'method_chaining_indentation' => true,
29+
'modernize_types_casting' => true,
30+
'multiline_comment_opening_closing' => true,
31+
'native_function_casing' => true,
32+
'native_function_type_declaration_casing' => true,
33+
'new_with_braces' => true,
34+
'no_blank_lines_after_class_opening' => true,
35+
'no_blank_lines_after_phpdoc' => true,
36+
'no_empty_comment' => true,
37+
'no_empty_phpdoc' => true,
38+
'no_empty_statement' => true,
39+
'no_leading_namespace_whitespace' => true,
40+
'no_mixed_echo_print' => ['use' => 'echo'],
41+
'no_unneeded_control_parentheses' => true,
42+
'no_unused_imports' => true,
43+
'no_useless_return' => true,
44+
'ordered_imports' => ['sort_algorithm' => 'alpha'],
45+
// 'phpdoc_add_missing_param_annotation' => ['only_untyped' => false], // adds params in the wrong order
46+
'phpdoc_align' => ['align' => 'left'],
47+
'phpdoc_indent' => true,
48+
'phpdoc_scalar' => true,
49+
'phpdoc_trim' => true,
50+
'short_scalar_cast' => true,
51+
'single_line_comment_style' => true,
52+
'single_quote' => true,
53+
'ternary_to_null_coalescing' => true,
54+
'whitespace_after_comma_in_array' => true
55+
])
56+
->setRiskyAllowed(true)
57+
->setIndent("\t")
58+
->setFinder($finder);

LICENSE.md

100755100644
File mode changed.

README.md

100755100644
Lines changed: 155 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,25 @@
22

33
Static site performance on demand!
44

5-
This plugin will give you the performance of a static site generator for your regular Kirby installations. Without a huge setup or complex deploy steps, you can just run your Kirby site on any server – cheap shared hosting, VPS, you name it – and switch on the static cache to get incredible speed on demand.
5+
This plugin will give you the performance of a static site generator for your regular Kirby installations. Without a huge setup or complex deployment steps, you can run your Kirby site on any server – cheap shared hosting, VPS, you name it – and enable the static cache to get incredible speed on demand.
66

7-
With custom ignore rules, you can even mix static and dynamic content. Keep some pages static while others are still served live by Kirby.
7+
With custom ignore rules, you can even mix static and dynamic content. Keep some pages static while others are still served live by Kirby.
88

99
The static cache will automatically be flushed whenever content gets updated in the Panel. It's truly the best of both worlds.
1010

11-
Rough benchmark comparison for our starterkit home page:
11+
Rough benchmark comparison for our Starterkit home page:
1212

1313
Without page cache: ~70 ms
1414
With page cache: ~30 ms
1515
With static cache: ~10 ms
1616

17-
## 🚨 Experimental
17+
## Limitations
1818

19-
This plugin is still an experiment. The first results are very promising but it needs to be tested on more servers and has a couple open todos:
19+
A statically cached page will prevent any Kirby logic from executing. This means that Kirby can no longer differentiate between visitors and logged-in users. Every request will be served directly by your web server, even if the response would differ based on the cookies or other request headers.
2020

21-
- [ ] Nginx config example
22-
- [ ] Caddy config example
23-
- [x] Publish on Packagist to be installable via composer
24-
- [x] Hooks to automatically flush the cache when content is updated via the Panel
25-
- [x] Add options to ignore pages from caching
21+
If your site has any logic in controllers, page models, templates, snippets or plugins that results in different page responses depending on the request, this logic will naturally not be compatible with Staticache.
22+
23+
If only specific pages are affected by this, you can add them to the cache ignore list (see below) and use Staticache for the rest of your site. Otherwise, using Kirby's default page cache will be the better option overall because Kirby will automatically detect which responses can be cached and which caches can be used for the current request.
2624

2725
## Installation
2826

@@ -46,7 +44,9 @@ git submodule add https://github.com/getkirby/staticache.git site/plugins/static
4644

4745
### Cache configuration
4846

49-
Staticache is just a cache driver that can be activated for the page cache:
47+
**Basic setup:**
48+
49+
Staticache is a cache driver that can be activated for the pages cache:
5050

5151
```php
5252
// /site/config/config.php
@@ -55,14 +55,15 @@ return [
5555
'cache' => [
5656
'pages' => [
5757
'active' => true,
58-
'type' => 'static'
58+
'type' => 'static'
5959
]
6060
]
6161
];
6262
```
6363

64-
You can also use the cache ignore rules to skip pages that should not be cached:
65-
https://getkirby.com/docs/guide/cache#caching-pages
64+
**Ignore rules:**
65+
66+
If you want to keep some of your pages dynamic, you can configure ignore rules like for the native pages cache: https://getkirby.com/docs/guide/cache#caching-pages
6667

6768
```php
6869
// /site/config/config.php
@@ -71,7 +72,7 @@ return [
7172
'cache' => [
7273
'pages' => [
7374
'active' => true,
74-
'type' => 'static',
75+
'type' => 'static',
7576
'ignore' => function ($page) {
7677
return $page->template()->name() === 'blog';
7778
}
@@ -80,32 +81,156 @@ return [
8081
];
8182
```
8283

83-
Kirby will automatically purge the cache when changes are made in the Panel.
84+
All pages that are not ignored will automatically be cached on their first visit. Kirby will automatically purge the cache when changes are made in the Panel.
85+
86+
**Custom cache comment:**
87+
88+
Staticache adds an HTML comment like `<!-- static YYYY-MM-DDT01:02:03+00:00 -->` to the end of every cached HTML file by default. You can override or disable this comment in the cache configuration:
89+
90+
```php
91+
// /site/config/config.php
92+
93+
return [
94+
'cache' => [
95+
'pages' => [
96+
'active' => true,
97+
'type' => 'static',
98+
99+
// disabled comment
100+
'comment' => '',
101+
102+
// OR string value (only for HTML)
103+
'comment' => '<!-- your custom comment -->',
104+
105+
// OR a custom closure
106+
'comment' => fn ($contentType) => $contentType === 'html' ? '<!-- comment -->' : ''
107+
]
108+
]
109+
];
110+
```
111+
112+
**Custom root:**
113+
114+
The rendered HTML files are stored in the `site/cache/example.com/pages/` folder just like with the native pages cache. The difference is that all paths within this folder match the URL structure of your site. The separate directories for each root URL ensure that links and references in your rendered HTML keep working even in a multi-domain setup.
115+
116+
If you are using a custom web server setup, you can override the cache root like so:
117+
118+
```php
119+
// /site/config/config.php
120+
121+
return [
122+
'cache' => [
123+
'pages' => [
124+
'active' => true,
125+
'type' => 'static',
126+
'root' => '/path/to/your/cache/root',
127+
'prefix' => null
128+
]
129+
]
130+
];
131+
```
132+
133+
If your site is only served on a single domain, you can disable the root URL prefix like so while keeping the general storage location in the `site/cache` directory:
134+
135+
```php
136+
// /site/config/config.php
137+
138+
return [
139+
'cache' => [
140+
'pages' => [
141+
'active' => true,
142+
'type' => 'static',
143+
'prefix' => 'pages'
144+
]
145+
]
146+
];
147+
```
148+
149+
If you use a custom root and/or prefix, please modify the following server configuration examples accordingly.
150+
151+
### Web server integration
152+
153+
This plugin will automatically generate and store the cache files, however you need to configure your web server to pick the files up and prefer them over a dynamic result from PHP.
84154

85-
### htaccess rules
155+
The configuration depends on your used web server:
86156

87-
Add the following lines to your Kirby htaccess file, directly after the RewriteBase rule.
157+
**Apache:**
158+
159+
Add the following lines to your Kirby `.htaccess` file, directly after the `RewriteBase` rule.
88160

89161
```
90-
RewriteCond %{DOCUMENT_ROOT}/static/%{REQUEST_URI}/index.html -f [NC]
91-
RewriteRule ^(.*) %{DOCUMENT_ROOT}/static/%{REQUEST_URI}/index.html [L]
162+
RewriteCond %{DOCUMENT_ROOT}/site/cache/%{SERVER_NAME}/pages/%{REQUEST_URI}/index.html -f [NC]
163+
RewriteRule ^(.*) %{DOCUMENT_ROOT}/site/cache/%{SERVER_NAME}/pages/%{REQUEST_URI}/index.html [L]
164+
165+
RewriteCond %{DOCUMENT_ROOT}/site/cache/%{SERVER_NAME}/pages/%{REQUEST_URI} -f [NC]
166+
RewriteRule ^(.*) %{DOCUMENT_ROOT}/site/cache/%{SERVER_NAME}/pages/%{REQUEST_URI} [L]
92167
```
93168

94-
### nginx
169+
**Caddy:**
170+
171+
A simple Caddy config for Staticache may look like this:
172+
173+
```
174+
example.com
175+
176+
root * /path/to/your/site
177+
178+
file_server
179+
php_fastcgi unix//var/run/php-fpm.sock {
180+
try_files {path} site/cache/{host}/pages/{path}/index.html site/cache/{host}/pages/{path} index.php
181+
}
182+
```
183+
184+
**nginx:**
185+
186+
Standard PHP nginx config will have this location block for all requests:
187+
188+
```
189+
location / {
190+
try_files $uri $uri/ /index.php?$query_string;
191+
}
192+
```
95193

96-
Standard php nginx config will have this location block for all requests
194+
Change it to add `/site/cache/$server_addr/pages/$uri/index.html` before the last `/index.php` fallback:
97195

98196
```
99-
location / {
100-
try_files $uri $uri/ /index.php?$query_string;
101-
}
197+
location / {
198+
try_files $uri $uri/ /site/cache/$server_addr/pages/$uri/index.html /site/cache/$server_addr/pages/$uri /index.php?$query_string;
199+
}
102200
```
103-
change it to add `/static/$uri/index.html` before last `/index.php` fallback
201+
202+
### Header support
203+
204+
Staticache stores only the response bodies by default. The HTTP status code as well as headers set by your pages are not preserved in this mode. This ensures compatibility with all web servers.
205+
206+
If your web server supports reading headers from the static files, you can enable header support with the `headers` option:
207+
208+
```php
209+
// /site/config/config.php
210+
211+
return [
212+
'cache' => [
213+
'pages' => [
214+
'active' => true,
215+
'type' => 'static',
216+
'headers' => true
217+
]
218+
]
219+
];
220+
```
221+
222+
You need to adapt your web server configuration accordingly:
223+
224+
**Apache:**
225+
226+
Header support in Apache requires [`mod_asis`](https://httpd.apache.org/docs/current/mod/mod_asis.html). Please ensure that your Apache installation has this module installed and enabled.
227+
228+
Afterwards add the following block to your `.htaccess` file to make Apache use `mod_asis` for cached files:
104229

105230
```
106-
location / {
107-
try_files $uri $uri/ /static/$uri/index.html /index.php?$query_string;
108-
}
231+
<Directory "/var/www/your-site/site/cache">
232+
SetHandler send-as-is
233+
</Directory>
109234
```
110235

111236
## What’s Kirby?
@@ -123,8 +248,4 @@ change it to add `/static/$uri/index.html` before last `/index.php` fallback
123248

124249
## License
125250

126-
MIT
127-
128-
## Credits
129-
130-
- [Bastian Allgeier](https://getkirby.com/plugins/getkirby)
251+
[MIT](./LICENSE) License © 2022 [Bastian Allgeier](https://getkirby.com)

0 commit comments

Comments
 (0)