pecotamic / sitemap

Sitemap for Statamic v3
6 stars 9 forks source link

XML Header in antlers template is missing #15

Closed steffenschmidt closed 2 years ago

steffenschmidt commented 2 years ago

I used an antlers template instead of a blade. This is my code:

{{ xml_header }}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  ...
</urlset>

This is my expected output for /sitemap.xml:

<?xml version="1.0" encoding="UTF-8"?>
<urlset>
  ...
</urlset>

This is the output I got for /sitemap.xml:

<urlset>
  ...
</urlset>

The complete xml tag is missing. After switching back to the blade template I found out that there is also output of a xml header.

Any idea how I can fix this?

werner-freytag commented 2 years ago

Fixed in release 1.2.7

steffenschmidt commented 2 years ago

The release 1.2.7 does not fix this issue for me. Anything I have to change beside updating the package?

werner-freytag commented 2 years ago

Maybe a caching issue?

steffenschmidt commented 2 years ago

I cleared all caches and started debugging. The interesting part is the following:

I added \Log::debug($content); in line 30 of SitemapController:

    public function show()
    {
        $cacheUntil = Carbon::now()->addSeconds(config('pecotamic.sitemap.expire'));
        $content = Cache::remember(self::cacheKey(), $cacheUntil, function () {
            $view = view('pecotamic/sitemap::sitemap');
            return $view->with([
                'entries' => self::entriesFor($view),
                'xml_header' => '<?xml version="1.0" encoding="UTF-8"?>',
            ])->render();
        });

        \Log::debug($content);

        return response($content)
            ->header('Content-Type', 'application/xml')
            ->header('Expires', $cacheUntil->format('D, d M Y H:i:s T'));
    }

Log debug output:

[11:14:54] LOG.debug: <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/...

In this output the xml tag is available but with curl http://localhost:8000/sitemap.xml (also on prod env) the xml tag is missing and also the namespace for urlset:

<urlset><url><loc>http://localhost:8000</loc>...
werner-freytag commented 2 years ago

On my testing device the xml tag is there when I use your antlers template and fetch it with curl.

I saw that you forked the original repo. Are you sure that the updated version is used? Could the old version be in use for some reason?

steffenschmidt commented 2 years ago

The fork was from my pull request I sent you. I checked everything that I use the current release.

I will now create a new project and test your add-on to rule out that it is due to individual settings.

steffenschmidt commented 2 years ago

Ok, it's definitely something in my project. The new release works fine in a new project.

steffenschmidt commented 2 years ago

I found the error: The html minify plugin kicks the xml header. 🤦‍♂️