getServerData Support #190
Replies: 5 comments 3 replies
-
Hello @graysonhicks 👋🏻 Great to hear that you liked the auth experience, even if this plugin is mainly focused on CSR until now :) Happy to say that updating this plugin to fully leverage the SSR capabilities of Clerk v3 that was released two weeks ago was on our roadmap already; the actual implementation would be starting next week. I think you're correct; the final solution could look very similar to It'd be great if you could give me a couple of days to run some tests internally and get back to you once I can share more details about the above. We always aim to provide the best possible DX, so could you please share the kind of public API (HOC vs. callback, and possible naming) you think would feel more natural to a Gatsby user? I believe you are just the right person to ask :) Gatsby is fantastic and we're more than happy to work on this plugin again |
Beta Was this translation helpful? Give feedback.
-
@nikosdouvlis Awesome to hear! I'm going to share this with the OSS team and will get you some feedback. |
Beta Was this translation helpful? Give feedback.
-
I think a minimal gatsby site using the Clerk plugin would be a good first step. Maybe this could be a good place to do that on a branch. That would make |
Beta Was this translation helpful? Give feedback.
-
Hello again! I just wanted to let you know that we'll be moving For
We still have to iron out some last details, but the proposed API looks like this: Plugin name:
Clerk configuration:
Clerk SSR helper:
Example: // 1. Read userId, sessionId, getToken from `auth`
export const getServerData: GetServerData = withServerAuth(async ({ auth }) => {
const { userId, sessionId, getToken } = auth;
const posts = userId ? await db.getPostsByUserId(userId) : [];
return { props: { posts } };
});
// 2. Get access to the whole `user` object
export const getServerData: GetServerData = withServerAuth(async ({ auth, user }) => {
const { userId, sessionId, getToken } = auth;
const data = user ? await serviceThatNeedsAnEmailAddress(user.emailAddresses[0].EmailAddress) : null;
return { props: { data } };
}, { loadUser: true });
// 3. Access Clerk state from the page component
const IndexPage = ({ serverData }) => {
// auth state
const { userId, sessionId, getToken } = useAuth();
const { user } = useUser();
// other data fetched during SSR
const { posts } = serverData;
return <div>...</div>;
}; I have to say, building Gatsby plugins is fun. I'd love to hear your thoughts on the API; any feedback is welcome :) PS: I've also converted the original issue into a discussion and moved it to the |
Beta Was this translation helpful? Give feedback.
-
Following my last comment, I have a couple of questions I'd like to ask. The Gatsby plugin system is very flexible, so I want to make sure we're not missing something. 1. Making
|
Beta Was this translation helpful? Give feedback.
-
First, this plugin is great! Probably the most seamless auth experience I've had in a while.
I'm looking to add support for SSR auth with Gatsby. Since Gatsby 4 in October 2021, Gatsby has supported SSR rendering pages outside of SSG. The pattern is similar to Next.js, but has a few differences. I have found that using the
withServerSideAuth
function basically works with Gatsby, but I'm not sure it's working as well as it could.First Gatsby relies on exporting an
async
function calledgetServerData
. That makes following the HOC pattern difficult. When trying to copy and paste the example directly, thecontext
will be undefined insidewithServerSideAuth
:The Gatsby pattern looks more like this:
With this pattern, the auth actually seems to work (it redirects to the auth url from Clerk), so I assume the Node logic from the Clerk library is working. When logged in, it successfully renders the page. But note that there is no callback to get the
sessionId
orresolvedUrl
.My guess is that this plugin could have an
ssr
folder that tweaks this same logic slightly and allows for SSR auth with Gatsby as well. Something like this:Or the callback could be passed in to
serverSideAuth
as well. Either way, I think the existing Next.js pattern is already very, very close to what would work here, with maybe additional tweaks to sanitizing/serializing props that might not be needed.Happy to help with this, thanks!
Beta Was this translation helpful? Give feedback.
All reactions