Skip to content

Commit

Permalink
Added no_Sandbox() optin and workflow for use with Browsershot/Puppet…
Browse files Browse the repository at this point in the history
…eer (#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
  • Loading branch information
mbootsman authored Jun 3, 2024
1 parent d15c618 commit 72c999d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
12 changes: 12 additions & 0 deletions resources/fieldsets/globals_seo_social_generate_social_images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
22 changes: 11 additions & 11 deletions src/Jobs/GenerateSocialImagesJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,8 +24,7 @@ class GenerateSocialImagesJob implements ShouldQueue
*
* @return void
*/
public function __construct($item)
{
public function __construct($item) {
$this->item = $item;
}

Expand All @@ -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)];
}

Expand All @@ -44,8 +42,7 @@ public function middleware(): array
*
* @return void
*/
public function handle()
{
public function handle() {
$container = AssetContainer::find('social_images');
$disk = $container->disk();

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 72c999d

Please sign in to comment.