【问题标题】:How to redirect pages/app folder to subdomain in next.js如何将页面/应用文件夹重定向到 next.js 中的子域
【发布时间】:2020-08-28 21:12:55
【问题描述】:

我在互联网上搜索了很多,但我没有找到任何相关的文章。

就像我在项目的根目录中有一个名为 pages 的文件夹,树下面是它的文件。

|   404.js
|   auth.js
|   index.js
|   _app.js
|   _error.js
\---app
        index.js

next.js 在有人打开project.local:3000 时提供默认行为,它将打开index.jsproject.local:3000/app,它将打开app/index.js,但我希望当有人打开app.project.local:3000 时,它将打开app/index.js

我的主机文件

127.0.0.1 project.local
127.0.0.1 app.project.local

简而言之

我想在 next.js 中将 pages/app 文件夹重定向到 app.project.localapp.example.com

【问题讨论】:

    标签: javascript reactjs redirect next.js


    【解决方案1】:

    NextJS 现在支持语言环境:https://nextjs.org/docs/advanced-features/i18n-routing

    您可以在配置中指定语言环境,例如admin 并像这样指定 URL:

    // next.config.js
    module.exports = {
      i18n: {
        // These are all the locales you want to support in
        // your application
        locales: ['en-US', 'admin', 'nl-NL'],
        // This is the default locale you want to be used when visiting
        // a non-locale prefixed path e.g. `/hello`
        defaultLocale: 'en-US',
        // This is a list of locale domains and the default locale they
        // should handle (these are only required when setting up domain routing)
        // Note: subdomains must be included in the domain value to be matched e.g. "fr.example.com".
        domains: [
          {
            domain: 'example.com',
            defaultLocale: 'en-US',
          },
          {
            domain: 'example.nl',
            defaultLocale: 'nl-NL',
          },
          {
            domain: 'admin.example.com',
            defaultLocale: 'admin',
            // an optional http field can also be used to test
            // locale domains locally with http instead of https
            http: true,
          },
        ],
      },
    }
    

    【讨论】:

      【解决方案2】:

      最新解决方案

      我在浏览 redirects 上的文档时找到了解决方案。

      在 Next.js 的根文件夹中,创建一个 vercel.json 文件,然后将重定向作为对象插入到 redirects 数组中,如下所示:

      {
        "redirects": [
          { "source": "/blog", "destination": "https://blog.example.com" }
        ]
      }
      
      

      这仅适用于生产环境。它应该按预期工作。

      【讨论】:

        【解决方案3】:

        我仍然是 next.js 的菜鸟(第二天学习),但我正在寻找子域支持,我在这个 Github 问题上找到了三个解决方案:https://github.com/vercel/next.js/issues/5682

        1. 使用区域(仍然不知道它是如何工作的)
        2. “Vercel 近期会实现子域路由”(我预计近期不会使用 Vercel)
        3. (我的首选,但尚未测试)在 Next.js 中使用自定义服务器的示例:https://github.com/dcangulo/nextjs-subdomain-example 对于 #3,请参阅server.js 文件中的实现方式

        【讨论】:

        • 在测试选项 3 后,我意识到我们失去了“自动静态优化”功能 (nextjs.org/docs/advanced-features/custom-server) 此外,即使使用 getServerSideProps,页面第一次加载也需要一些时间给定的子域
        • 忽略我最后关于速度的评论。这是 OSX 上的 localhost DNS 解析问题。我通过在一行中添加所有 127.0.0.1 来修复它(请参阅superuser.com/questions/1189379/…
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-15
        • 1970-01-01
        • 2013-12-03
        • 1970-01-01
        • 1970-01-01
        • 2023-03-04
        相关资源
        最近更新 更多