[英]Route - Redirection (Next JS)
我正在將我的網站 (PHP) 移至 Next JS,但我對路線有疑問。
目前我有這種路由(來自 API 的唯一 ID 和 slug):
www.website.com/section/id_slug-of-post
我管理一些實現,例如:
www.website.com/section/id => 301 to www.website.com/section/id_slug-of-post
www.website.com/section/id_slug-with-wrong-words => 301 to www.website.com/section/id_slug-of-post
www.website.com/section/wrong-id => 301 to www.website.com/section
我可以用 Next JS 做同樣的事情還是應該使用 Express JS?
此類功能目前正在 Next.js GitHub 上的此 RFC中進行討論。但是,還有一種方法可以使用getServerSideProps中的res.writeHead
方法來執行此操作
export const getServerSideProps = (ctx) => {
ctx.res.writeHead(301, { Location: '/new/url' });
}
您還可以使用 next/router 提供的 Router 實例在客戶端重定向。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.