Next.js Discord

Discord Forum

revalidatePath() and redirect() without using strings?

Answered
Damien Cassou posted this in #help-forum
Open in Discord
The functions revalidatePath() (from next/cache) and redirect() (from next/navigation) take a string as argument. For example (from the official tutorial):

  revalidatePath('/dashboard/invoices');
  redirect('/dashboard/invoices');


The string as argument is a bit annoying because it means duplicating the paths in many places of the application without any refactoring support. Also, there is absolutely no validation that the path actually exists. The problem is the same for Link (from next/link). Would it be possible to pass a Page component instead? Something like:

import InvoicesPage from "@/app/dashboard/invoices/page.tsx";
...
revalidatePath(InvoicesPage);
redirect(InvoicesPage);


This is not a final solution (e.g., "@/app/dashboard/invoices/[id]/edit/page.tsx" isn't enough to know which invoice "id" should be used) but just an idea. Given that Next.js already knows how to map files to routes, I guess something like that should be possible to implement.
Answered by Damien Cassou
View full answer

2 Replies

possible to implement? maybe yes. is it already implemented? certainly not, so you can't do it now. the team isn't active here so i suggest opening an issue over the nextjs repo for a feature request
Answer