Closed ellimondo closed 1 year ago
Can confirm the bug. Same thing recently crashed a customer website.
Can you both please provide your config/statamic/sites.php
and config/statamic/static_caching.php
files?
Sure:
sites.php
return [
'sites' => [
'default' => [
'name' => 'Deutsch',
'locale' => 'de_DE',
'url' => '/',
'attributes' => [
'site_name' => config('app.name'),
'lang_switcher_hint' => 'Seite auf Deutsch ansehen'
]
],
'english' => [
'name' => 'English',
'locale' => 'en_GB',
'url' => '/en/',
'attributes' => [
'site_name' => config('app.name'),
'lang_switcher_hint' => 'Go to English site'
]
],
],
];
static_caching.php
return [
'strategy' => env('STATAMIC_STATIC_CACHING_STRATEGY', null),
'strategies' => [
'half' => [
'driver' => 'application',
'expiry' => null,
],
'full' => [
'driver' => 'file',
'path' => public_path('static'),
'lock_hold_length' => 0,
],
],
'exclude' => [
'/sitemap.xml',
'/livewire*',
],
'invalidation' => [
'class' => null,
'rules' => 'all',
],
'ignore_query_strings' => false,
];
Related to #6496?
Here’s mine:
sites.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Sites
|--------------------------------------------------------------------------
|
| Each site should have root URL that is either relative or absolute. Sites
| are typically used for localization (eg. English/French) but may also
| be used for related content (eg. different franchise locations).
|
*/
'sites' => [
'default' => [
'name' => config('app.name'),
'locale' => 'en_US',
'url' => '/',
],
],
];
static_caching.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Active Static Caching Strategy
|--------------------------------------------------------------------------
|
| To enable Static Caching, you should choose a strategy from the ones
| you have defined below. Leave this null to disable static caching.
|
*/
'strategy' => 'full',
/*
|--------------------------------------------------------------------------
| Caching Strategies
|--------------------------------------------------------------------------
|
| Here you may define all of the static caching strategies for your
| application as well as their drivers.
|
| Supported drivers: "application", "file"
|
*/
'strategies' => [
'half' => [
'driver' => 'application',
'expiry' => null,
],
'full' => [
'driver' => 'file',
'path' => public_path('static'),
'lock_hold_length' => 0,
],
],
/*
|--------------------------------------------------------------------------
| Exclusions
|--------------------------------------------------------------------------
|
| Here you may define a list of URLs to be excluded from static
| caching. You may want to exclude URLs containing dynamic
| elements like contact forms, or shopping carts.
|
*/
'exclude' => [
'/feed.xml'
],
/*
|--------------------------------------------------------------------------
| Invalidation Rules
|--------------------------------------------------------------------------
|
| Here you may define the rules that trigger when and how content would be
| flushed from the static cache. See the documentation for more details.
| If a custom class is not defined, the default invalidator is used.
|
| https://statamic.dev/static-caching
|
*/
'invalidation' => [
'rules' => [
'collections' => [
'episodes' => [
'urls' => [
'/episodes*',
'/episodes/*',
'/',
'*',
]
],
'pies' => [
'urls' => [
'/pie-charts*',
'/episodes*',
]
]
]
]
],
/*
|--------------------------------------------------------------------------
| Ignoring Query Strings
|--------------------------------------------------------------------------
|
| Statamic will cache pages of the same URL but with different query
| parameters separately. This is useful for pages with pagination.
| If you'd like to ignore the query strings, you may do so.
|
*/
'ignore_query_strings' => true,
];
To both of you, I think this could be worked around by using an actual url in your site config.
'default' => [
'name' => config('app.name'),
'locale' => 'en_US',
'url' => '/',
],
Change '/'
to 'https://yoursite.com/
or use an environment variable like env('APP_URL')
.
We'll look into the actual issue, but this could solve your error for now.
Hi Jason
I'd already tried the first example - I tried the env variable as well - but the error persists - this is on 3.3.37 locally.
How are you serving your sites? Laravel Valet?
I can't reproduce this. Would you be able to provide a github repo with this problem happening?
Hej Jason, we tried the workaround fix, but it didn't work, either.
We actually rolled back to v3.3.35 and the error still persists. The site where the error occurs is served with k8s on a live server and it occurs only when we turn on static caching (half). With stataic caching turned off, the page works.
The error we have is not the same as above, but it has the same pattern.
production.ERROR: Undefined array key "b78dd0dbcef234639b15d6854a7bfa39980cda7a" {"exception":"[object] (ErrorException(code: 0): Undefined array key \"b78dd0dbcef234639b15d6854a7bfa39980cda7a\" at /home/forge/alaskapac.org/vendor/laravel/framework/src/Illuminate/Collections/Collection.php:1679)
Sound like it is somehow related to https://github.com/statamic/cms/issues/6781 and https://github.com/statamic/cms/issues/6496
We couldn't reproduce it locally, either.
Are you load balancing? What cache driver are you using?
I'm also having this issue for one of our sites with the static cache strategy set to half and using the file cache driver.
No load balancing or anything. Just a straight LAMP stack.
The above was on my staging server. I've also reproduced it on a local dev copy
Environment
Application Name: Statamic
Laravel Version: 9.34.0
PHP Version: 8.0.7
Composer Version: 2.1.9
Environment: local
Debug Mode: ENABLED
URL: peldon.test/
Maintenance Mode: OFF
Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED
Drivers
Broadcasting: log`
Cache: statamic
Database: mysql
Logs: stack
Mail: smtp
Queue: sync
Session: file
Statamic
Addons: 2
Antlers: runtime
Version: 3.3.43 PRO
Statamic Addons
rias/statamic-address-field: 1.0.3
statamic/seo-pro: 3.1.0
I believe I've found the source of the issue described above (at least on my project), and it comes down to the invalidation rules.
I found if a URL in the invalidation rule does not start with a '/' then you get the above error when saving that collection / taxonomy term / etc.
For example, the below rules triggers the error when saving:
'rules' => [
'collections' => [
'news' => [
'urls' => [
'news'
]
],
'projects' => [
'urls' => [
'projects',
'projects/inspiration',
'projects/map',
]
],
'people' => [
'urls' => [
'about/people'
]
],
'jobs' => [
'urls' => [
'about/jobs'
]
],
],
],
And changing to this resolves the error:
'rules' => [
'collections' => [
'news' => [
'urls' => [
'/news'
]
],
'projects' => [
'urls' => [
'/projects',
'/projects/inspiration',
'/projects/map',
]
],
'people' => [
'urls' => [
'/about/people'
]
],
'jobs' => [
'urls' => [
'/about/jobs'
]
],
],
],
Without diving deeper in to how Statamic static caching works under the hood, I assume it may be using the PHP function parse_url which is failing when the rules don't include the prefixed slash.
I hope this helps!
I can confirm that works with 3.3.43. I'd tried the trailing slash before but had overlooked putting one before a catchall *
.
Great spot Chris thanks.
Jon
Thank you @ChrisWorkhouse! That fixed it for me.
Can we add some sort of check to prepend a slash if it's not present? It looks like that happens in /cms/src/StaticCaching/Cachers/AbstractCacher.php
In the getPathAndDomain method, it uses parse_url.
Here's the current code:
protected function getPathAndDomain($url)
{
if (Str::startsWith($url, '/')) {
return [
$url,
$this->getBaseUrl(),
];
}
$parsed = parse_url($url);
$query = isset($parsed['query']) ? '?'.$parsed['query'] : '';
$path = $parsed['path'] ?? '/';
return [
$path.$query,
$parsed['scheme'].'://'.$parsed['host'],
];
}
I think adding something like the below code could be a preventative measure, but I'm not entirely familiar with everything going on in terms of the caching system. This effectively just checks the first character of the string of the path for a '/', and it prepends it to the string if it's not present. I'm not sure how elegant of a solution that is, but maybe it's worth looking into.
protected function getPathAndDomain($url)
{
if (Str::startsWith($url, '/')) {
return [
$url,
$this->getBaseUrl(),
];
}
$parsed = parse_url($url);
$query = isset($parsed['query']) ? '?'.$parsed['query'] : '';
if (!$parsed['path']) {
$path = '/';
} else {
if (substr($parsed['path'],0,1) != '/') {
$path = '/' . $parsed['path'];
}
}
return [
$path.$query,
$parsed['scheme'].'://'.$parsed['host'],
];
}
Still happening in Version: 3.3.58, slash already in place
Nice work tracking down the issue @ChrisWorkhouse 💪🏻 !
Bug description
When saving a collection entry – specifically static entries in this case - I suddenly get a
server error
.How to reproduce
The entry is actually saved but the static cache is not cleared and an error is thrown.
This occurred once statamic cms is updated to 3.3.36. I also updated to 3.3.37 to see if there was a fix but the error remains.
I've gone back locally to 3.3.35, run
statamic update
, pushed the changes, and the error goes away.Log error listed below.
Logs
Environment
Installation
Fresh statamic/statamic site via CLI
Antlers Parser
runtime (new)
Additional details
No response