Closed6
Cloudflare PagesにNext.jsをデプロイできたけどInternal Server Error発生

色々やって何とかデプロイ自体はできたけどInternal Server Errorが発生して表示されない。
runtime edge
で何か起きてそう。
一旦状況整理
調査に使用していたページは2つ
1つ目がメインページでSSRのためruntime edge
+async
を使っていて以下エラー発生。
Disallowed operation called within global scope. Asynchronous I/O (ex: fetch() or connect()), setting a timeout, and generating random values are not allowed within global scope
2つ目がnotFoundページでruntime edge
もasync
も使っていない状態でエラーなし。
試しにruntime edge
を使ってみたところ以下エラー発生。
TypeError: Cannot redefine property: __import_unsupported
notFoundページで使っていたnext/link
のimportを外してみたところ以下エラーが発生。
Disallowed operation called within global scope. Asynchronous I/O (ex: fetch() or connect()), setting a timeout, and generating random values are not allowed within global scope

このエラーは、グローバルスコープで fetch() などの非同期処理を実行している ことが原因です。
つまり、fetch() や setTimeout() などを 関数の外側 に記述している場合に発生します。
export const runtime = "edge";
const res = await fetch("https://api.example.com/data"); // ❌ グローバルスコープで実行
export default async function Home() {
const data = await res.json();
return <div>{data.title}</div>;
}
とのことだけどこんなコード書いてないんだよなー

export const runtime = "edge";
export default function Home() {
return <div>テストページ</div>;
}
このコードをデプロイしてみてもエラーが発生する
Nextjsのバージョンとかの問題なのか…?

rm -rf node_modules package-lock.json
npm install next@14.1.0 react@18.2.0 react-dom@18.2.0
これで15系からダウングレードしたら表示された

tailwindcssもダウングレード
npm install -D tailwindcss@3.4.17
これに伴い色々修正した結果globals.css
で何か起きている模様

next.config.js
の書き方を修正して
npm install autoprefixer postcss --save-dev
これを実行することですべて解決、表示された
このスクラップは2ヶ月前にクローズされました