ssg doesn't work for new data

When a user creates a new post, it doesn't generate a new site. I thought I could achieve that through revalidate but it doesn't seem to work. This is the code of post/[id].tsx:
export const getStaticPaths: GetStaticPaths = async () => {
const posts = await prisma.post.findMany();
// TODO: this can cause problems if we run this the first time the database is reset
if (posts) {
return {
paths: posts.map((post) => ({
params: {
id: post.id,
},
})),
fallback: false,
};
} else {
return {
paths: [],
fallback: false,
};
}
};

export const getStaticProps = async (
// this is the id from the page
context: GetStaticPropsContext<{ id: string }>
) => {
const ssg = createProxySSGHelpers({
router: appRouter,
transformer: superjson,
ctx: await createContextInner({ session: null }),
});

await ssg.post.getAllPosts.prefetch();

return {
props: {
trpcState: ssg.dehydrate(),
id: context.params?.id,
},
revalidate: 1,
};
};
export const getStaticPaths: GetStaticPaths = async () => {
const posts = await prisma.post.findMany();
// TODO: this can cause problems if we run this the first time the database is reset
if (posts) {
return {
paths: posts.map((post) => ({
params: {
id: post.id,
},
})),
fallback: false,
};
} else {
return {
paths: [],
fallback: false,
};
}
};

export const getStaticProps = async (
// this is the id from the page
context: GetStaticPropsContext<{ id: string }>
) => {
const ssg = createProxySSGHelpers({
router: appRouter,
transformer: superjson,
ctx: await createContextInner({ session: null }),
});

await ssg.post.getAllPosts.prefetch();

return {
props: {
trpcState: ssg.dehydrate(),
id: context.params?.id,
},
revalidate: 1,
};
};
It works in dev mode (due to dev mode), but it doesn't work in production (vercel)
49 Replies
Johnson
JohnsonOP3y ago
the not generated sites just 404
barry
barry3y ago
I doubt the revalidation works properly locally but it’s set to 1 second lol
Johnson
JohnsonOP3y ago
I assume I should make that a bit bigger?
barry
barry3y ago
If it updates every second that’s fine 😂
Johnson
JohnsonOP3y ago
well idk if it's the revalidate, but it does work locally, just takes a long time to load for some reason
barry
barry3y ago
Revalidation isn’t enabled for development
Johnson
JohnsonOP3y ago
ohhhh
barry
barry3y ago
It’s just a normal request every time
Johnson
JohnsonOP3y ago
u think changing revalidate will fix the problem? I might not understand correctly
barry
barry3y ago
It’s just x seconds
Johnson
JohnsonOP3y ago
revalidate generally makes all the sites that have not been generated so far, generate, right? At least that's how I thought of it
barry
barry3y ago
You’re just passing x seconds It will only re run the code if there has been more than x seconds since last “run” Else it will just return a cached response
Johnson
JohnsonOP3y ago
yeah okay
barry
barry3y ago
There’s also on demand revalidation Haven’t tried it myself though
Johnson
JohnsonOP3y ago
yeah I read about that, but I really wonder why the current code won't generate the new site
barry
barry3y ago
Errors? And how do you know it isn t
Johnson
JohnsonOP3y ago
after all, if validate is 1, it should try every second I guess well I get a 404 when I try to connect and when I re-deploy it works
barry
barry3y ago
Server error?
Johnson
JohnsonOP3y ago
I can check vercels functions, but I don't think there is anything in there
barry
barry3y ago
It doesn’t just 404 😂 If it did without erroring I would be scared
Johnson
JohnsonOP3y ago
maybe its not an error directly, but the code itself is just wrong one sec
barry
barry3y ago
Your paths are wrong You just passed [] Oh nvm phone view cursed Debug time
Johnson
JohnsonOP3y ago
I get this, but this is just because I check for auth each time a user conects
Johnson
JohnsonOP3y ago
so no, vercel doesn't really give me any errors, so I think the code is just wrong
barry
barry3y ago
What if you build and run locally
Johnson
JohnsonOP3y ago
one sec same thing so its not vercel, its the code
barry
barry3y ago
No errors either?
Johnson
JohnsonOP3y ago
console is clear
barry
barry3y ago
Put on GitHub I clone when I get home
Johnson
JohnsonOP3y ago
ke
Johnson
JohnsonOP3y ago
GitHub
GitHub - vothvovo/nextjsdebug: oof
oof. Contribute to vothvovo/nextjsdebug development by creating an account on GitHub.
Johnson
JohnsonOP3y ago
done I put everything I know inside the readme.md
barry
barry3y ago
@Johnson Deployed it myself, nothing wrong. https://nextjsdebug.vercel.app/post/cl9mucr7c0000ju09ja9mm51o As you can see, I put revalidation on 1 minute, the ISO string which is a date only changes if a minute has passed. No problems.
Johnson
JohnsonOP3y ago
wut
barry
barry3y ago
Don't know what you did wrong lol Forgot to add env variables on Vercel's config?
Johnson
JohnsonOP3y ago
I just logged in and created a new post click on it it will give you a 404
barry
barry3y ago
lol your discord account fucked up
Johnson
JohnsonOP3y ago
wut
barry
barry3y ago
Oh I get why lol Lemme test something Yh you can't do it this way
Johnson
JohnsonOP3y ago
fyi the sites do generate if you re-deploy
barry
barry3y ago
getStaticPaths only runs at build time.
Johnson
JohnsonOP3y ago
yeah but I thought
barry
barry3y ago
So the routes don't exist Which is the problem
Johnson
JohnsonOP3y ago
through revalidate I can re-run that damn
barry
barry3y ago
It does but not getStaticPaths
Johnson
JohnsonOP3y ago
ohh so I only run getStaticProps on revalidate well how do I solve this problem then
barry
barry3y ago
csr / ssr
Johnson
JohnsonOP3y ago
can I implement some kind of logic that makes sure to only run ssr when there is no site available(so basically I haven't re-deployed yet)?
Want results from more Discord servers?
Add your server