How to add a script to head in 1.3?

I need to add Stripe. Stripe is supposed to go in the head of the page.

Is there a way to add to the head of the page? I keep getting errors with iFrame when I do.

relevant stripe docs here

This works for me:

  <script type='text/javascript'>console.log('test')</script>

Alternatively, you might find it easier to just use the npm package

There’s also a new API in 1.3 for injecting stuff even before the Meteor script tags. @gadicc What was that API again? Can you write a tiny example?

1 Like

link to docs? Where is this?

Here it is:

There’s also this nice package:

EDIT: I meant to link to meteorhacks/inject-initial, not inject-data.

1 Like

you can script like google analytics does

I think it looks like this (but haven’t tried):

WebApp.connectHandlers.use(function(req, res, next) {
  if (/* check URL */) {
    req.dynamicHead = 'something to insert';
    req.dynamicBody = 'something to insert';

However, for non-dynamic content, that will always remain the same (i.e. inserting some external script into the HEAD), I think just doing it via an HTML file is much simpler and clearer:

  <script src="..."></script>

It’s fine to have many such files, Meteor goes through all the *.html files and concatenates all the HEAD sections.

Neither seems to be working, I think it has something to do with Stripe in particular

The thing with that is that the script tags are always inserted after the script tags for all the Meteor core packages and 3rd party packages. In my case, I want a non-dynamic script tag to be the very first script tag in the head. Even with the dynamicHead feature, the content comes after the other stuff. Seems like meteorhacks/inject-initial is the way to do it.

Oh, previously, I meant to link to meteorhacks/inject-initial, not meteorhacks/inject-data (see edited comment above). That package makes it easy!

Ah ok, I hear you. Yes, in that case, meteorhacks:inject-initial is the way to go :ok_hand:

Can you give @corvid an example of how to use it in this case? :slight_smile:

Yep, here’s how @corvid:

meteor add meteorhacks:inject-initial
// on the server side
Inject.rawModHtml('inject a script at the beginning of the head', function(html) {
    return html.replace('<head>', '<head><script>...</script>');

@gadicc Just to make sure, when and where do we use this code? I’m guessing anywhere on the serverside, not inside a Meteor.startup block?

Exactly :ok_hand:


P.S. I’m pretty sure that even the rawHead() method injects to the very top of the HEAD.

1 Like

Ah, good point. I used rawModHtml because the docs only had an example of that one, and didn’t explain that detail about rawHead. See, goes to show that I like owning code more than reading it, and otherwise try to rely on documentation. :smiley:


I have it on good authority that a PR adding such an example and clarification would be graciously accepted to the project :wink:

1 Like

Still getting some problems:

This occurs after trying to use both inject and just plain head tag to include

Can you clone your project for us on github? @trusktr helped me realize that the plain head tag would be too late, but the inject code should work perfectly for this.

Or perhaps before that, can you view-source on your project and confirm if the script is being injected at the top of the HEAD tag?

For whatever reason, removing and re-adding all packages seemed to fix the problem :\

1 Like