Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react-meteor-accounts not working with SSR #364

Open
dnish opened this issue Aug 31, 2022 · 2 comments
Open

react-meteor-accounts not working with SSR #364

dnish opened this issue Aug 31, 2022 · 2 comments

Comments

@dnish
Copy link

dnish commented Aug 31, 2022

Hey,
when adding the useUserId() hook to my component, I get the following error:

Error running template: Error: Meteor.userId can only be invoked in method calls or publications.

We are running the latest Meteor version with React 18.

@Grubba27
Copy link
Contributor

Hey, @CaptainN, any thoughts on this one?
Also, @dnish, can you provide a snippet of code so that anyone who may find this same issue may relate? thx a lot for reporting 🙏

@dnish
Copy link
Author

dnish commented Aug 31, 2022

Hey @Grubba27,
yeah, we use the standard way for SSR. On our main.js we got this:

import {onPageLoad} from "meteor/server-render";
import { renderToString } from "react-dom/server";

onPageLoad(sink => {
    const htmlString = renderToString(<App sink={sink}/>);
    sink.renderIntoElementById("app-wrapper", htmlString);
});

I guess the main problem is that Meteor.userId() is only callable within the context of a publish or method. In this case, it is not available within the onPageLoad handler. This one gives an error:

onPageLoad(sink => {
    console.log(Meteor.userId());
    const htmlString = renderToString(<App sink={sink}/>);
    sink.renderIntoElementById("app-wrapper", htmlString);
});

I found a fix, because I was wondering why it worked on another application. If you use the FastRender package, it works perfectly.

import {onPageLoad} from "meteor/server-render";
import { renderToString } from "react-dom/server";
import { FastRender } from 'meteor/communitypackages:fast-render';

FastRender.onPageLoad(sink => {
    console.log(Meteor.userId()); // Works
    const htmlString = renderToString(<App sink={sink}/>);
    sink.renderIntoElementById("app-wrapper", htmlString);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants