Discourse pups: Add google-site-verification meta tag to crawler view of Discourse

Discourse presents a Rails-only (non-ember.js, not SPA) view to (identifiable) search engine crawlers.

When you add your site to Google Search Console (GSE) you must verify your site and Google offers many ways to do this. Normally, I do this with a google-site-verification meta tag.

In Discourse, the recommended way to do this is to edit the default theme and to place the meta tag in the default theme. There are a couple of very minor problems with this approach:

  • When you upgrade the theme, Discourse will replace all your custom code.
  • The meta tag you need for your crawler verification (google-site-verification) also will be served to regular users, taking a tiny bit of extra (and unnecessary overhead).

The way I do this is to use the custom commands feature (pups) in the build container for the Discourse application, like this:

  - exec: echo "Beginning of custom commands"
  - exec: sed -i '/<\/head>/c\<meta name="google-site-verification" content="zyz-your-google-site-verification-token-abcdefg" />\n</head>' /var/www/discourse/app/views/layouts/crawler.html.erb

This way, I can direct the meta tag to only show in the crawler view, eliminating the need for an extra google-site-verification meta tag served to regular users and to insure when I upgrade the default theme (or change it) that the meta tag is not lost in the upgrade. This method, of course, works for all custom meta tags, not only google-site-verification.

The reason I do it this way is because I recently had this exact problem and GSE stopped working as it should because the verification meta tag was lost during a Discourse theme upgrade "click".

A Discourse theme component would also work, but this method (recommended by Discourse) adds a tiny bit more overhead as well; so I have opted for the the "simple unix approach" which is to SED the file when building the container.

See also:

  1. Removing Canonical Link Tags (canonical_link_tag) from Discourse
  2. GitHub - discourse/pups: Simple yaml based bootstrapper for Linux machines
1 Like