Empty new line at start of document

I got the problem that on a Grav site, I get a new empty line at the start of every document. With the sitemap plugin installed, I get this error on /sitemap.xml:
Screenshot of error message

This is because there is an empty line at the start of the document:


<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/user/plugins/sitemap/sitemap.xsl"?>
<urlset
  xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
  xmlns:xhtml="http://www.w3.org/1999/xhtml"
  xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
  xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"
>
...

Every other page, html, xml, … get this empty line too, and I don’t know why. On other sites with the almost same config and plugins (same theme with some customizations), there is no error.


After some playing around, I found out that this error only happens if the shortcode-core plugin is installed. But this can’t be the main reason, since other sites have this plugin installed too and there is no error. There must be a different cause of the problem.

To me it sounds like there’s some kind of error, but error output is turned off and you get just empty line instead of actual error.

Or maybe you’re using a custom theme which has empty line in base.html.twig or something

Thanks for the response. I already thought of that and checked my themes, it looks like this:

{% extends '@quark/partials/base.html.twig' %}

{% block head deferred %}
<meta charset="utf-8" />
...

The empty like isn’t the cause. Could the extends or block tag cause this?

I tried some things to fix this bug. Here’s what I found out:

unsuccessful attempts

Every of these attempts didn’t solve the issue:

  • enahble all logging/ debugger settings in config ⇒ no output, so it isn’t an error which causes the problem
  • replace the base.html.twig file (which includes the extends) with the original quark base template ⇒ it isn’t the extends tag
  • removing the base.html.twig template in the inherited theme doesn’t affect anything ⇒ it isn’t a theme error
  • the sitemap.xml.twig template in the plugins/sitemap dir doesn’t have an extends tag ⇒ it isn’t the extends tag or a theme causing the error
    • the template starts directly with <?xml version="1.0" encoding="UTF-8"?> ⇒ it has to be somewhere in the template processing
  • reverting to various older versions of grav ⇒ the issue doesn’t come with the latest update
  • the issue persists without the sitemap plugin

successful attempts

  • removing the shortcode-core plugin resolves the issue ⇒ it probably has to do something with how the plugin processes templates

Solution

With the help I got from discord, I successfully located the issue and solved the problem (from my side). After long debugging, the problem was caused by 2 empty lines which were in the .gitkeep file in the custom shortcodes folder in the theme. When I removed the empty line, the problem was gone.

Since this is probably caused by the shortcode plugin parsing all files in that directory, it added that 2nd empty line from .gitkeep to the HTML output. I opened an issue on GitHub about this and it’s potential security risk.

2 Likes