Trouble while unloading in Next.js #493
Unanswered
SneakySensei
asked this question in
Help Wanted!
Replies: 1 comment
-
I found a workaround for this When Router Change starts I unload the Unity component like so: const { unityProvider, isLoaded, unload } = useUnityContext({
loaderUrl: "/builds/Builds.loader.js",
dataUrl: "/builds/Builds.data",
frameworkUrl: "/builds/Builds.framework.js",
codeUrl: "/builds/Builds.wasm"
})
const router = useRouter();
const bypassConfirmationRef = useRef(false)
useEffect(() => {
const shouldByPassconfimation = () =>
bypassConfirmationRef.current;
const handleBrowseAway = () => {
if (isLoaded) {
unload()
.then(() => {
bypassConfirmationRef.current = true;
})
.catch(e => console.log(e));
}
if (shouldByPassconfimation()) return;
router.events.emit('routeChangeError');
throw 'Quitting please wait.';
};
router.events.on('routeChangeStart', handleBrowseAway);
return () => {
router.events.off('routeChangeStart', handleBrowseAway);
};
}, [router.events, isLoaded]); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First of all, thank you to the whole community for actively maintaining this package. I've seen a few discussions around Next.js on this repo but I feel my issue is somewhat different.
So I'm able to mount the Unity component properly. For this, I'm disabling SSR completely using the
next/dynamic
package.But after changing routes, it throws this error:
I'm calling
unload()
in a useEffect cleanup function. From the active issues, I can tell that this problem is happening because the canvas unmounts before unity gets a chance to quit properly. I've also tried replacing this withUNSAFE__detachAndUnloadImmediate()
, but the issue persists. And we cannot useawait
inside a cleanup function.So what's the recommended way to unload the unity instance on a route change in Next.js?
@jeffreylanters
Beta Was this translation helpful? Give feedback.
All reactions