简体   繁体   English

NextJS SSR 功能在 Vercel 服务器上不起作用

[英]NextJS SSR functions doesn't work on Vercel Server

My pages which I use getStaticProps or getServerSideProps working on my localhost but I get Internal Server Error or build time error when I deploy to vercel.我使用 getStaticProps 或 getServerSideProps 的页面在我的本地主机上工作,但是当我部署到 vercel 时出现内部服务器错误或构建时间错误。

I searched everywhere for error logs.我到处搜索错误日志。 but I couldn't find any solution.但我找不到任何解决方案。

import React from 'react';
import {Wrapper} from "#layouts";
import axios from "axios";

function PrivacyPolicy({data}) {
    return (
       <Wrapper>
           {data &&
               <div className="conditions" dangerouslySetInnerHTML={{__html: data}} />
           }
       </Wrapper>
    );
}

export async function getStaticProps() {
    const settings = await axios.get(`${process.env.BASE_URL}/getSettings`).then(res => res.data);

    return { props: { data: settings?.confident_agree } }
}

export default PrivacyPolicy;

When I use getStaticProps (build time error):当我使用 getStaticProps (构建时间错误)时:

GET] /_next/data/xIyCYnTjWYvq1SkNn0EfE/privacy-policy.json
14:05:24:19
nt-length': '699',
      date: 'Wed, 20 Jul 2022 11:05:26 GMT',
      'alt-svc': 'quic=":443"; ma=2592000; v="43,46", h3-Q043=":443"; ma=2592000, h3-Q046=":443"; ma=2592000, h3-Q050=":443"; ma=2592000, h3-25=":443"; ma=2592000, h3-27=":443"; ma=2592000'
    },
    config: {
      transitional: [Object],
      adapter: [Function: httpAdapter],
      transformRequest: [Array],
      transformResponse: [Array],
      timeout: 0,
      xsrfCookieName: 'XSRF-TOKEN',
      xsrfHeaderName: 'X-XSRF-TOKEN',
      maxContentLength: -1,
      maxBodyLength: -1,
      env: [Object],
      validateStatus: [Function: validateStatus],
      headers: [Object],
      method: 'get',
      url: 'xxx/api/getSettings',
      data: undefined
    },
    request: <ref *1> ClientRequest {
      _events: [Object: null prototype],
      _eventsCount: 7,
      _maxListeners: undefined,
      outputData: [],
      outputSize: 0,
      writable: true,
      destroyed: false,
      _last: true,
      chunkedEncoding: false,
      shouldKeepAlive: false,
      _defaultKeepAlive: true,
      useChunkedEncodingByDefault: false,
      sendDate: false,
      _removedConnection: false,
      _removedContLen: false,
      _removedTE: false,
      _contentLength: 0,
      _hasBody: true,
      _trailer: '',
      finished: true,
      _headerSent: true,
      socket: [TLSSocket],
      _header: 'GET /api/getSettings HTTP/1.1\r\n' +
        'Accept: application/json, text/plain, */*\r\n' +
        'Access-Control-Allow-Origin: *\r\n' +
        'Access-Control-Allow-Credentials: true\r\n' +
        'Access-Control-Allow-Methods: GET,PUT,POST,DELETE\r\n' +
        'Content-Type: application/json\r\n' +
        'User-Agent: axios/0.27.2\r\n' +
        'Connection: close\r\n' +
        '\r\n',
      _keepAliveTimeout: 0,
      _onPendingData: [Function: noopPendingOutput],
      agent: [Agent],
      socketPath: undefined,
      method: 'GET',
      maxHeaderSize: undefined,
      insecureHTTPParser: undefined,
      path: '/api/getSettings',
      _ended: true,
      res: [IncomingMessage],
      aborted: false,
      timeoutCb: null,
      upgradeOrConnect: false,
      parser: null,
      maxHeadersCount: null,
      reusedSocket: false,
      host: 'popil.berataras.com',
      protocol: 'https:',
      _redirectable: [Writable],
      [Symbol(kCapture)]: false,
      [Symbol(kNeedDrain)]: false,
      [Symbol(corked)]: 0,
      [Symbol(kOutHeaders)]: [Object: null prototype]
    },
    data: '<!DOCTYPE html>\n' +
      '<html style="height:100%">\n' +
      '<head>\n' +
      '<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />\n' +
      '<title> 403 Forbidden\r\n' +
      '</title></head>\n' +
      '<body style="color: #444; margin:0;font: normal 14px/20px Arial, Helvetica, sans-serif; height:100%; background-color: #fff;">\n' +
      '<div style="height:auto; min-height:100%; ">     <div style="text-align: center; width:800px; margin-left: -400px; position:absolute; top: 30%; left:50%;">\n' +
      '        <h1 style="margin:0; font-size:150px; line-height:150px; font-weight:bold;">403</h1>\n' +
      '<h2 style="margin-top:20px;font-size: 30px;">Forbidden\r\n' +
      '</h2>\n' +
      '<p>Access to this resource on the server is denied!</p>\n' +
      '</div></div></body></html>\n'
  },
  page: '/privacy-policy'
}
RequestId: fd64c257-6f5c-46fa-b1d3-e2e8ef855bd7 Error: Runtime exited with error: exit status 1
Runtime.ExitError

and when I use getServerSideProps (just in page):当我使用 getServerSideProps (仅在页面中)时:

在此处输入图像描述

try this尝试这个

    const settings = await fetch(`${process.env.BASE_URL}/getSettings`, {
       method: 'get'
    });
    const result = await settings.json()

And check process.env.BASE_URL has the correct value并检查process.env.BASE_URL的值是否正确

PS you should have CORS enabled PS你应该启用CORS

P.S2 maybe your path should be /api/getSettings ? P.S2 也许你的路径应该是/api/getSettings

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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