I’ve got a UI problem. When I use SSR to render a public page, there doesn’t appear to be a way to check if the user is logged in or not. As a result the navigation bar always renders with login/register links and then after the client side loads, which is a split second later, if the user is logged in the navbar updates accordingly. This isn’t a huge issue, it just doesn’t look very clean.
With my knowledge, there is no solution. Maybe somehow you store the token in cookie and try to read it in server side.
But, I use SSR for SEO purpose only, then it’s not the problem.
My solution is to skip SSR for authenticated users, figuring they are going to stick around, even with a slower page load (maybe this is a faulty assumption, but it’s what I do). What I do is set a cookie (called sssr - skip server side rendering) on sign in, and unset it on sign out. In my server-render script, I simply checks that, and doesn’t bother rendering on the server if it’s set.
I haven’t actually done this, but there is a package called fast-render which has done it. Take a look at the source code there for some guidance. Most of what you need is probably in auth.js.
In ssr, I think we should check that the user is logged in to his request, and in fact when every user requests to the server, the components should be rendered according to his request. That is, in a request, the server renders the components according to the login of that request (user).
@minhna@captainn@copleykj@bp123 in fact, our main problem is how to check the user’s login in each request to the server? How can we check this and render the components for that request?
fast-render does provider userId in server side calls Meteor.subscribe (and does set Meteor.userId() AFAIR).
That beeing said, you should be careful whether you want to SSR a component that depends on the user, because it destroys every caching attempt. In next.js you might even lazy load and exclude components from SSR that show user based information (through dynamic(loader, {ssr: false}).