为啥我的自定义 Nextjs 服务器在我的 Vercel 部署中不起作用?

Posted

技术标签:

【中文标题】为啥我的自定义 Nextjs 服务器在我的 Vercel 部署中不起作用?【英文标题】:Why my custom Nextjs server doesn't work in my Vercel deployment?为什么我的自定义 Nextjs 服务器在我的 Vercel 部署中不起作用? 【发布时间】:2021-03-25 03:07:13 【问题描述】:

您好,我有这个存储库用于测试。在本地主机中,我可以看到当我执行“npm run dev”我的自定义服务器时......如果存在我的自定义 cookie,root 的 server.js 会进行重定向。 但是在 Vercel 的生产中,我看不到这个自定义服务器正在运行(我可以看到,因为在 Server.js 中我添加了一个名为“n-session”的会话 Cookie,值为“1”,但在生产中不起作用(也登录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

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

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

【问题讨论】:

【参考方案1】:

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

它说明了这一点:

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

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

Reference

【讨论】:

好的!谢谢!你知道以防万一吗..如果我将静态 html 与 StaticProps 一起使用,但我需要在我的 IndexPage 中,我如何从我的索引页面进行重定向?我使用了 ServerSideProps,这很好用!但是当我执行xml-sitemaps.com 为我的网站进行爬网时。我还在 Nextjs 10 中使用 i18n ......当我用我的第二语言抓取我的网站时,抓取工具不会给我抓取结果是错误的。出于这个原因,在我的本地主机中,我使用了自定义 server.js,因为 getServerSideProps 不允许我拥有一个适用于爬虫的静态站点。有什么想法吗?

以上是关于为啥我的自定义 Nextjs 服务器在我的 Vercel 部署中不起作用?的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的自定义按钮的值不能保存在我的数据库中?

Sencha Touch - 为啥在我的自定义组件中未定义此功能?

为啥具有“用户”范围级别的自定义维度未显示在我的报告中?

为啥在我的自定义 UIView 类中没有调用 init(frame: CGRect)?

SwiperJS 样式不适用于 NextJS

为啥我的自定义 Swift 类没有转移到我的 GameScene [关闭]