Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NextJS Build fails #137

Open
Jyolbriger opened this issue Oct 28, 2021 · 3 comments
Open

NextJS Build fails #137

Jyolbriger opened this issue Oct 28, 2021 · 3 comments

Comments

@Jyolbriger
Copy link

Jyolbriger commented Oct 28, 2021

I implemented NoSleep into my NextJS App and tried to build on Vercel but the process unfortunately failed. See the server log below.

On my local machine it's not throwing an error.
NextJS-Version: 12.0.1

EDIT: I see there has been a ticket already concerning SSR and a workaround: https://github.com/richtr/NoSleep.js/pull/106

Is there a way to deploy my project via build process on Vercel with SSR and to use NoSleep?

00:08:00.982 > Build error occurred
00:08:00.987 ReferenceError: navigator is not defined
00:08:00.987 at Q (/vercel/path0/node_modules/nosleep.js/dist/NoSleep.min.js:2:1732)
00:08:00.987 at new A (/vercel/path0/node_modules/nosleep.js/dist/NoSleep.min.js:2:1900)
00:08:00.988 at Object.2588 (/vercel/path0/.next/server/pages/undercover.js:34:15)
00:08:00.988 at webpack_require (/vercel/path0/.next/server/webpack-runtime.js:25:42)
00:08:00.989 at webpack_exec (/vercel/path0/.next/server/pages/undercover.js:300:39)
00:08:00.989 at /vercel/path0/.next/server/pages/undercover.js:301:70
00:08:00.989 at Function.webpack_require.X (/vercel/path0/.next/server/webpack-runtime.js:108:21)
00:08:00.990 at /vercel/path0/.next/server/pages/undercover.js:301:47
00:08:00.990 at Object. (/vercel/path0/.next/server/pages/undercover.js:304:3)
00:08:00.990 at Module._compile (internal/modules/cjs/loader.js:1085:14) {
00:08:00.991 type: 'ReferenceError'
00:08:00.991 }
@appcessorize
Copy link

Did you get this to work?

@H4sh3
Copy link

H4sh3 commented Jan 27, 2022

Have you tried using it inside useEffect? This way the code is only executed on the client.

const { request, release } = useNoSleepWakeLock();

useEffect(() => {
    request()
    return () => {
        release()
    }
}, [])

Edit: useNoSleepWakeLock is this react hook:
https://github.com/JoshuaKGoldberg/use-no-sleep/blob/master/src/index.ts

@ioandev
Copy link

ioandev commented Feb 17, 2022

Here is what I ended up doing:

let [noSleepEnabled, setNoSleepEnabled] = useNoSleep()
let handleNoSleepClick = () => {
	setNoSleepEnabled(!noSleepEnabled)
}


    <Button onClick={handleNoSleepClick}>
            ....
    </Button> 


    import NoSleep from "nosleep.js";
    import { useEffect, useMemo, useState } from "react";
    
    export default function useNoSleep () {
        const [enabled, setEnabled] = useState(false);
        const [isMounted, setIsMounted] = useState(false);
    
        let noSleep = useMemo(() => {
            if (!isMounted) {
                return null
            }
    
            return new NoSleep()
        }, [isMounted])
    
        useEffect(() => {
            setIsMounted(true)
    
            if (noSleep == null) {
                return;
            }
    
            if (enabled) {
                noSleep.enable();
            } else {
                noSleep.disable();
            }
    
            return function cleanup() {
                if (noSleep == null) {
                    return;
                }
                
                if (enabled) {
                    noSleep.disable()
                }
            }
        }, [enabled, noSleep]);
    
        return [enabled, setEnabled] as const
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants