将Next.js应用程序部署到Vercel时,json响应正文错误无效
应用程序在本地构建良好,可在本地生产服务器+开发服务器上运行。 用于获取数据的api工作正常 产生错误的代码将Next.js应用程序部署到Vercel时,json响应正文错误无效,json,deployment,next.js,vercel,Json,Deployment,Next.js,Vercel,应用程序在本地构建良好,可在本地生产服务器+开发服务器上运行。 用于获取数据的api工作正常 产生错误的代码 export const getStaticProps = async () => { const resp = await fetch( 'https://cdn-api.co-vin.in/api/v2/admin/location/states', { headers: { 'Us
export const getStaticProps = async () => {
const resp = await fetch(
'https://cdn-api.co-vin.in/api/v2/admin/location/states',
{
headers: {
'User-Agent': '*',
}, //this is required by api provider
}
);
const data = await resp.json();
//console.log('states in getStaticProps : ', data);
return { props: { data: JSON.parse(JSON.stringify(data)) } };
};
const Ninjas = ({ data: { states } }) => {
console.log('Ninjas : ', states);
return (
<>
<Head>
<title> Listing</title>
</Head>
<div>
{states.map((ei) => (
<div key={ei.state_id}>
<a className={styles.single}>
<h3>{ei.state_name}</h3>
</a>
</div>
))}
</div>
</>
);
};
export default Ninjas;
export const getStaticProps=async()=>{
const resp=等待提取(
'https://cdn-api.co-vin.in/api/v2/admin/location/states',
{
标题:{
“用户代理”:“*”,
},//这是api提供程序所必需的
}
);
const data=wait resp.json();
//log('getStaticProps中的状态:',数据);
返回{props:{data:JSON.parse(JSON.stringify(data))};
};
常量忍者=({data:{states}})=>{
log('Ninjas:',states);
返回(
表册
{states.map((ei)=>(
{ei.state_name}
))}
);
};
输出默认忍者;
下面是vercel日志
20:17:40.644 Cloning github.com/user-name/co2-app (Branch: main, Commit: 8ab0e48)
20:17:41.349 Cloning completed: 704.592ms
20:17:41.374 Analyzing source code...
20:17:42.573 Installing build runtime...
20:17:45.235 Build runtime installed: 2.662s
20:17:48.104 Looking up build cache...
20:17:48.453 Build cache not found
20:17:49.697 Installing dependencies...
20:18:04.559 > ejs@2.7.4 postinstall /vercel/path0/node_modules/ejs
20:18:04.559 > node ./postinstall.js
20:18:05.113 npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.3.2 (node_modules/fsevents):
20:18:05.113 npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
20:18:05.116 added 600 packages from 352 contributors in 14.87s
20:18:05.426 64 packages are looking for funding
20:18:05.426 run `npm fund` for details
20:18:05.492 Detected Next.js version: 10.2.0
20:18:05.492 Running "npm run build"
20:18:05.756 > we2-cowax@0.1.0 build /vercel/path0
20:18:05.757 > next build
20:18:07.292 info - Using webpack 4. Reason: custom webpack configuration in next.config.js https://nextjs.org/docs/messages/webpack5
20:18:08.224 info - Checking validity of types...
20:18:08.244 Attention: Next.js now collects completely anonymous telemetry regarding usage.
20:18:08.245 This information is used to shape Next.js' roadmap and prioritize features.
20:18:08.245 You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
20:18:08.245 https://nextjs.org/telemetry
20:18:08.326 info - Creating an optimized production build...
20:18:08.748 > [PWA] Compile client (static)
20:18:08.748 > [PWA] Auto register service worker with: /vercel/path0/node_modules/next-pwa/register.js
20:18:08.750 > [PWA] Service worker: /vercel/path0/public/service-worker.js
20:18:08.750 > [PWA] url: /service-worker.js
20:18:08.750 > [PWA] scope: /
20:18:08.860 > [PWA] Compile server
20:18:25.603 info - Compiled successfully
20:18:25.604 info - Collecting page data...
20:18:26.366 info - Generating static pages (0/3)
20:18:26.918 error : invalid json response body at https://cdn-api.co-vin.in/api/v2/admin/location/states reason: Unexpected token < in JSON at position 0
20:18:26.919 Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
20:18:26.919 Error: Error serializing props returned from `getStaticProps` in "/".
20:18:26.919 Reason: Props must be returned as a plain object from getStaticProps: `{ props: { ... } }`.
20:18:26.919 at isSerializableProps (/vercel/path0/node_modules/next/dist/lib/is-serializable-props.js:1:462)
20:18:26.919 at renderToHTML (/vercel/path0/node_modules/next/dist/next-server/server/render.js:30:1730)
20:18:26.919 at processTicksAndRejections (internal/process/task_queues.js:93:5)
20:18:26.919 at async /vercel/path0/node_modules/next/dist/export/worker.js:26:6
20:18:26.919 at async Span.traceAsyncFn (/vercel/path0/node_modules/next/dist/telemetry/trace/trace.js:6:584)
20:18:26.920 info - Generating static pages (3/3)
20:18:26.921 > Build error occurred
20:18:26.926 Error: Export encountered errors on following paths:
20:18:26.926 /
20:18:26.926 at /vercel/path0/node_modules/next/dist/export/index.js:31:1106
20:18:26.926 at async Span.traceAsyncFn (/vercel/path0/node_modules/next/dist/telemetry/trace/trace.js:6:584)
20:18:26.927 at async /vercel/path0/node_modules/next/dist/build/index.js:43:49
20:18:26.927 at async Span.traceAsyncFn (/vercel/path0/node_modules/next/dist/telemetry/trace/trace.js:6:584)
20:18:26.927 at async /vercel/path0/node_modules/next/dist/build/index.js:25:1475
20:18:26.927 at async Span.traceAsyncFn (/vercel/path0/node_modules/next/dist/telemetry/trace/trace.js:6:584)
20:18:26.954 npm ERR! code ELIFECYCLE
20:18:26.954 npm ERR! errno 1
20:18:26.958 npm ERR! co2-app@0.1.0 build: `next build`
20:18:26.958 npm ERR! Exit status 1
20:18:26.958 npm ERR!
20:18:26.959 npm ERR! Failed at the we2-cowax@0.1.0 build script.
20:18:26.959 npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
20:18:26.968 npm ERR! A complete log of this run can be found in:
20:18:26.968 npm ERR! /vercel/.npm/_logs/2021-05-17T14_48_26_959Z-debug.log
20:18:26.982 Error: Command "npm run build" exited with 1
20:17:40.644克隆github.com/user-name/co2-app(分支:main,提交:8ab0e48)
20:17:41.349克隆完成:704.592ms
20:17:41.374正在分析源代码。。。
20:17:42.573正在安装生成运行时。。。
20:17:45.235安装的生成运行时:2.662s
20:17:48.104正在查找生成缓存。。。
20:17:48.453未找到生成缓存
20:17:49.697正在安装依赖项。。。
20:18:04.559 > ejs@2.7.4postinstall/vercel/path0/node_模块/ejs
20:18:04.559>节点。/postinstall.js
20:18:05.113 npm警告可选跳过可选依赖项:fsevents@2.3.2(节点\单元模块/fsevents):
20:18:05.113 npm WARN notsup跳过可选依赖项:不支持的平台fsevents@2.3.2:通缉{“os”:“darwin”,“arch”:“any”}(当前:{“os”:“linux”,“arch”:“x64”})
20:18:05.116在14.87秒内添加了来自352名贡献者的600个包
20:18:05.426 64个项目正在寻求资金支持
20:18:05.426运行“npm基金”了解详细信息
20:18:05.492检测到Next.js版本:10.2.0
20:18:05.492运行“npm运行构建”
20:18:05.756>we2-cowax@0.1.0build/vercel/path0
20:18:05.757>下一次生成
20:18:07.292信息-使用网页4。原因:next.config.js中的自定义网页包配置https://nextjs.org/docs/messages/webpack5
20:18:08.224信息-检查类型的有效性。。。
20:18:08.244注意:Next.js现在收集有关使用情况的完全匿名遥测数据。
20:18:08.245此信息用于塑造Next.js的路线图并确定功能的优先级。
20:18:08.245通过访问以下URL,您可以了解更多信息,包括如果您不想参与此匿名计划,如何选择退出:
20:18:08.245 https://nextjs.org/telemetry
20:18:08.326信息-创建优化的生产构建。。。
20:18:08.748>[PWA]编译客户端(静态)
20:18:08.748>[PWA]使用:/vercel/path0/node_modules/next PWA/register.js自动注册服务工作人员
20:18:08.750>[PWA]服务工作者:/vercel/path0/public/Service-worker.js
20:18:08.750>[PWA]url:/service-worker.js
20:18:08.750>[PWA]范围:/
20:18:08.860>[PWA]编译服务器
20:18:25.603信息-已成功编译
20:18:25.604信息-正在收集页面数据。。。
20:18:26.366信息-生成静态页面(0/3)
20:18:26.918错误:位于的json响应正文无效https://cdn-api.co-vin.in/api/v2/admin/location/states 原因:JSON中位置0处出现意外标记<
20:18:26.919预呈现页面“/”时出错。阅读更多:https://nextjs.org/docs/messages/prerender-error
20:18:26.919错误:序列化从“/”中的“getStaticProps”返回的道具时出错。
20:18:26.919原因:道具必须作为普通对象从getStaticProps:`{Props:{…}}}返回。
20:18:26.919在isSerializableProps(/vercel/path0/node_modules/next/dist/lib/is serializable props.js:1:462)
renderToHTML上的20:18:26.919(/vercel/path0/node_modules/next/dist/next server/server/render.js:30:1730)
20:18:26.919在处理和拒绝时(内部/process/task_queues.js:93:5)
20:18:26.919 at async/vercel/path0/node_modules/next/dist/export/worker.js:26:6
20:18:26.919异步Span.traceAsyncFn(/vercel/path0/node_modules/next/dist/telemetry/trace/trace.js:6:584)
20:18:26.920信息-生成静态页面(3/3)
20:18:26.921>发生生成错误
20:18:26.926错误:导出在以下路径上遇到错误:
20:18:26.926 /
20:18:26.926at/vercel/path0/node_modules/next/dist/export/index.js:31:1106
20:18:26.926异步Span.traceAsyncFn(/vercel/path0/node_modules/next/dist/telemetry/trace/trace.js:6:584)
20:18:26.927 at async/vercel/path0/node_modules/next/dist/build/index.js:43:49
20:18:26.927异步Span.traceAsyncFn(/vercel/path0/node_modules/next/dist/telemetry/trace/trace.js:6:584)
20:18:26.927 at async/vercel/path0/node_modules/next/dist/build/index.js:25:1475
20:18:26.927异步Span.traceAsyncFn(/vercel/path0/node_modules/next/dist/telemetry/trace/trace.js:6:584)
20:18:26.954 npm错误!代码失效循环
20:18:26.954 npm错误!错误1
20:18:26.958 npm错误!二氧化碳-app@0.1.0build:`下一个build`
20:18:26.958 npm错误!退出状态1
20:18:26.958 npm错误!
20:18:26.959 npm错误!在we2上失败-cowax@0.1.0构建脚本。
20:18:26.959 npm错误!这可能不是npm的问题。上面可能还有其他日志输出。
20:18:26.968 npm错误!此运行的完整日志可在以下位置找到:
20:18:26.968 npm错误/vercel/.npm/_logs/2021-05-17T14_48_26_959Z-debug.log
20:18:26.982错误:命令“npm运行生成”已退出,返回1
我正在使用下一个pwa来实现pwa功能,从而实现自定义网页版本
已在Vercel删除应用程序并重新部署,甚至已删除缓存构建,但仍无法解决错误
import Head from "next/head";
import isEmpty from "lodash.isempty";
export const getStaticProps = async () => {
let data = [];
let error = "";
try {
const res = await fetch(
"https://cdn-api.co-vin.in/api/v2/admin/location/states",
{
method: "GET",
headers: {
// update with your user-agent
"User-Agent":
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36",
Accept: "application/json; charset=UTF-8",
},
}
);
if (res.status !== 200)
throw String(`Invalid server response: ${res.status} ${res.statusText}`);
data = await res.json();
if (isEmpty(data)) throw String("No data was found!");
data = JSON.parse(JSON.stringify(data));
} catch (e) {
error = e.toString();
}
return {
props: {
data,
error,
},
};
};
const Ninjas = ({ data, error }) => (
<>
<Head>
<title> Listing</title>
</Head>
<div>
{error && <p style={{ color: "red" }}>{error}</p>}
{!isEmpty(data) && !isEmpty(data.states) &&
data.states.map((ei) => (
<div key={ei.state_id}>
<a className={styles.single}>
<h3>{ei.state_name}</h3>
</a>
</div>
))}
</div>
</>
);
export default Ninjas;