Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Broken" links inside a <template> tag

I've recently started using the <template> tag for HTML that I process afterwards using a template library, e.g.

<template id="tmpl">
  <div class="something">
    <a href="/pages/{{link}}">{{title}}</a>
  </div>
</template>
...
<script>
var output = Mustache.render($('#tmpl').html(), {
  link: 'abc',
  title: 'abc'
});
</script>

However, I've come to realise this means I have a broken link (example.com/pages/{{link}}) in my HTML. This is a concern, as various crawlers might consider it invalid (in fact, the Google Search Console reports my homepage as having a broken link).

  1. Is it valid to use <template> this way?

  2. Is it better to put it in something like <script type="text/template"> instead (as seen on the handlebars.js website)?

like image 224
fstanis Avatar asked Oct 31 '16 12:10

fstanis


People also ask

Do Broken links hurt SEO?

Broken links can destroy your conversion rates. They indirectly harm SEO by affecting bounce rate, time on site, and how you pass link juice. Broken links may also directly harm SEO rankings by sending signals that your website is old and outdated.

Why do hyperlinks break?

Links may be broken for a variety of reasons, including the URL being mistyped, the webpage no longer being online, the page's URL having changed, or the linked page having restricted access (such as by being behind a password or firewall).


1 Answers

The output variable does contain the HTML we would expect, i.e., the rendered template; however, your code does not write the contents of the output variable anywhere.

Here is a working example:

<template id="tmpl">
  <div class="something">
    <a href="/pages/{{link}}">{{title}}</a>
  </div>
</template>

<span id="output"></span>

<script>
var output = Mustache.render($('#tmpl').html(), {
  link: 'abc',
  title: 'abc'
});
$('#output').html(output);
</script>

Google has not properly crawled the test site I setup for this. However, when I asked GoogleBot to render my version of your code it displayed the link inside the template element, i.e., *{{title}}* and the rendered template link, i.e., *abc*. Even though Google says you have a broken link in the template element, you really don't when a user views it.

One possible way to get Google to quit indicating that you have a broken link is to surround your template tags with <!--googleoff: anchor--> ...templates... <!--googleon: anchor-->. These tags stop googlebot from indexing anchor tags contained within.

Example:

<!--googleoff: anchor-->
<template id="tmpl">
    <div class="something">
        <a href="/pages/{{link}}">{{title}}</a>
    </div>
</template>
<!--googleon: anchor-->
like image 113
Tim Avatar answered Sep 20 '22 13:09

Tim