Best performance strategy when the same subscription is needed on multiple pages?

I have a potentially large pub / sub which contains all events happening in the current month. I need to show this on an events page and also on a separate calender page.

These pages are different React components. Currently I’m using withTracker in both components to pass the data to React. If a user visits the events page and then the calender page, will the database be called twice? Or is Meteor clever enough to not require this?

Assuming that its called twice this seems bad for performance. Is there a way of caching queries? I could just have 1 subscription in my top level component and use React to pass the data to the pages, but then the user is always requesting the data even if they don’t end up needing it.

The data may be retrieved twice, depending on where it is subscribed / de-subscribed. One quick option to optimise those scenarios: have a look at https://github.com/ccorcos/meteor-subs-cache. But there are other strategies

As long as you are moving from one page to the next without lag time between, the new subscription will take over and the publication will be reused even though the old subscription is stopped.

So just to be clear, when you say the publication will be reused, does this mean no extra database query will be sent?

Correct. You can verify this pretty easily with a console.log at the beginning of your publication.

1 Like

To add some official documentation to @copleykj’s answer, check out this section of the Meteor Guide, in particular, step 4:

  1. If the subscription is run with the same arguments then the “new” subscription discovers the old “marked for destruction” subscription that’s sitting around, with the same data already ready, and simply reuses that.
1 Like