statamic / ssg

The official Statamic Static Site Generator
233 stars 24 forks source link

Glide Improvements #104

Closed jasonvarga closed 2 years ago

jasonvarga commented 2 years ago

This PR does two things:

Problem

By default, the SSG would reconfigure Glide to use the "cached" option and generate stuff into a directory within the static folder. This was necessary if you had the default Glide setup in Statamic which used the dynamic routes. The SSG would therefore need to generate the images at template-render-time.

However, each time you run the SSG, the whole static directory would get cleared, which includes your Glide generated images. Meaning the next time you run it, it would need to generate all the Glide images again.

Solution

Since there were improvements to Glide within the core, there are now better options than the override the SSG does. You can now tell the SSG to not override Glide and instead opt to leverage the improved core features. (The default is still to override it to maintain backwards compatibility.)

The main way to speed things up is to have the Glide generated images stick around between SSG generations.

For example you can have your images be generated to some directory (using the custom path glide setting), then copy that directory into the right place in your static folder.

// config/statamic/assets.php
'image_manipulation' => [
  'cache' => true,
  'cache_path' => public_path('img'),
]
// config/statamic/ssg.php
'glide' => [
    'override' => false,
],
SSG::after(function () {
    $from = public_path('img');
    $to = config('statamic.ssg.destination').'/img';
    app('files')->copyDirectory($from, $to);
});

Or, if you put your Glide images on S3, you can just set 'override' => false, and rely on the S3 URLs. No copying necessary.

Caveat

You need to adjust your config as mentioned above in order to get a speed increase. It's not going to happen just by upgrading.

mbootsman commented 1 year ago

@jasonvarga Thanks for this. Where do I put the SSG:after function?

jasonvarga commented 1 year ago

You can put it inside your AppServiceProvider's boot method.

mbootsman commented 1 year ago

I've added it like so:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Statamic\Statamic;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // Statamic::script('app', 'cp');
        // Statamic::style('app', 'cp');

        SSG::after(function () {
            $from = public_path('img');
            $to = config('statamic.ssg.destination').'/img';
            app('files')->copyDirectory($from, $to);
        });
    }
}

When I then run php please ssg:generate, I get this error: Class "App\Providers\SSG" not found SSG is installed with composer require statamic/ssg

jasonvarga commented 1 year ago

Make sure to import the class at the top of the file with the other use statements.

use Illuminate\Support\ServiceProvider;
use Statamic\Statamic;
+use Statamic\StaticSite\SSG;
mbootsman commented 1 year ago

@jasonvarga Doh! Thanks, that did the trick :+1: