How do I revalidatePath from a trpc router?

TRPCClientError: Invariant: static generation store missing in revalidateTag / I'm getting this error when I try to call revalidatePath from my trpc router.
import { z } from "zod";
import {
createTRPCRouter,
publicProcedure,
protectedProcedure,
} from "@/server/api/trpc";
import { formSchema } from "@/pages/admin";
import { revalidatePath } from "next/cache";

export const gigsRouter = createTRPCRouter({
getAll: publicProcedure.query(({ ctx }) => {
return ctx.prisma.gigs.findMany();
}),

create: protectedProcedure.input(formSchema).mutation(({ ctx, input }) => {
revalidatePath("/");

return ctx.prisma.gigs.create({
data: {
...input,
date: new Date(input.date),
},
});
}),

delete: protectedProcedure
.input(z.object({ id: z.string() }))
.mutation(({ ctx, input }) => {
revalidatePath("/");

return ctx.prisma.gigs.delete({ where: { id: input.id } });
}),
});
import { z } from "zod";
import {
createTRPCRouter,
publicProcedure,
protectedProcedure,
} from "@/server/api/trpc";
import { formSchema } from "@/pages/admin";
import { revalidatePath } from "next/cache";

export const gigsRouter = createTRPCRouter({
getAll: publicProcedure.query(({ ctx }) => {
return ctx.prisma.gigs.findMany();
}),

create: protectedProcedure.input(formSchema).mutation(({ ctx, input }) => {
revalidatePath("/");

return ctx.prisma.gigs.create({
data: {
...input,
date: new Date(input.date),
},
});
}),

delete: protectedProcedure
.input(z.object({ id: z.string() }))
.mutation(({ ctx, input }) => {
revalidatePath("/");

return ctx.prisma.gigs.delete({ where: { id: input.id } });
}),
});
Solution:
Ok solved it
Jump to solution
5 Replies
jackmcbride_98
jackmcbride_989mo ago
GitHub
Does tRPC support NextJS On-demand Revalidation? · trpc trpc · Disc...
Does tRPC support the NextJS On-demand Revalidation method which was added since version 12.2? or any other built-in apis?
jackmcbride_98
jackmcbride_989mo ago
Something like this
Solution
jackmcbride_98
jackmcbride_989mo ago
Ok solved it
jackmcbride_98
jackmcbride_989mo ago
/**
* This helper generates the "internals" for a tRPC context. If you need to use it, you can export
* it from here.
*
* Examples of things you may need it for:
* - testing, so we don't have to mock Next.js' req/res
* - tRPC's `createSSGHelpers`, where we don't have req/res
*
* @see https://create.t3.gg/en/usage/trpc#-serverapitrpcts
*/
const createInnerTRPCContext = (opts: CreateContextOptions) => {
return {
session: opts.session,
prisma,
res: opts.res,
};
};

/**
* This is the actual context you will use in your router. It will be used to process every request
* that goes through your tRPC endpoint.
*
* @see https://trpc.io/docs/context
*/
export const createTRPCContext = async (opts: CreateNextContextOptions) => {
const { req, res } = opts;

// Get the session from the server using the getServerSession wrapper function
const session = await getServerAuthSession({ req, res });

return createInnerTRPCContext({
session,
res,
});
};
/**
* This helper generates the "internals" for a tRPC context. If you need to use it, you can export
* it from here.
*
* Examples of things you may need it for:
* - testing, so we don't have to mock Next.js' req/res
* - tRPC's `createSSGHelpers`, where we don't have req/res
*
* @see https://create.t3.gg/en/usage/trpc#-serverapitrpcts
*/
const createInnerTRPCContext = (opts: CreateContextOptions) => {
return {
session: opts.session,
prisma,
res: opts.res,
};
};

/**
* This is the actual context you will use in your router. It will be used to process every request
* that goes through your tRPC endpoint.
*
* @see https://trpc.io/docs/context
*/
export const createTRPCContext = async (opts: CreateNextContextOptions) => {
const { req, res } = opts;

// Get the session from the server using the getServerSession wrapper function
const session = await getServerAuthSession({ req, res });

return createInnerTRPCContext({
session,
res,
});
};
Add this to your trpc.ts file Adn then you can do this in gigsRouter file
import { z } from "zod";
import {
createTRPCRouter,
publicProcedure,
protectedProcedure,
} from "@/server/api/trpc";
import { formSchema } from "@/pages/admin";

export const gigsRouter = createTRPCRouter({
getAll: publicProcedure.query(({ ctx }) => {
return ctx.prisma.gigs.findMany();
}),

create: protectedProcedure
.input(formSchema)
.mutation(async ({ ctx, input }) => {
const returnThis = await ctx.prisma.gigs.create({
data: {
...input,
date: new Date(input.date),
},
});

await ctx.res.revalidate("/");
return returnThis;
}),

delete: protectedProcedure
.input(z.object({ id: z.string() }))
.mutation(async ({ ctx, input }) => {
const returnThis = await ctx.prisma.gigs.delete({
where: { id: input.id },
});

await ctx.res.revalidate("/");
return returnThis;
}),
});
import { z } from "zod";
import {
createTRPCRouter,
publicProcedure,
protectedProcedure,
} from "@/server/api/trpc";
import { formSchema } from "@/pages/admin";

export const gigsRouter = createTRPCRouter({
getAll: publicProcedure.query(({ ctx }) => {
return ctx.prisma.gigs.findMany();
}),

create: protectedProcedure
.input(formSchema)
.mutation(async ({ ctx, input }) => {
const returnThis = await ctx.prisma.gigs.create({
data: {
...input,
date: new Date(input.date),
},
});

await ctx.res.revalidate("/");
return returnThis;
}),

delete: protectedProcedure
.input(z.object({ id: z.string() }))
.mutation(async ({ ctx, input }) => {
const returnThis = await ctx.prisma.gigs.delete({
where: { id: input.id },
});

await ctx.res.revalidate("/");
return returnThis;
}),
});
Jaaneek
Jaaneek5mo ago
Where did you get CreateNextContextOptions from?