Next.js Discord

Discord Forum

how to not cache a page

Answered
American Chinchilla posted this in #help-forum
Open in Discord
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 Bighead carp
Right upgrade to 14
View full answer

56 Replies

export const dynamic = 'force-dynamic';
@Bighead carp https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic
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
Bighead carp
Show the code
American ChinchillaOP
i can provide a video if you wish
Bighead carp
Which should work but isnt
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
}
Bighead carp
Are you running a dev server
American ChinchillaOP
yea
is that the reason
Bighead carp
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
@Bighead carp Try stopping the server, deleting .next, and running again
American ChinchillaOP
hmm still doesnt seem to work, maybe it will in production
Bighead carp
Yeah try running a build and start
@Bighead carp Yeah try running a build and start
American ChinchillaOP
still a no
Bighead carp
What version of nextjs are you running
American ChinchillaOP
13.5.6
Bighead carp
Right upgrade to 14
Answer
Bighead carp
Would be my suggestion
It depends on how big the project is
American ChinchillaOP
only reason i did not when it came out is that i had some trouble with my code
Bighead carp
Or
American ChinchillaOP
but maybe the newest version works well
Bighead carp
Try adding this:

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