putyourlightson / craft-blitz

Intelligent static page caching for creating lightning-fast sites with Craft CMS.
https://putyourlightson.com/plugins/blitz
Other
149 stars 36 forks source link

`blitz/cache/warm` doesn't warm any files #17

Closed Tam closed 6 years ago

Tam commented 6 years ago

As part of our deployment process we run ./craft blitz/cache/warm, but every time we do we get the following response:

Warming Blitz cache – this may take some time.
Blitz cache successfully warmed 0 files.

I'm using the blitz.php config file, currently with the following patterns:

return [
  // ...
  'includedUriPatterns' => [
    '/',
    'team.*',
  ]
];

The cache is cleared correctly but the pages only re-cache upon first visit by a user, never by the command.

putyourlightson commented 6 years ago

Is warming the cache working using the utility in the control panel?

Tam commented 6 years ago

Seems to be, yeah.

putyourlightson commented 6 years ago

Ok, will look into it and let you know what I find.

Tam commented 6 years ago

I've since updated my includedUriPatterns to:

'includedUriPatterns' => [
    '/',
    'team.*',
    'shop.*',
],

I also added some debug code to CacheService.php#L384-L403:

Warming Blitz cache – this may take some time.
# print_r($urls);
Array
(
    [0] => /product-categories/book
    [1] => /product-categories/food
    [2] => /product-categories/kitchenware
    [3] => /partners/guittard-chocolate
    [4] => /team/person
    [5] => /shop
    [6] => /news
    [7] => /team
    [8] => /cookery-courses
    [9] => /apprenticeships
    [10] => /
    [11] => /chefs-table
    [12] => /courses/mexican-fiesta
    [13] => /shop/infused-oils
    [14] => /shop/cooking-showdown
)
# Empty catches replated w/ echo $e->getMessage() . PHP_EOL;
cURL error 3: <url> malformed (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
cURL error 3: <url> malformed (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
cURL error 3: <url> malformed (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
cURL error 3: <url> malformed (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
cURL error 3: <url> malformed (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
cURL error 3: <url> malformed (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
cURL error 3: <url> malformed (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
cURL error 3: <url> malformed (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
cURL error 3: <url> malformed (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
cURL error 3: <url> malformed (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
cURL error 3: <url> malformed (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
cURL error 3: <url> malformed (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
cURL error 3: <url> malformed (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
cURL error 3: <url> malformed (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
cURL error 3: <url> malformed (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
Blitz cache successfully warmed 0 files.

There are two clear issues here:

  1. Blitz is matching all URLs seemingly regardless of the URI patterns.
  2. The URLs are all relative.

The second issue is an easy fix. We were using @web in our siteUrl and as you mentioned in the readme that won't work in the console, so I've fixed that by hard-coding the siteUrl in general.php.

The first issue however, is a bug in the code. You are only getting the first character of the URI patterns (CacheService.php#L83) meaning all the patterns are being read incorrectly (it also means empty patterns throw a PHP notice causing the command to fail).

Next, you are trimming / when checking if the URI is a match (CacheService.php#L449). So when Blitz recieved a pattern starting with / it was becoming an empty regex, hence all URLs being matched. / becoming empty isn't really an issue, just something it might be worth making people aware of.

Another thing worth noting is that __home__ becomes an empty string (CacheService.php#L124) so / wouldn't have worked any way.

(If anyone's interested, to match the home page a negative look ahead for any char ^(?!.) is needed.)

Long story longer, I've made a pull request with a fix.

putyourlightson commented 6 years ago

I'm not sure your assumption is correct. Your includedUriPatterns config setting should contain an array of arrays. I realise this may be counter-intuitive but it is how the table field stores its data, so that is how Blitz expects to find it. I may update the code for the next release but in the meantime can you try this with the original code?

'includedUriPatterns' => [
    ['/'],
    ['team.*'],
    ['shop.*'],
],
Tam commented 6 years ago

Yep, that makes sense! Also explains why the table in the settings was all empty rows. That's definately something worth mentioning in the docs as well.

putyourlightson commented 6 years ago

I've updated Blitz in https://github.com/putyourlightson/craft-blitz/commit/94eddbb7fe2ae4ca1a5f6945854e17c4f2efacef to normalize patterns to strings, so this should now work for you as well:

'includedUriPatterns' => [
    '/',
    'team.*',
    'shop.*',
],