Home > Mobile >  Crypto.randomUUID() not working with Next.js v13
Crypto.randomUUID() not working with Next.js v13

Time:01-29

The Environment

Next.js v13 (stable, no ./app folder) is running with Node v18 in Ubuntu WSL.

As per de The client text did not match the server which is to be expected as an UUID function always returns a different result.

CodePudding user response:

Browsers restrict access to some crypto APIs when not running in a secure context (as defined here).

CodePudding user response:

Set it to state in a useEffect hook when page initially loads so it persist and then render it from state.

const Crypto = () => {
 const [randomUUID, setRandomUUID] = useState();

 useEffect((
  if (typeof window !== 'undefined' && !randomUUID) {
   setRandomUUID(crypto.randomUUID());
  }
 ),[]);

 if(!randomUUID) <>No UUID</>;
 return <>{randomUUID}</>
}

export default Crypto;
  • Related