FoF Sitemap

FoF Sitemap fof/sitemap

Generate a sitemap

Sitemap by FriendsOfFlarum

MIT license Latest Stable Version Total Downloads OpenCollective

This extension simply adds a sitemap to your forum.

It uses default entries like Discussions and Users, but is also smart enough to conditionally add further entries based on the availability of extensions. This currently applies to flarum/tags and fof/pages. Other extensions can easily inject their own Resource information, check Extending below.

Modes

There are two modes to use the sitemap, both now serving content from the main domain for search engine compliance.

Runtime mode

After enabling the extension the sitemap will automatically be available at /sitemap.xml and generated on the fly. Individual sitemap files are served at /sitemap-1.xml, /sitemap-2.xml, etc. It contains all Users, Discussions, Tags and Pages guests have access to.

Applicable to small forums, most likely on shared hosting environments, with discussions, users, tags and pages summed up being less than 10,000 items. This is not a hard limit, but performance will be degraded as the number of items increase.

Cached multi-file mode

For larger forums, sitemaps are automatically generated and updated via the Flarum scheduler. Sitemaps are stored on your configured storage (local disk, S3, CDN) but always served from your main domain to ensure search engine compliance. Individual sitemaps are accessible at /sitemap-1.xml, /sitemap-2.xml, etc.

A first sitemap will be automatically generated after the setting is changed. Subsequent updates are handled automatically by the scheduler (see Scheduling section below).

A rebuild can be manually triggered at any time by using:

php flarum fof:sitemap:build

Best for larger forums, starting at 10,000 items.

Risky Performance Improvements

This setting is meant for large enterprise customers.

The optional "Enable risky performance improvements" option modifies the discussion and user SQL queries to limit the number of columns returned. By removing those columns, it significantly reduces the size of the database response but might break custom visibility scopes or slug drivers added by extensions.

This setting only brings noticeable improvements if you have millions of discussions or users. We recommend not enabling it unless the CRON job takes more than an hour to run or that the SQL connection gets saturated by the amount of data.

Search Engine Compliance

This extension automatically ensures search engine compliance by:

  • Domain consistency: All sitemaps are served from your main forum domain, even when using external storage (S3, CDN)
  • Unified URLs: Consistent URL structure (/sitemap.xml, /sitemap-1.xml) regardless of storage backend
  • Automatic proxying: When external storage is detected, content is automatically proxied through your main domain

This means you can use S3 or CDN storage for performance while maintaining full Google Search Console compatibility.

Scheduling

The extension automatically registers with the Flarum scheduler to update cached sitemaps. This removes the need for manual intervention once configured. Read more information about setting up the Flarum scheduler here.

The frequency setting for the scheduler can be customized via the extension settings page.

Installation

This extension requires PHP 8.0 or greater.

Install manually with composer:

composer require fof/sitemap

Updating

composer update fof/sitemap
php flarum migrate
php flarum cache:clear

Nginx issues

If you are using nginx and accessing /sitemap.xml or individual sitemap files (e.g., /sitemap-1.xml) results in an nginx 404 page, you can add the following rules to your configuration file:

location = /sitemap.xml {
    try_files $uri $uri/ /index.php?$query_string;
}

location ~ ^/sitemap-\d+\.xml$ {
    try_files $uri $uri/ /index.php?$query_string;
}

These rules ensure that Flarum will handle sitemap requests when no physical files exist.

Extending

Register a new Resource

In order to register your own resource, create a class that implements FoF\Sitemap\Resources\Resource. Make sure to implement all abstract methods, check other implementations for examples. After this, register your

return [
    new \FoF\Sitemap\Extend\RegisterResource(YourResource::class),
];

Dynamic Priority and Frequency (Optional)

Your custom resource can optionally implement dynamic priority and frequency values based on the actual model data:

class YourResource extends Resource
{
    // Required abstract methods...
    
    /**
     * Optional: Dynamic frequency based on model activity
     */
    public function dynamicFrequency($model): ?string
    {
        $lastActivity = $model->updated_at ?? $model->created_at;
        $daysSinceActivity = $lastActivity->diffInDays(now());
        
        if ($daysSinceActivity < 1) return Frequency::HOURLY;
        if ($daysSinceActivity < 7) return Frequency::DAILY;
        if ($daysSinceActivity < 30) return Frequency::WEEKLY;
        return Frequency::MONTHLY;
    }
    
    /**
     * Optional: Dynamic priority based on model importance
     */
    public function dynamicPriority($model): ?float
    {
        // Example: Higher priority for more popular content
        $popularity = $model->view_count ?? 0;
        
        if ($popularity > 1000) return 1.0;
        if ($popularity > 100) return 0.8;
        return 0.5;
    }
}

If these methods return null or are not implemented, the static frequency() and priority() methods will be used instead. This ensures full backward compatibility with existing extensions.

That's it.

Remove a Resource

In a very similar way, you can also remove resources from the sitemap:

return [
    (new \FoF\Sitemap\Extend\RemoveResource(\FoF\Sitemap\Resources\Tag::class)),
];

Register a static URL

Some pages of your forum might not be covered by the default resources. To add those urls to the sitemap there is a pseudo resource called StaticUrls. You can use the RegisterStaticUrl extender to add your own urls. The extender takes a route name as parameter, which will be resolved to a url using the Flarum\Http\UrlGenerator class.

return [
    (new \FoF\Sitemap\Extend\RegisterStaticUrl('reviews.index')),
];

Force cache mode

If you wish to force the use of cache mode, for example in complex hosted environments, this can be done by calling the extender:

return [
    (new \FoF\Sitemap\Extend\ForceCached()),
]

Optional Sitemap Elements

The extension allows you to control whether <priority> and <changefreq> elements are included in your sitemap:

Admin Settings

  • Include priority values: Priority values are ignored by Google but may be used by other search engines like Bing and Yandex
  • Include change frequency values: Change frequency values are ignored by Google but may be used by other search engines for crawl scheduling

Both settings are enabled by default for backward compatibility.

Dynamic Values

When enabled, the extension uses intelligent frequency calculation based on actual content activity:

  • Discussions: Frequency based on last post date (hourly for active discussions, monthly for older ones)
  • Users: Frequency based on last seen date (weekly for active users, yearly for inactive ones)
  • Static content: Uses predefined frequency values

This provides more meaningful information to search engines compared to static values.

Troubleshooting

Regenerating Sitemaps

If you've updated the extension or changed storage settings, you may need to regenerate your sitemaps:

php flarum fof:sitemap:build

Debug Logging

When Flarum is in debug mode, the extension provides detailed logging showing:

  • Whether sitemaps are being generated on-the-fly or served from storage
  • When content is being proxied from external storage
  • Route parameter extraction and request handling
  • Any issues with sitemap generation or serving

Check your Flarum logs (storage/logs/) for detailed information about sitemap operations.

Commissioned

The initial version of this extension was sponsored by profesionalreview.com.

Links

Versions

  • Version 2.3.0.

    Likely works with Flarum v1.8.10.

    Unlikely to work with Flarum v2.0.0-beta.3.

  • 23 additional versions.
  • Extension created.