Next.js Discord

Discord Forum

pop current page from history if user goes back, user goes cart to checkout, back to cart.

Unanswered
hareshFF posted this in #help-forum
Open in Discord
nextjs 14, how to remove /checkout from history so if user click back button to go to /cart, he wont be allowed to click -> button to forward to checkout. he should only click the checkout ui button.

4 Replies

Sun bear
checkout this: https://nextjs.org/docs/app/building-your-application/routing/linking-and-navigating

import { useRouter} from "next/navigation"

const router = useRouter()

// this might be what you want
router.replace("/checkout")
since router.replace does not add /checkout to history
@Sun bear hi, im not replacing because user should be able to go back to cart from checkout, but then i need to disable browser forward -> button, so user is forced to click [go to checkout] UI button if they want to go to /checkout route. i need to delete forward history if user is in cart.