Skip to content

Commit

Permalink
feat: improve sitemap tools
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Apr 18, 2024
1 parent 1fe297b commit 154b76c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
33 changes: 22 additions & 11 deletions RockFrontend.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -2404,18 +2404,22 @@ public function sitemap($callback = null, $options = []): void
wire()->addHookAfter("Pages::saved", $this, "sitemapReset");
}

protected function sitemapRender()
public function sitemapMarkup(): string
{
// start timer
$time = Debug::startTimer();

// make sure to render the sitemap as seen by the guest user
// save current user for later
$user = $this->wire->user;
$this->wire->user = $this->wire->users->get('guest');
$time = Debug::startTimer();
$count = 0;

// create markup
$out = "<?xml version='1.0' encoding='UTF-8'?>\n";
$out .= "<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>\n";

// recursive function to traverse the page tree
$count = 0;
$f = function ($items = null) use (&$f, &$out, &$count) {
if (!$items) $items = wire()->pages->get(1);
if ($items instanceof Page) $items = [$items];
Expand All @@ -2433,7 +2437,8 @@ protected function sitemapRender()
$out .= "<url>\n"
. "<loc>{$result->httpUrl()}</loc>\n"
. "<lastmod>$modified</lastmod>\n"
. "</url>\n";
. "</url>\n"
. $result->sitemapAppendMarkup;
} elseif ($result) {
// custom markup returned - add it to output
$out .= "$result\n";
Expand All @@ -2445,15 +2450,22 @@ protected function sitemapRender()
$f();
$out .= '</urlset>';

// create sitemap.xml file
$file = $this->wire->config->paths->root . "sitemap.xml";
$this->wire->files->filePutContents($file, $out);

$seconds = Debug::stopTimer($time);
$this->log("Sitemap showing $count pages generated in " . round($seconds * 1000) . " ms", [
'url' => '/sitemap.xml',
]);

$this->wire->user = $user;
return $out;
}

protected function sitemapRender()
{
// create sitemap.xml file
$out = $this->sitemapMarkup();
$file = $this->wire->config->paths->root . "sitemap.xml";
$this->wire->files->filePutContents($file, $out);

header('Content-Type: application/xml');
return $out;
}
Expand Down Expand Up @@ -2863,9 +2875,8 @@ private function configSEO($inputfields)
'columnWidth' => 50,
]);

$hasSitemap = $http->status(
$this->wire->pages->get(1)->httpUrl() . "sitemap.xml"
);
$httpUrl = $this->wire->pages->get(1)->httpUrl() . "sitemap.xml";
$hasSitemap = $http->status($httpUrl) === 200;
$fs->add([
'type' => 'markup',
'label' => 'sitemap.xml',
Expand Down
Binary file added docs/seo/appendmarkup.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 32 additions & 4 deletions docs/seo/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ rockfrontend()->sitemap(function (Page $page) {
});
```

### Debugging

You can use TracyDebugger to efficiently debug your sitemap while developing:

```php
// in /site/ready.php
bd(rockfrontend()->sitemapMarkup());
```

### Excluding pages from the sitemap

By default, all hidden pages will be included in the sitemap generated by RockFrontend. However, there might be instances where you want to exclude specific pages or even an entire branch of your page tree from appearing in the sitemap. This can be easily achieved by customizing the callback function provided to the `sitemap()` method:
Expand All @@ -58,14 +67,33 @@ rockfrontend()->sitemap(function (Page $page) {
return;
}

if($page->name === 'baz') {
// returning the page object results in the default markup:
// <url><loc>...</loc><lastmod>...</lastmod></url>
return $page;
// all other pages
// returning the page object results in the default markup:
// <url><loc>...</loc><lastmod>...</lastmod></url>
return $page;
});
```

### Adding pages to the sitemap

Sometimes you have endpoints that are not represented by pages in the pagetree (these docs are an example as they are rendered from markdown files rather than from ProcessWire pages).

```php
rockfrontend()->sitemap(function ($page) {
if ($page->template == 'module') {
$modified = date("Y-m-d", $page->modified);
$page->sitemapAppendMarkup =
"<url>\n"
. "<loc>{$page->httpUrl()}docs/</loc>\n"
. "<lastmod>{$modified}</lastmod>\n"
. "</url>\n";
}
return $page;
});
```

<img src=appendmarkup.png class=blur>

### How does it work?

RockFrontend adds an url hook to `/sitemap.xml` that creates the sitemap on demand. The great thing is that if the sitemap has been already created the url hook will not trigger and your server will render the static file instead! RockFrontend will simply delete that file whenever a page is saved or modules are refreshed.
Expand Down

0 comments on commit 154b76c

Please sign in to comment.