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

Craft 3.x + Blitz 3.x + LocalWarmer: transform images' URLs are empty #545

Closed therealpecus closed 1 year ago

therealpecus commented 1 year ago

Running Craft CMS 3.8.16 with Blitz 3.12.10 and LocalWarmer.

Due to a combination of CDN+WAF setup that prevents us from using GuzzleWarmer, we're forced to generate pages via the LocalWarmer driver.

The issue we're experiencing is that image transforms come out empty in the HTML. Meaning that a call to asset.getUrl(transform) yields src="", whereas calls to asset.getUrl() yield the full URL to the asset, e.g. src="https://site/asset/resource.ext".

Running Blitz in dev mode provides a hint, but I have not been able to trace back the code to find where the issue actually is. For some reason, mocking the request as it is done in the LocalWarmer throws when accessing the image transform:

2023-08-03 19:49:41 [-][-][-][info][yii\db\Command::execute] UPDATE `assettransformindex` SET `assetId`=5230, `volumeId`=1, `filename`='Marketing-color.png', `format`=NULL, `location`='_80x90_fit_center-center_none', `fileExists`=0, `inProgress`=0, `error`=1, `dateIndexed`='2023-08-03 17:16:31', `dateUpdated`='2023-08-03 17:49:41' WHERE `id`=15
2023-08-03 19:49:41 [-][-][-][profile begin][yii\db\Command::execute] UPDATE `assettransformindex` SET `assetId`=5230, `volumeId`=1, `filename`='Marketing-color.png', `format`=NULL, `location`='_80x90_fit_center-center_none', `fileExists`=0, `inProgress`=0, `error`=1, `dateIndexed`='2023-08-03 17:16:31', `dateUpdated`='2023-08-03 17:49:41' WHERE `id`=15
2023-08-03 19:49:41 [-][-][-][profile end][yii\db\Command::execute] UPDATE `assettransformindex` SET `assetId`=5230, `volumeId`=1, `filename`='Marketing-color.png', `format`=NULL, `location`='_80x90_fit_center-center_none', `fileExists`=0, `inProgress`=0, `error`=1, `dateIndexed`='2023-08-03 17:16:31', `dateUpdated`='2023-08-03 17:49:41' WHERE `id`=15
2023-08-03 19:49:41 [-][-][-][error][craft\errors\VolumeObjectNotFoundException] craft\errors\VolumeObjectNotFoundException: The file "Marketing-color.png" does not exist (assets/industry/Marketing-color.png). in /var/www/html/vendor/craftcms/cms/src/services/AssetTransforms.php:1145
Stack trace:
...

This happens when running the console command craft blitz/cache/warm, which is a cron job we need to run to ensure all pages of the site are fresh and available. If the page is accessed via a regular HTTP request, the cached file content is correct.

This seems to point to a configuration (possibly in the asset path) for console requests in the LocalWarmer.

I am available for further troubleshooting. I realize the LocalWarmer is experimental in v3 and that it has been completely rewritten in v4, but we cannot upgrade yet.

bencroker commented 1 year ago

Have you set the generateTransformsBeforePageLoad config setting to true?

therealpecus commented 1 year ago

Have you set the generateTransformsBeforePageLoad config setting to true?

yes, and transforms are available on the file system

bencroker commented 1 year ago

Hmm, I’ll need to look into it and will give you an update on what I find.

bencroker commented 1 year ago

This seems to point to a configuration (possibly in the asset path) for console requests in the LocalWarmer.

What is the asset path set to? And if it contains an alias, how is that defined?

therealpecus commented 1 year ago

Hi Ben, here's the config for the asset volume

handle: assets
hasUrls: true
name: Assets
settings:
  path: assets
sortOrder: 1
titleTranslationKeyFormat: null
titleTranslationMethod: site
type: craft\volumes\Local
url: $ASSET_URL

the .env sets $ASSET_URL to:

ASSET_URL=https://mydomain.ddev.site/assets

(the example is from the dev setup, it is matched with the correct domain in staging and prod.)

Aliases are defined as:

    // Custom aliases
    'aliases' => [
        '@rootUrl' => App::env('DEFAULT_SITE_URL'),
        '@webroot' => dirname(__DIR__) . '/web',
    ],
bencroker commented 1 year ago

From your asset volume config, it looks like the path is set to assets. This should be set to an absolute path such as @webroot/assets/site, otherwise the path will not be resolvable when the request is called via a console command.

therealpecus commented 1 year ago

Hi Ben,

thanks, your suggestion was spot on and fixed it. I did not know that paths needed to be absolute.

Now that images work, I realized all cached pages are saved with the content of the first page being generated, despite progressing through the URLs and calling each one (debug is on). Again, only with the localWarmer. This seems like the request not being reset, or the Yii app somehow not releasing completely the previous execution.

Let me know if you prefer to track this issue in a separate ticket.

bencroker commented 1 year ago

Now that images work, I realized all cached pages are saved with the content of the first page being generated, despite progressing through the URLs and calling each one (debug is on). Again, only with the localWarmer.

Is this happening in a local development environment only? The local warmer was never fully robust in all local dev environments, hence the “experimental” warning on it.

therealpecus commented 1 year ago

unfortunately no, it happens in production environments too.

it seems the response object is not cleared after the first emission, but there seem to be no method to reset it after saving it. I understand this is a high effort/low return issue, since the plugin has moved to v4.

bencroker commented 1 year ago

unfortunately no, it happens in production environments too.

In that case, I’d put your efforts into getting the GuzzleWarmer to work (or update to Blitz 4). Even with a CDN/WAF it should be possible to add the server’s IP address to a safe list.

I understand this is a high effort/low return issue, since the plugin has moved to v4.

Indeed, this is an experimental feature of a version that is no longer in active development.

therealpecus commented 1 year ago

I think we can close the issue as solved. Thanks Ben