Skip to content

Commit

Permalink
feat: Added sitemap snippet and routes
Browse files Browse the repository at this point in the history
  • Loading branch information
holmey committed Apr 1, 2023
1 parent 5ec76dd commit 17e217b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
23 changes: 23 additions & 0 deletions site/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,33 @@
'hooks' => [],
'routes' => [
[
// articles overview is not a page itself but just a container
'pattern' => 'articles',
'action' => fn () => false
],
[
'pattern' => 'sitemap.xml',
'action' => function () {
$pages = site()->pages()->index();

// fetch the pages to ignore from the config settings,
// if nothing is set, we ignore the error page
$ignore = kirby()->option('sitemap.ignore', ['error']);

$content = snippet('sitemap', compact('pages', 'ignore'), true);

// return response with correct header type
return new Kirby\Cms\Response($content, 'application/xml');
}
],
[
'pattern' => 'sitemap',
'action' => function () {
return go('sitemap.xml', 301);
}
]
],
'sitemap.ignore' => ['error'],
'cache' => [
'pages' => [
'active' => true,
Expand Down
13 changes: 13 additions & 0 deletions site/snippets/sitemap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?= '<?xml version="1.0" encoding="utf-8"?>'; ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php foreach ($pages as $p): ?>
<?php if (in_array($p->uri(), $ignore)) {
continue;
} ?>
<url>
<loc><?= html($p->url()) ?></loc>
<lastmod><?= $p->modified('c', 'date') ?></lastmod>
<priority><?= ($p->isHomePage()) ? 1 : number_format(0.5 / $p->depth(), 1) ?></priority>
</url>
<?php endforeach ?>
</urlset>

0 comments on commit 17e217b

Please sign in to comment.