next.js - 尝试部署 Next.js 应用程序时出现“仅支持绝对 URL”错误
问题描述
我在绑定和TypeError: Only absolute URLs are supported
部署 Next.js 应用程序时遇到错误。在我的应用程序中,我有一个文件可以避免错误,因为数据是从服务器获取的。Vercel
Netlify
config
absolute path
config.js
const dev = process.env.NODE_ENV !== "production";
export const server = dev
? "http://localhost:3000"
: "https://next-js-next-app-pjjs4ka8d-grf.vercel.app";
vercel
在开始部署之前生成随机域时我选择的域。但我想这是错误的,我必须提供另一个 url 进行部署。
这是我在文件中使用server
应用程序中的 url 的方式。config
pages/article[id]
export const getStaticProps = async (context) => {
const res = await fetch(`${server}/api/articles/${context.params.id}`);
const article = await res.json();
return {
props: {
article,
},
};
};
export const getStaticPaths = async () => {
const res = await fetch(`${server}/api/articles/`);
const articles = await res.json();
const ids = articles.map((article) => article.id);
const paths = ids.map((id) => ({ params: { id: id.toString() } }));
return {
fallback: "blocking",
paths: paths,
};
};
Vercel
尝试部署时输出错误
10:11:04.813 > Build error occurred
10:11:04.815 TypeError: Only absolute URLs are supported
10:11:04.815 at getNodeRequestOptions (/vercel/path0/node_modules/node-fetch/lib/index.js:1305:9)
10:11:04.815 at /vercel/path0/node_modules/node-fetch/lib/index.js:1410:19
10:11:04.815 at new Promise (<anonymous>)
10:11:04.815 at fetch (/vercel/path0/node_modules/node-fetch/lib/index.js:1407:9)
10:11:04.815 at getStaticPaths (/vercel/path0/.next/server/pages/article/[id].js:168:21)
10:11:04.815 at buildStaticPaths (/vercel/path0/node_modules/next/dist/build/utils.js:16:86)
10:11:04.815 at /vercel/path0/node_modules/next/dist/build/utils.js:26:628
10:11:04.815 at processTicksAndRejections (internal/process/task_queues.js:93:5)
10:11:04.815 at async Span.traceAsyncFn (/vercel/path0/node_modules/next/dist/telemetry/trace/trace.js:6:584) {
10:11:04.815 type: 'TypeError'
10:11:04.815 }
10:11:04.834 npm ERR! code ELIFECYCLE
10:11:04.834 npm ERR! errno 1
10:11:04.838 npm ERR! traversytutorial@0.1.0 build: `next build && next export`
10:11:04.838 npm ERR! Exit status 1
10:11:04.839 npm ERR!
10:11:04.839 npm ERR! Failed at the traversytutorial@0.1.0 build script.
10:11:04.839 npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
10:11:04.846 npm ERR! A complete log of this run can be found in:
10:11:04.847 npm ERR! /vercel/.npm/_logs/2021-06-18T08_11_04_839Z-debug.log
10:11:04.859 Error: Command "npm run build" exited with 1
任何帮助将不胜感激
解决方案
推荐阅读
- haskell - 毕达哥拉斯树在 2 级及以上失败,为什么?
- java - spring web starter中路径变量的自定义反序列化
- html - CSS ::before 中的“重加号”➕?
- android - 使用 camera2 进行人脸检测仅适用于特定方向(横向)
- windows - WinAPI 文档在哪里?
- javascript - 简单 JavaScript“Web 应用程序”中的问题
- c - Windows GDI:位图的图案画笔和 BitBlt 之间的区别
- java - 在数据存储视图中找不到使用 objectify 持久化的实体
- sql - object_id 和 data_object_id 在 Oracle 中是否只使用一次?
- javascript - 调用两个相同的子组件,但只有一个在 react-native 的父组件中工作,为什么?