Skip to content

[6.x] Warm paginated pages with static:warm command#9493

Merged
jasonvarga merged 30 commits into
masterfrom
warm-paginated-pages
Dec 11, 2025
Merged

[6.x] Warm paginated pages with static:warm command#9493
jasonvarga merged 30 commits into
masterfrom
warm-paginated-pages

Conversation

@duncanmcclean
Copy link
Copy Markdown
Member

@duncanmcclean duncanmcclean commented Feb 8, 2024

This pull request implements warming of paginated pages into the static:warm command. This will be especially useful for larger sites which have lots of pagination pages since right now, only the first page is warmed.

How it works

Under the hood, Statamic's Cache middleware checks if there's a paginator instance being "blinked". If there is, a header is added to the response containing the URL of the next paginated page.

This header then gets picked up by the static:warm command and triggers the paginated page to be warmed, which happens in a loop until there's no more paginated pages to warm.

The implementation is inspired by statamic/ssg#140.

Warning

Due to the nature of the setting, this feature will only work when 'ignore_query_strings' => false.

It will also only pick up paginated pages when the first page isn't already cached (unless you're using the background re-caching feature).

@duncanmcclean duncanmcclean changed the title [4.x] Warm paginated pages [4.x] Warm paginated pages with static:warm command Feb 8, 2024
duncanmcclean and others added 7 commits February 9, 2024 16:35
By default, we'll have the `page` query parameter in here so pagination works.

However, if you change the pagination param name or have some other query parameter you want to whitelist, then that setting lets you do that.
@duncanmcclean duncanmcclean marked this pull request as ready for review February 12, 2024 15:19
jasonvarga

This comment was marked as resolved.

# Conflicts:
#	src/Console/Commands/StaticWarm.php
@duncanmcclean duncanmcclean changed the base branch from 4.x to 5.x May 13, 2024 12:10
@duncanmcclean duncanmcclean changed the title [4.x] Warm paginated pages with static:warm command [5.x] Warm paginated pages with static:warm command May 13, 2024
# Conflicts:
#	src/Console/Commands/StaticWarm.php
#	src/Console/Commands/StaticWarmJob.php
#	src/StaticCaching/Middleware/Cache.php
#	tests/Console/Commands/StaticWarmJobTest.php
#	tests/StaticCaching/CacherTest.php
@sylvesterdamgaard
Copy link
Copy Markdown
Contributor

@duncanmcclean Can we add query string sorting as well?

Take a look at the original code example i posted in the private channel.

This version takes a few more options into account:

  • ignore_query_strings
  • allowed_query_strings
  • disallowed_query_strings
public function getUrl(Request $request)
    {
        $url = $request->getUri();

        if ($this->config('ignore_query_strings')) {
            $url = explode('?', $url)[0];
        }

        $parts = parse_url($url);

        if (isset($parts['query'])) {
            parse_str($parts['query'], $query);

            if ($this->config('sort_query_strings')) {
                $query = Arr::sort($query);
            }

            if ($allowedQueryStrings = $this->config('allowed_query_strings')) {
                $query = array_intersect_key($query, array_flip($allowedQueryStrings));
            }

            if ($disallowedQueryStrings = $this->config('disallowed_query_strings')) {
                $disallowedQueryStrings = array_flip($disallowedQueryStrings);
                $query = array_diff_key($query, $disallowedQueryStrings);
            }

            $url = $parts['scheme'] . '://' . $parts['host'] . '?'. http_build_query($query);
        }

        return $url;
    }

@duncanmcclean
Copy link
Copy Markdown
Member Author

@sylvesterdamgaard I've split out the "allowed query parameters" part into a separate PR, where I've also implemented the sorting/disallowed options: #10701

Copy link
Copy Markdown
Member

@jasonvarga jasonvarga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gonna mark this as a draft. My previous review somehow got marked as resolved but I think it's still valid.

This unfortunately only works the first time.

Run php please static:warm, you'll see the paginated pages in the output.

Run it again, they won't be there.

This might not be an issue after #9396

@jasonvarga jasonvarga marked this pull request as draft September 11, 2024 17:29
# Conflicts:
#	config/static_caching.php
#	src/StaticCaching/Cachers/AbstractCacher.php
#	src/StaticCaching/StaticCacheManager.php
#	tests/StaticCaching/CacherTest.php
Comment thread src/StaticCaching/Cachers/FileCacher.php Outdated
@sdussaut
Copy link
Copy Markdown

sdussaut commented Apr 1, 2025

Hi!
That would be a great thing to be able to warm paginated pages. Is there any estimated time on when this would land?
Thanks!

@duncanmcclean
Copy link
Copy Markdown
Member Author

Sorry, we don't have an ETA for reviewing/merging this pull request. We'll get to it when we can.

In the meantime, you can pull this PR into your project with a composer patch.

@jasonvarga
Copy link
Copy Markdown
Member

This needs to target master and be updated appropriately since #9396 has merged.

@duncanmcclean duncanmcclean changed the base branch from 5.x to master November 5, 2025 16:43
@duncanmcclean duncanmcclean changed the title [5.x] Warm paginated pages with static:warm command [6.x] Warm paginated pages with static:warm command Nov 5, 2025
@duncanmcclean duncanmcclean marked this pull request as ready for review December 10, 2025 18:54
@jasonvarga jasonvarga merged commit 08c8115 into master Dec 11, 2025
14 checks passed
@jasonvarga jasonvarga deleted the warm-paginated-pages branch December 11, 2025 19:46
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.

4 participants