我在 TypeError: Only absolute URLs are supported
和 Vercel
中尝试部署 Next.js 应用时遇到了 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
文件中的 config
url
在 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
任何帮助将不胜感激