SSR and this.userId

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.

Is there a clean solution to fix this?

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.

Thanks @captainn I’ve read about the use of cookies. @minhna suggested the same thing.

Can you share your code for doing this?

Meteor store the login token in local storage:

  • Meteor.loginToken
  • Meteor.loginTokenExpires
  • Meteor.userId

You can read them and set the cookie value.

1 Like

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.

1 Like

You haven’t specified which view layer you are using, but if it’s React then you could check out

1 Like

This is really an important issue and needs to be resolved, please METEOR officials do something.
Maybe done and I do not know ?!

There have been many conversations on this issue, which I have listed below.
But I still have not found any relief !!!

1 - get current user-id on server-side outside publish (forums.meteor)

2- how to check user Login in ssr (forums.meteor)

3- ssr and this userid (forums.meteor)

4 - how to get meteor user to return on the server-side (stackoverflow)

5 - in meteor how to do server-side user login check (stackoverflow)

@filipenevola @storyteller and Other Frinds …
Isn’t Rahel really convincing?

How do we really render our components in SSR? Should we render based on the user logged in or not?

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?

it works in combination of and

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}).

1 Like

Does Fast Render support React-Router ?