首页 > 解决方案 > 尝试部署 Next.js 应用程序时出现“仅支持绝对 URL”错误

问题描述

我在绑定和TypeError: Only absolute URLs are supported部署 Next.js 应用程序时遇到错误。在我的应用程序中,我有一个文件可以避免错误,因为数据是从服务器获取的。VercelNetlifyconfigabsolute 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 的方式。configpages/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

任何帮助将不胜感激

标签: next.jsvercel

解决方案


推荐阅读