Next.js Discord

Discord Forum

Is it possible to render a new page on same url?

Unanswered
Spectacled bear posted this in #help-forum
Open in Discord
Spectacled bearOP
Hi there, I need to show the /restricted page on the dashboard page when the user is not authorized. Is there a way where I can render a different page but the URL stays the same?

For example, show /restricted page but the URL should remain intact i.e
app.com/dashboard

5 Replies

@joulev sounds like you are looking for rewrites ([next.config.js](<https://nextjs.org/docs/app/api-reference/next-config-js/rewrites>), [middleware](<https://nextjs.org/docs/app/api-reference/functions/next-response#rewrite>))
Spectacled bearOP
I don't think that matches the requirement. I want to handle this config inside the page itself since it is a conditional thing.
then i doubt it is possible, no.

unless you do like
import RestrictedPage from "~/app/restricted/page";

if (restricted) return <RestrictedPage />

but then if you do this it's better to make the RestrictedPage an ordinary component rather than a page (and get rid of /restricted altogether)