Skip to content

Add sites hook to Sitemap class#438

Merged
duncanmcclean merged 4 commits into6.xfrom
sitemap-site-hook
Oct 23, 2025
Merged

Add sites hook to Sitemap class#438
duncanmcclean merged 4 commits into6.xfrom
sitemap-site-hook

Conversation

@duncanmcclean
Copy link
Copy Markdown
Member

This pull request adds a sites hook to the Sitemap class, allowing developers to override which sites should be used when generating a sitemap.

Useful for headless setups where you may only have a single domain, but you have multiple sites. You may want to determine the returned sites using a query parameter instead.

// app/Providers/AppServiceProvider.php

use Statamic\Facades\Site;
use Statamic\SeoPro\Sitemap\Sitemap;

Sitemap::hook('sites', function ($sites) {
    return Site::all()->filter(fn ($site) => $site->handle() === request()->site)->values();
});

Closes #437

@duncanmcclean duncanmcclean merged commit 0e66ab1 into 6.x Oct 23, 2025
43 checks passed
@duncanmcclean duncanmcclean deleted the sitemap-site-hook branch October 23, 2025 09:28
@jerome2710
Copy link
Copy Markdown

@duncanmcclean that's quick, thank you! This looks like a solution we could use.

One observation, as the sitemap is cached with Cache::remember(Sitemap::CACHE_KEY.'_'.$key.'_index'), won't the output for all site be cached, regardless of the hook, as it's called later?

We would call /sitemap.xml?site=X and /sitemap.xml?site=Y, where I would assume site Y would also get the sitemap of site X, as this response is cached?

@duncanmcclean
Copy link
Copy Markdown
Member Author

Good catch! I've opened #439 to address this which I'll merge and tag soon.

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.

Sitemap for headless multi-site

2 participants