how to not cache a page
Answered
American Chinchilla posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ChinchillaOP
i have a problem with a dynamic route segment
the problem i have so i need this behaviour is that it queries the database, and the WelcomeForm modifies the database for the given id im at (
import WelcomeForm from "@/components/WelcomeForm"
import { ensureGuildExists } from "@/lib/utils"
export default async function Page({
params,
}: {
params: { guildId: string }
}) {
const guild = await ensureGuildExists(params.guildId)
return <WelcomeForm guild={guild} />
}
i want the ensureGuildExists
function to run on each time i visit the page so its not caching what it was the first time i visited itthe problem i have so i need this behaviour is that it queries the database, and the WelcomeForm modifies the database for the given id im at (
guildId
in the url) so when its cached the form still displays all data even its updated already56 Replies
export const dynamic = 'force-dynamic';
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ChinchillaOP
yeah so i tried that already but did not seem to work like i wanted it to
i put a
console.log
inside the function to see when it triggersand it still does not log out when i visit same url twice
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Southern rough shrimp
Show the code
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ChinchillaOP
i can provide a video if you wish
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Southern rough shrimp
Which should work but isnt
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ChinchillaOP
import WelcomeForm from "@/components/WelcomeForm"
import { ensureGuildExists } from "@/lib/utils"
export const dynamic = "force-dynamic"
export default async function Page({
params,
}: {
params: { guildId: string }
}) {
const guild = await ensureGuildExists(params.guildId)
return <WelcomeForm guild={guild} />
}
export async function ensureGuildExists(guildId: string) {
console.log(guildId)
const guild =
(await prisma.guild.findUnique({
where: { id: guildId },
})) ||
(await prisma.guild.create({
data: {
id: guildId,
},
}))
return guild
}
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Southern rough shrimp
Are you running a dev server
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ChinchillaOP
yea
is that the reason
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Southern rough shrimp
Try stopping the server, deleting .next, and running again
then try
Also, use upsert instead of this weird query:
export async function ensureGuildExists(guildId: string) {
console.log(guildId);
const guild = await prisma.guild.upsert({
where: { id: guildId },
update: {},
create: {
id: guildId,
},
});
return guild;
}
export async function ensureGuildExists(guildId: string) {
console.log(guildId);
const guild = await prisma.guild.upsert({
where: { id: guildId },
update: {},
create: {
id: guildId,
},
});
return guild;
}
Separate to your issue but cleaner code
It means that only 1 query is run, you dont have to be awaiting multiple calls
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ChinchillaOP
hmm still doesnt seem to work, maybe it will in production
oh thanks
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Southern rough shrimp
Yeah try running a build and start
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ChinchillaOP
still a no
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Southern rough shrimp
What version of nextjs are you running
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ChinchillaOP
13.5.6
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Southern rough shrimp
Right upgrade to 14
Answer
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Southern rough shrimp
Would be my suggestion
It depends on how big the project is
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ChinchillaOP
only reason i did not when it came out is that i had some trouble with my code
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Southern rough shrimp
Or
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ChinchillaOP
but maybe the newest version works well
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Southern rough shrimp
Try adding this:
export const revalidate = 0;
export const revalidate = 0;
But either way, I would suggest keeping up to date with the latest nextjs version
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ChinchillaOP
eh gotta also upgrade nodejs
do i just download the installer again
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Southern rough shrimp
you on windows?
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ChinchillaOP
macos
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Southern rough shrimp
Installer here yeah
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ChinchillaOP
yeah newest nextjs version works well
only i have some depreciated config things
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Southern rough shrimp
Does it fix your issue?
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ChinchillaOP
yea
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Southern rough shrimp
Perfect
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ChinchillaOP
what do i mark solution tho
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Southern rough shrimp
What package manager are you using?
this
but also make sure to upgrade all your package.json dependencies
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ChinchillaOP
i use bun , but i still have npm and the lock files from it
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Southern rough shrimp
Keep em up to date
run a bun update
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ChinchillaOP
i did both bun install and npm install
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Southern rough shrimp
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ChinchillaOP
hm good to know
also
would you suggest droping the package-lock.json ?
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
Southern rough shrimp
I dont know if bun have their own lock file