From 72c999d41eb205399f5eaa3f289c41b9cff4cc6f Mon Sep 17 00:00:00 2001 From: Marcel Bootsman Date: Mon, 3 Jun 2024 20:48:31 +0200 Subject: [PATCH] Added no_Sandbox() optin and workflow for use with Browsershot/Puppeteer (#44) * Added no_Sandbox() optin and workflow for use with Browsershot/Puppeteer * Added if clause to the new option to make sure it is only shown is the use_social_image_generation option is true --- ...als_seo_social_generate_social_images.yaml | 12 ++++++++++ src/Jobs/GenerateSocialImagesJob.php | 22 +++++++++---------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/resources/fieldsets/globals_seo_social_generate_social_images.yaml b/resources/fieldsets/globals_seo_social_generate_social_images.yaml index d004b10..f97ca61 100644 --- a/resources/fieldsets/globals_seo_social_generate_social_images.yaml +++ b/resources/fieldsets/globals_seo_social_generate_social_images.yaml @@ -23,3 +23,15 @@ fields: width: 50 if: use_social_image_generation: 'equals true' + - + handle: use_no_sandbox_for_social_image_generation + field: + display: 'Use noSandbox() for Social Images generation' + type: toggle + icon: toggle + instructions: 'This runs Browsershot with the [noSandbox()](https://spatie.be/docs/browsershot/v4/miscellaneous-options/disable-sandboxing) method. **Important**: Only use this if you have problems with Browsershot/Puppeteer.' + instructions_position: below + listable: hidden + width: 100 + if: + use_social_image_generation: 'equals true' diff --git a/src/Jobs/GenerateSocialImagesJob.php b/src/Jobs/GenerateSocialImagesJob.php index 790c3ca..eb8db91 100644 --- a/src/Jobs/GenerateSocialImagesJob.php +++ b/src/Jobs/GenerateSocialImagesJob.php @@ -12,9 +12,9 @@ use Illuminate\Support\Str; use Spatie\Browsershot\Browsershot; use Statamic\Facades\AssetContainer; +use Statamic\Globals\GlobalSet; -class GenerateSocialImagesJob implements ShouldQueue -{ +class GenerateSocialImagesJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; public $item; @@ -24,8 +24,7 @@ class GenerateSocialImagesJob implements ShouldQueue * * @return void */ - public function __construct($item) - { + public function __construct($item) { $this->item = $item; } @@ -34,8 +33,7 @@ public function __construct($item) * * @return void */ - public function middleware(): array - { + public function middleware(): array { return [(new WithoutOverlapping('generate-social-images'))->expireAfter(60)]; } @@ -44,8 +42,7 @@ public function middleware(): array * * @return void */ - public function handle() - { + public function handle() { $container = AssetContainer::find('social_images'); $disk = $container->disk(); @@ -84,12 +81,15 @@ public function handle() Artisan::call('cache:clear'); } - protected function setupBrowsershot(): Browsershot - { + protected function setupBrowsershot(): Browsershot { $id = $this->item->id(); $absolute_url = $this->item->site()->absoluteUrl(); - $browsershot = Browsershot::url("{$absolute_url}/social-images/{$id}"); + if (GlobalSet::findByHandle('seo')->inDefaultSite()->get('use_no_sandbox_for_social_image_generation')) { + $browsershot = Browsershot::url("{$absolute_url}/social-images/{$id}")->noSandbox(); + } else + $browsershot = Browsershot::url("{$absolute_url}/social-images/{$id}"); + if ($nodeBinary = config('statamic-peak-seo.social_image.node_binary')) { $browsershot->setNodeBinary($nodeBinary);