Incorrect header::contentType on sitemap

Hi,

I’ve been putting together a small test site to play with Kirby 3. As part of this I wanted to generate an automatic sitemap so I followed the instructions here:

https://getkirby.com/docs/cookbook/seo/xmlsitemap

I think I’ve followed everything correctly, however when I run a validation check on the sitemap using the xml-sitemap’s checker I get an error of:

Incorrect http header content-type: “text/html; charset=UTF-8” (expected: “application/xml”)

To check what I’ve done, I have a snippet in site/snippets called sitemap.php:

<?= '<?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') ?></lastmod>
            <priority><?= ($p->isHomePage()) ? 1 : number_format(0.5 / $p->depth(), 1) ?></priority>
        </url>
    <?php endforeach ?>
</urlset>

and in site/config/config.php I have:

'sitemap.ignore' => ['error'],
'routes' => [
  [
'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');

    // set the correct header type
    header::contentType('text/xml', 'UTF-8', true);

    return snippet('sitemap', compact('pages', 'ignore'), true);
}
  ],
  [
'pattern' => 'sitemap',
'action'  => function() {
  return go('sitemap.xml', 301);
}
  ]
],

and the HTML generated is:

<?xml version="1.0" encoding="utf-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
                            <url>
            <loc>*website url*</loc>
            <lastmod>2019-01-17T19:04:41+00:00</lastmod>
            <priority>1</priority>
        </url>
    </urlset>

The generated HTML looks correct, so I think the problem is the header::contentType isn’t being set correctly by the config file.

Can anyone help me see where I’m going wrong?

you need to use a new way to send headers in kirby3.

also a plugin exists for sitemaps if you need a ready-to-use solution.

Hi,

The instruction I’m using to set the header content type is the same as in the cookbook article on sitemaps which I would have thought would be right? I’ll have a look at the forum post you linked to and try that.

Thanks,

i see. as far i know her @texnixe has read this already, she will check the cookbook example and fix it if needed.