This is not a new issue in Next.js 13 - it existed in previous versions as well. The issue may be that your code is being executed on the server side when you don't expect it to be, or you might have changed how your components/pages are being rendered between versions.
To resolve this issue, you could ensure that your Ethereum related code only runs on the client side, where the window object is available. Here is how you could refactor your code to achieve this:
import { ethers } from "ethers";
export const signMessage = async () => {
if (typeof window === 'undefined') {
// We are on the server, exit early.
return;
}
try {
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const result = await signer.signMessage("Hey hey");
console.log(result);
} catch (error) {
// handle error
console.log(error);
}
};
By adding a check for typeof window === 'undefined', we can make sure that the Ethereum related code only executes on the client side, where the window object is available.
Make sure to call signMessage function only in lifecycle methods or hooks that are guaranteed to run only on the client-side, such as componentDidMount in a class component, or useEffect in a functional component. Do not call this function directly in the body of a Next.js page or component, as that code can run on both server and client sides.