Skip to content

Commit

Permalink
feat: add debug info for sitemap.xml generation
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Mar 19, 2024
1 parent e3354e9 commit e030f26
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
15 changes: 13 additions & 2 deletions RockFrontend.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,13 @@ class RockFrontend extends WireData implements Module, ConfigurableModule
*/
public $remBase;

private $scripts;

/** @var Seo */
public $seo;

private $scripts;
private $sitemapCallback;

private $styles;

/** @var array */
Expand Down Expand Up @@ -2303,13 +2306,15 @@ protected function sitemapRender()
{
// make sure to render the sitemap as seen by the guest 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
$f = function ($items = null) use (&$f, &$out) {
$f = function ($items = null) use (&$f, &$out, &$count) {
if (!$items) $items = wire()->pages->get(1);
if ($items instanceof Page) $items = [$items];
foreach ($items as $p) {
Expand All @@ -2331,6 +2336,7 @@ protected function sitemapRender()
// custom markup returned - add it to output
$out .= "$result\n";
}
$count++;
$f($p->children("include=hidden"));
}
};
Expand All @@ -2341,6 +2347,11 @@ protected function sitemapRender()
$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',
]);

header('Content-Type: application/xml');
return $out;
}
Expand Down
12 changes: 12 additions & 0 deletions docs/seo/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ rockfrontend()->sitemap(function (Page $page) {
});
```

### 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.

That means that on large sites the first request of /sitemap.xml after a page save could potentially take some time. The method is built in a way that it is as efficiently as possible, meaning that if you `return false` for a page in the tree the whole branch will neither be processed nor loaded into memory. Though all other pages will be processed and loaded into memory, so on very large websites you'd have to take care of that issue yourself.

Just check the logs for RockFrontend - there you will see something like this and you'll get an idea if you have to do something or its fast enough for your use case:

```
Sitemap showing 958 pages generated in 272 ms
```

---

## SEO Markup Tags
Expand Down

0 comments on commit e030f26

Please sign in to comment.