AuthSession in T3 boiler plate not working when fetching data in SSG with tRPC

So when fetching data in getStaticProps, the docs instruct to use the SSG headers https://trpc.io/docs/ssg. Now doing this causes the file in server/common/get-server-auth-session to throw errors because the context will be undefined. What's the way to go around this
8 Replies
cje
cje2y ago
As far as I know it's fundamentally not possible SSG pages are generated at build time how would there be a session?
Tony
Tony2y ago
That's what kept me wondering, but the error occurs once I fetch data in getStaticProps Here is the code I use
export async function getStaticProps(context: GetStaticPropsContext) {
const ssg = createProxySSGHelpers({
router: appRouter,
ctx: await createContext(),
transformer: superjson,
});
await ssg.sanity.getReports.prefetch(context.params.slug as string);
return {
props: {
trpcState: ssg.dehydrate(),
},
revalidate: 10,
};
}
export async function getStaticProps(context: GetStaticPropsContext) {
const ssg = createProxySSGHelpers({
router: appRouter,
ctx: await createContext(),
transformer: superjson,
});
await ssg.sanity.getReports.prefetch(context.params.slug as string);
return {
props: {
trpcState: ssg.dehydrate(),
},
revalidate: 10,
};
}
cje
cje2y ago
im not on my computer now but i think you need to use the createContextInner function that's the one that's not dependent on req/res existing
Tony
Tony2y ago
aight let me look on it
cje
cje2y ago
Create T3 App
tRPC 🚀 Create T3 App
The best way to start a full-stack, typesafe Next.js app.
Tony
Tony2y ago
Yeah I have checked that and its included on the t3 boiler plate. But the error is occuring on this line return await unstable_getServerSession(ctx.req, ctx.res, authOptions); which comes from the file in server/common/get-server-auth-session. What am i missing
cje
cje2y ago
if you're still doing this ctx: await createContext(), the createContext function tries to get your session and there is no session because there is no req and res instead do ctx: await createContextInner({ session: null }),
Tony
Tony2y ago
Ooh shoot I guess that will fix the error
Want results from more Discord servers?
Add your server