我正在从事一个我正在遇到错误的下一个项目:JS项目:
Error occurred prerendering page "/dashboard". Read more: https://nextjs.org/docs/messages/prerender-error ReferenceError: window is not defined at new u (D:\Projects\FREELANCE\ROAD RUNNER RSA\Frontend\.next\server\chunks\500.js:13:9122)
useEffect(() => {
if (typeof window !== "undefined") {
const scrollbarWidth =
window.innerWidth - document.documentElement.clientWidth;
setScrollbarWidth(scrollbarWidth);
}
}, []);
"use client";
import { useEffect, useState } from "react";
3.Checked的第三方库(Lucide-React,Framer-Motion,Next/Navigation和 @Radix-UI/React-Visallial-Visally Hidded)确保它们都没有访问服务器上的窗口。
• Avoid Module-Level Window References: Ensure that no code accesses window outside of useEffect or other browser-only functions.
• Review Imported Components: Check if any imported component (even in client components) references window at the module level.
• Use Dynamic Imports: For components that require window, import them dynamically with SSR disabled:
import dynamic from 'next/dynamic';
CONSTclientOnlyComponent = dynamic(()=> import('./ yourcomponent'),{ssr:false}); •检查_app.js和_document.js:确保这些文件没有任何服务器端窗口参考。