kadirahq / flow-router

Carefully Designed Client Side Router for Meteor
MIT License
1.09k stars 194 forks source link

[SSR] SSR and Counts #533

Closed SachaG closed 8 years ago

SachaG commented 8 years ago

I'm using the publish-counts package to publish a count of how many total posts exist on the server to the client.

Publish-counts only works on the client, so it doesn't work when called server-side with FlowRouter-SSR. So I decided to do this instead:

const totalCount = Meteor.isClient ? Counts.get("totalPostsCount") : Posts.find().count();

But it seems like even when called on the server, Posts.find() returns the data that would be published to the client, not the total data available server-side. Is this expected behavior? Or am I mistaken?

arunoda commented 8 years ago

@SachaG that's a feature. In the server (with SSR) you can't access to the whole database. You only have access to the data fetched from publications.

I assume, you can can Counts.get from the server as well. I'm doing a blog post today, you'll get more info on designing a SSR app with that.

SachaG commented 8 years ago

OK, that's what I thought. Any way to bypass this? Maybe Posts.findAll() or something? Anyway I'll wait for the blog post (but apart from that, SSR now works in Telescope Nova!)

And no, Counts.get doesn't work on the client. See https://github.com/percolatestudio/publish-counts/issues/80

arunoda commented 8 years ago

@SachaG You shouldn't bypass that. That's the idea. In the server, SSR is also just like the client for data.

SachaG commented 8 years ago

I understand the principle, but how else can I deal with this issue? Writing Counts.get("myCount") on the server doesn't make sense, because myCount only gets registered inside the publication – which is not called on the server… So it sounds like the publish-count package would need to be refactored quite a bit, but in the meantime I was hoping there would be an easier fix.

(Also ideally I'd prefer avoiding using Meteor.isClient, so I agree with you)

arunoda commented 8 years ago

Actually, it don't need much refactoring. Just run this code on the server as well. See: https://github.com/percolatestudio/publish-counts/blob/master/client/publish-counts.js

Then Counts.get also works on the SSR context. That's why we build FlowRouter SSR in the first place :)

SachaG commented 8 years ago

I might be wrong but I thought @tmeasday was saying it's not that simple?

arunoda commented 8 years ago

That's possible because we created that :)

arunoda commented 8 years ago

I hope we can close this since we are clear with how FlowRouter SSR do things.