How to make sure something on the server runs before anything else? (prerender.io)

I’m using preprender.io 's npm package to cache pages and I’m using it as the very first line in my server index.js, before importing any other server side code like so:

// import prerender from 'prerender-node';
// prerender.set('prerenderToken', 'Meteor.settings.prerender.token');
var prerender = require('prerender-node').set('prerenderToken', Meteor.settings.prerender.token);

Either of the two. After that I import the rest of my server code. Prerender is being loaded and initialized properly but when I cache a page nothing is happening. A suggestion I got from the prerender support was:

I’d expect this page to return a prerendered page but instead I’m seeing a javascript page.

Can you show me how you have the prerender-node middleware set up? Can you make sure it’s being run before your static files are being returned?

Is this as earliest as it gets? I am guessing client and server both execute at the same time, which is why it is not working.

Does prerender work with connect? That’s the underlying web server in Meteor. Sounds like you need to attach a middleware there, which you can do with:

WebApp.rawConnectHandlers.use(middleware);
1 Like

@AndreasGalster, have you tried to use Meteor package for prerender.io (dfischer:prerenderio)? It is just a wrapper around prerender-node but it is executed before your project code and does exactly what @sashko described (WebApp.rawConnectHandlers.use(middleware) and bla-bla-bla). All packages are actually initialized before it in the alphabetical order. First of all the dependencies are loaded and then up along the tree to the packages you have directly added (seen via meteor list). @sashko will clarify it if I am wrong.

I don’t think Prerender.io is somehow dependant on your cache (they don’t know about it). As I understand their tech is that when someone who looks like search engine asks server for the page, node.js redirects the request to prerender.io, which returns the cached (on their side) page. If the page is not in the cache yet, it is to be fetched by prerender, rendered on their side and put into their cache. So, if you have a page in your (node.js side) cache and it is still not in the prerender’s cache, it will be fetched from your cache.

prerender adds blaze to the app unfortunately, that’s why I am not using it (and because I want to use as little atmosphere packages as possible)

I understand you very well… You can fork the package to get rid of the unnecessary deps of just copy their logic where required.

I’ve just tried this, no success so far.

const prerender = require('prerender-node');
const settings = Meteor.settings.prerender;

if (settings && settings.token && settings.host) {
    prerender.set('prerenderToken', settings.token);
    prerender.set('host', settings.host);
    WebApp.rawConnectHandlers.use(prerender);
}

I’m in contact with an engineer from Prerender hopefully he can help me out and I’ll post a solution for this :slight_smile:

P.S.: To all MDG devs reading: Can’t wait for the day we got rid of meteor packages. For the third time my app is loading jquery/blaze and I have no idea it’s coming from… It’s frustrating :frowning: … Meteor 1.5 FTW pls :wink:

Did you ever discover the way forwards with this?