Next.js Discord

Discord Forum

how to not cache a page

Answered
American Chinchilla posted this in #help-forum
Open in Discord
Avatar
American ChinchillaOP
i have a problem with a dynamic route segment
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 it

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 (guildId in the url) so when its cached the form still displays all data even its updated already
Answered by Southern rough shrimp
Right upgrade to 14
View full answer

56 Replies

export const dynamic = 'force-dynamic';
Avatar
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 triggers
and it still does not log out when i visit same url twice
Avatar
Southern rough shrimp
Show the code
Avatar
American ChinchillaOP
i can provide a video if you wish
Avatar
Southern rough shrimp
Which should work but isnt
Avatar
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
Southern rough shrimp
Are you running a dev server
Avatar
American ChinchillaOP
yea
is that the reason
Avatar
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;
}
Separate to your issue but cleaner code
It means that only 1 query is run, you dont have to be awaiting multiple calls
Avatar
American ChinchillaOP
hmm still doesnt seem to work, maybe it will in production
oh thanks
Avatar
Southern rough shrimp
Yeah try running a build and start
Avatar
American ChinchillaOP
still a no
Avatar
Southern rough shrimp
What version of nextjs are you running
Avatar
American ChinchillaOP
13.5.6
Avatar
Southern rough shrimp
Right upgrade to 14
Answer
Avatar
Southern rough shrimp
Would be my suggestion
It depends on how big the project is
Avatar
American ChinchillaOP
only reason i did not when it came out is that i had some trouble with my code
Avatar
Southern rough shrimp
Or
Avatar
American ChinchillaOP
but maybe the newest version works well
Avatar
Southern rough shrimp
Try adding this:

export const revalidate = 0;
But either way, I would suggest keeping up to date with the latest nextjs version
Avatar
American ChinchillaOP
eh gotta also upgrade nodejs
do i just download the installer again
Avatar
Southern rough shrimp
you on windows?
Avatar
American ChinchillaOP
macos
Avatar
Southern rough shrimp
Installer here yeah
Avatar
American ChinchillaOP
yeah newest nextjs version works well
only i have some depreciated config things
Avatar
Southern rough shrimp
Does it fix your issue?
Avatar
American ChinchillaOP
yea
Avatar
Southern rough shrimp
Perfect
Avatar
American ChinchillaOP
what do i mark solution tho
Avatar
Southern rough shrimp
What package manager are you using?
this
but also make sure to upgrade all your package.json dependencies
Avatar
American ChinchillaOP
i use bun , but i still have npm and the lock files from it
Avatar
Southern rough shrimp
Keep em up to date
run a bun update
Avatar
American ChinchillaOP
i did both bun install and npm install
Avatar
Southern rough shrimp
Avatar
American ChinchillaOP
hm good to know
also
would you suggest droping the package-lock.json ?
Avatar
Southern rough shrimp
I dont know if bun have their own lock file