Are template-level subscriptions with multiple templates subscribing to the same publication run once or once for each template?

Viewed 242

Suppose you have template1 and template2, both subscribing using this.subscribe('samePublication', sameArg) inside their .onCreated() and this.autorun().

What happens when we have something like this:

<template name="template3">
    {{>template1}}
    {{>template2}}
</template>

Will this.subscribe('samePublication', sameArg) run once for each template, and hit my server and DB twice?

Should I put this.subscribe() inside the .onCreated() on template3?

My own understanding is 'no' and 'no', after having read this: http://docs.meteor.com/#/full/meteor_subscribe

Hoping someone more knowledgeable can comment. Thanks in advance.

1 Answers

In my experience, the subscriptions load and hit your DB every time you route to the view and render the template.

While the general best practice (probably to promote modularity) is to use template level subscriptions, I have found that if a route is heavily used and frequently changed to and it involves large amounts of docs, then it makes more performance sense to load those subscriptions on client startup and share between views.

Did you know you can pass an argument with a subscription to a publication and use the argument in your publication to filter the results? Using this often makes a big difference on the performance without falling back to global subscriptions.

PS: I am working on parallel subscriptions that do not start until after the loading of 'fast' landing pages, so that when the user switches to a heavily subscribed page, the parallel loading would already have completed, but would not affect the 'first impressions' of the user.

Related