Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Glide Improvements #104

Merged
merged 4 commits into from
Jul 18, 2022
Merged

Glide Improvements #104

merged 4 commits into from
Jul 18, 2022

Conversation

jasonvarga
Copy link
Member

@jasonvarga jasonvarga commented Jun 28, 2022

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.

@jasonvarga jasonvarga marked this pull request as ready for review July 18, 2022 19:28
@jasonvarga jasonvarga merged commit 90cd590 into master Jul 18, 2022
@jasonvarga jasonvarga deleted the glide branch August 10, 2022 14:37
@mbootsman
Copy link

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

@jasonvarga
Copy link
Member Author

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

@mbootsman
Copy link

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
Copy link
Member Author

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
Copy link

@jasonvarga Doh! Thanks, that did the trick 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Images not being generated Slow when using Reponsive Images addon Slow when using Glide with S3
2 participants