为什么我的自定义 Nextjs 服务器无法在我的 Vercel 部署中工作?

2023-12-25

您好,我有这个存储库用于测试。在本地主机中,当我执行“npm run dev”时,我可以看到我的自定义服务器...根目录下的 server.js 会进行重定向(如果存在我的自定义 cookie)。 但是在 Vercel 的生产中我看不到这个自定义服务器正在工作(我可以看到,因为在 Server.js 中我添加了一个名为“n-session”、值为“1”的会话 Cookie,但在生产中不起作用(也登录Server.js 不会在 Server.js 中显示此跟踪)。

在我的 package.json 中我可以看到:

    "scripts": {
        "dev": "node server.js",
        "build": "next build",
        "start": "NODE_ENV=production node server.js",
        "export": "next export",
      },

我的 server.js 有以下代码:

    // server.js
    const { createServer } = require('http')
    const { parse } = require('url')
    const next = require('next')
    
    const app = next({})
    const handle = app.getRequestHandler()
    
    var Cookies = require('cookies');
    
    const { localeLanguages } = require('next/config').default().publicRuntimeConfig;
    
    app.prepare().then(() => {
      createServer((req, res) => {
        // Be sure to pass `true` as the second argument to `url.parse`.
        // This tells it to parse the query portion of the URL.
        const parsedUrl = parse(req.url, true);
        const { pathname } = parsedUrl;
    
        console.log("pathname", pathname)
        if (pathname === '/') {
          console.log("pathname is root..................")
          const mainLang = process.env.NEXT_PUBLIC_MAIN_LANG;  
    
          let uriRedirect = null;
    
          if (req && req.headers) 
          {
            console.log("req.headers", req.headers)
            const cookies = new Cookies(req, res);
        
            let userLang = mainLang;  
            let userLangCookie = cookies.get(process.env.NEXT_PUBLIC_USER_LANGUAGE_COOKIE);
            let initSession = cookies.get(process.env.NEXT_PUBLIC_INIT_SESION_COOKIE);
            
            console.log("userLangCookie", userLangCookie)
            console.log("initSession", initSession)
    
            let acceptLanguage = req.headers['accept-language']; 
            if (acceptLanguage) {  
              acceptLanguage = (acceptLanguage.split(',')[0]).split('-')[0];
    
              let found = localeLanguages.filter(function (e) {
                return e.label == acceptLanguage;
              });
              if (found.length > 0) {
                userLang = acceptLanguage;
              }
    
              if (typeof initSession === "undefined" || initSession === null)
              {
                if (typeof userLangCookie === "undefined" || 
                  userLangCookie === null && userLang !== mainLang) 
                {
                  uriRedirect = `/${userLang}`;
                }
                else if (userLangCookie !== mainLang)
                {
                  uriRedirect = `/${userLangCookie}`;
                }
                cookies.set(process.env.NEXT_PUBLIC_INIT_SESION_COOKIE, 1, {
                  httpOnly: true // true by default
                })
              }
            } 
          }
          console.log("uriRedirect", uriRedirect)
          
          if (uriRedirect !== null) { 
              res.writeHead(302, { Location: `${uriRedirect}` }).end(); 
          } else {
            handle(req, res, parsedUrl);
          }
        } else {
          handle(req, res, parsedUrl);
        }
      }).listen(3000, (err) => {
        if (err) throw err
        console.log('> Ready on http://localhost:3000')
      })
    })

我使用我的 github 存储库和 git 命令 add/commit/push 上传到 Vercel

我的存储库是这样的:https://github.com/anialamo/nootric-next10 https://github.com/anialamo/nootric-next10

你能帮我吗?我的部署出了什么问题?

我的 server.js 文件应该托管在哪里?也在韦尔塞尔? 非常感谢!


在使用自定义服务器之前,您必须阅读文档以了解其缺点

它指出:

“自定义服务器无法部署在 Vercel 上,而 Vercel 是 Next.js 的专用平台。

在决定使用自定义服务器之前,请记住,仅当 Next.js 的集成路由器无法满足您的应用程序要求时才应使用它。自定义服务器将消除重要的性能优化,例如无服务器功能和自动静态优化。”

参考 https://nextjs.org/docs/advanced-features/custom-server

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么我的自定义 Nextjs 服务器无法在我的 Vercel 部署中工作? 的相关文章

随机推荐