sindresorhus / screenfull

Simple wrapper for cross-browser usage of the JavaScript Fullscreen API
https://sindresorhus.com/screenfull
MIT License
7.08k stars 698 forks source link

ReferenceError: document is not defined #194

Closed RyanWarner closed 3 years ago

RyanWarner commented 3 years ago

I updated from 5.1.0 to 6.0.0 and am now getting the error

ReferenceError: document is not defined

during server side rendering in a NextJS app.

sindresorhus commented 3 years ago

Screenfull is meant to run in a browser. The previous version did indeed polyfill document, but I no longer think that's the correct place to handle it. If you want to run a browser-based library on the server, it's up to you to polyfill the required globals.

tranmanhhung commented 2 years ago

I updated from 5.1.0 to 6.0.0 and am now getting the error

ReferenceError: document is not defined

during server side rendering in a NextJS app.

Dear @RyanWarner I'm meet this problem, Can you help me fix this problem, Please.

RyanWarner commented 2 years ago

@tranmanhhung I solved the problem by downgrading back to version 5.1.0.

tranmanhhung commented 2 years ago

@tranmanhhung I solved the problem by downgrading back to version 5.1.0.

I just found solved this problem by.. const nativeAPI = (() => { const unprefixedMethods = methodMap[0]; const returnValue = {};

for (const methodList of methodMap) {
    const exitFullscreenMethod = methodList?.[1];
    if (typeof window === 'object') {
        if (exitFullscreenMethod in document) {
            for (const [index, method] of methodList.entries()) {
                returnValue[unprefixedMethods[index]] = method;
            }

            return returnValue;
        }
    }
}
return false;

})();

line 56 added if (typeof window === 'object') {