Set meta tags from plugin?

plugins

#1

I’m attempting to dynamically inject some meta tags into my page from a plugin, the problem right now is that by using javascript to accomplish this is that the javascript runs after the page is rendered meaning the original rendered page does not contain the meta tags and the scraper I’m targeting cannot pick up those tags as they don’t yet exist.

Is there a way to define meta tags in my specific plugin and have it add it to the page before it’s rendered, thanks.


(Mittineague) #2

Have you tried adding the meta tags using the Admin Customize UI?


#3

Well that does appear to solve the problem in some way, however it removes any ability to make those programmatically generated so for example if I have two different configuration values that i want defined depending on the environment i’m in for example making this solution less than ideal.


(Mittineague) #4

Yes, if you want different meta tags for different pages, a solution will be more involved.

I’m unclear what you mean by “environments”, desktop vs. mobile?
What meta tag are you wanting to add?


#5

Just using the facebook sdk as an example here but there is an app_id meta tag that can change between development and production environments, the ability to have those tags set dynamically would be ideal.


(Mittineague) #6

I see nothing that would be useful for this in the page head, but this appears near the bottom

<script>
  Discourse.CDN = 'https://cdn-enterprise.discourse.org/meta';
  Discourse.BaseUrl = 'meta.discourse.org'.replace(/:[\d]*$/,"");
  Discourse.BaseUri = '';
  Discourse.Environment = 'production';
  Discourse.SiteSettings = PreloadStore.get('siteSettings');
  Discourse.LetterAvatarVersion = '5_410b5db071eaaad4484aa17e7909345f';
  I18n.defaultLocale = 'en';
  PreloadStore.get("customEmoji").forEach(function(emoji) {
    Discourse.Dialect.registerEmoji(emoji.name, emoji.url);
  });
  Discourse.start();
  Discourse.set('assetVersion','9f702385bb711d8ee1040c10ca094a87');
  Discourse.Session.currentProp("disableCustomCSS", false);
  Discourse.HighlightJSPath = "/highlight-js/meta.discourse.org/b27922047d554ba5955be67fa50a7ee1ee6c148c.js";
      Discourse.S3CDN = 'https://meta-s3-cdn.freetls.fastly.net';
    Discourse.S3BaseUrl = '//discourse-meta.s3-us-west-1.amazonaws.com';
</script>

Have you already tried using Discourse.Environment ?


#7

Yes that was my first attempt but this can’t be done using javascript as the javascript doesn’t run until after the page has initially been rendered, what I need is something either in the plugin.rb file or the settings.yml file that will parse the values before the page is initially rendered.


(system) #8

(Sam Saffron) #9

This is now possible, example in discourse solved.

See this example:

You register crawler custom HTML with: server:before-head-close-crawler

register_html_builder('server:before-head-close-crawler') do |controller|
    "<meta something='something'>".html_safe
end

You register non crawler with 'server:before-head-close :

register_html_builder('server:before-head-close') do |controller|
    "<meta something='something'>".html_safe
end