Iron Router 呼叫订阅多少次?

How many times does Iron Router call subscriptions?

我有一个包含多个路由的多页应用程序。每个路由的订阅是使用 "waitOn" 实现的,并且工作正常。

我注意到,当我导航到不同的页面时,先前路由的订阅被删除了。到目前为止,没问题。

我在 Router.configure 级别实现了一个 "waitOn" 调用,因此所有页面都可以使用特定的订阅。

我的问题是:Iron Router 是否会在我每次切换页面时重新调用该订阅?我担心这会浪费服务器资源。

谢谢。

是的,当您切换页面时,先前的订阅将被删除,稍后当您浏览回该页面时,Meteor 会重新订阅。

大气中有一个缓存订阅的社区包:https://github.com/meteorhacks/subs-manager

因此,为了扩展这一点,我做了更多测试,看起来即使 Iron Router 每次切换页面时都尝试 运行 相同的 Router.configure 订阅,服务器似乎只运行一次发布函数(我在发布函数里面放了一个console.log看看)。

我认为这是由他们的文档解释的:

If you call Meteor.subscribe within a reactive computation, for example using Tracker.autorun, the subscription will automatically be cancelled when the computation is invalidated or stopped; it's not necessary to call stop on subscriptions made from inside autorun. However, if the next iteration of your run function subscribes to the same record set (same name and parameters), Meteor is smart enough to skip a wasteful unsubscribe/resubscribe.