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

[4.x] Ability to provide additional urls for static:warm #9303

Merged

Conversation

ryanmitchell
Copy link
Contributor

When using static:warm the command already cleverly works out any urls to hit from the repositories and basic Route::statamic routes.

However if you have a Route::statamic that has dynamic elements, eg Route::statamic('/artist/{artistSlug}/{portfolioType}') then it doesn't know the URLs to hit.

This PR adds the concept of an extra uri generator to the static:warm command, allow you to specify a class with a handle method that returns a collection of (absolute or relative) urls to hit, e.g.

<?php

namespace App\Caching;

class StaticWarmExtras
{
    public function handle()
    {
        return collect(['/path/to/some/page']);
    }
}

This class can be defined in statamic.static_caching.warm_extra_routes_generator eg

    'warm_extra_routes_generator' => \App\Caching\StaticWarmExtras::Class,

@jasonvarga
Copy link
Member

jasonvarga commented Mar 22, 2024

I've simplified to use Hooks.

Throw this in a service provider:

use Statamic\Console\Commands\StaticWarm;

StaticWarm::hook('additional', function ($urls, $next) {
    return $next($urls->merge([
        '/custom-1',
        '/custom-2',
        'https://different-domain.com/custom-3',
    ]));
});

Using hooks allows multiple places to add extra uris. Maybe you want some in your app, and an addons could add their own.

Also tweaked the output and how urls are converted to absolutes.

If you have more a more complicated use case and really wanted that dedicated class, you still can:

use App\StaticWarmExtras;
use Statamic\Console\Commands\StaticWarm;

StaticWarm::hook('additional', function ($urls, $next) {
    return $next($urls->merge(StaticWarmExtras::handle()));
});

@jasonvarga jasonvarga changed the title [4.x] Allow a class to be specified to generate extra static:warm routes [4.x] Ability to provide additional urls for static:warm Mar 22, 2024
@jasonvarga jasonvarga merged commit f127b50 into statamic:4.x Mar 22, 2024
18 checks passed
@ryanmitchell ryanmitchell deleted the feature/extra-static-warm-routes branch March 22, 2024 19:17
@ryanmitchell
Copy link
Contributor Author

Nice. Thanks!

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.

None yet

2 participants