简体   繁体   中英

NextJS index and endpoint share the same page

I have few pages in my Next.js application, and i would like that going into http://localhost:3000/someEndpoint would be exactly the same component loaded as http://localhost:3000/

I already have created index.js file, but i dont know how can make some virtual URL?

Redirections are not the options, cause i would like to remain "/" endpoint also possible.

Is there any way to accomplish this?

You can use nextjs rewrites , which works as a URL masking for any URL.

module.exports = {
  async rewrites() {
    return [
      {
        source: '/someEndpoint',
        destination: '/',
      },
    ]
  },
}

So now every request on http://localhost:3000/someEndpoint will lands the user to http://localhost:3000/ without redirection. It will work as a proxy url

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM