-
Notifications
You must be signed in to change notification settings - Fork 26.1k
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
Video.js in App Route - window is not defined #61740
Comments
Hi @mufteev , you should import the component as dynamic with ssr: false. const Player = dynamic(() => import("../components/Player"), {ssr: false}) The Player component should be export default. |
Hi @hc0503, i update reproduction https://codesandbox.io/p/github/mufteev/nextjs-videojs-app-bug/main import dynamic from "next/dynamic";
const Player = dynamic(() => import("../components/Player"), {
ssr: false,
}); But... it didn't help |
@mufteev , Did you export Player component as default? windows undefined error doesn't occurs in my end. |
Yep, sorry i made changes to the repository, but did not apply it. You now see changes with dynamic import in https://codesandbox.io/p/github/mufteev/nextjs-videojs-app-bug/main Import without SSR not help. |
Nextjs app directory has strict mode by default enabled. Therefore, the videojs player is not loading properly. Following their guide, you will also not have a window is not defined error in production |
No, I still think it's not about React 18 (strictMode as say @Laityned) or the import method (dynamic + !SSR @hc0503 ) Because I've downgraded the version to Test version:
|
You are indeed right! However, following the other approach to load videojs, you can get it working in 14.1.0: |
I tried to narrow it down to a particular version: 14.0.5-canary.19 is still working |
For me, using mp4 works but throw video.js@8.10.0
|
downgrade next to 14.0.5-canary.19 works for me |
I got |
The window object is not available in server components. The Player components is a client component, but idk why sometimes even that doesn't help. You can do this client check trick in Player component and just return null if isClient is not true. |
const Player = dynamic(
() => import("../components/Player").then((mod) => ({ default: mod.Player }),
{ ssr: false },
); |
) Disable swc transform optimizer for node_modules in browser layer of app router bundles Fixes #61858 Fixes #60644 Fixes #60920 Fixes #61740 Closes NEXT-2418 In browser there could be not only one runtime, it could have both js worker and browser. In js worker the `typeof window` is not as same as in browser, so disabling the swc optimizer which will replace the code. Leave the condition as it as.
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Link to the code that reproduces this issue
https://codesandbox.io/p/github/mufteev/nextjs-videojs-app-bug/main
To Reproduce
Current vs. Expected behavior
Current
Catch
Uncaught Error: Uncaught ReferenceError: window is not defined
in console and not work video.jsCloned an example from a folder https://github.com/vercel/next.js/tree/canary/examples/with-videojs and ported to App Router
I get an error when using
App Router
in14.1.0
(latest), but if I change the version to^13.5.6
- Video.js worked.Expected
Video.js libraries initialized and mount component similar like previous major version
It is worth noting that everything works fine in
dev
mode, and the bug itself occurs in productionProvide environment information
Operating System: Platform: linux Arch: x64 Version: #15-Ubuntu SMP PREEMPT_DYNAMIC Tue Jan 9 17:03:36 UTC 2024 Binaries: Node: 21.6.1 npm: 10.2.4 Yarn: N/A pnpm: 8.15.1 Relevant Packages: next: 14.1.0 eslint-config-next: N/A react: 18.2.0 react-dom: 18.2.0 typescript: 5.3.3 Next.js Config: output: N/A
Which area(s) are affected? (Select all that apply)
App Router
Which stage(s) are affected? (Select all that apply)
next start (local)
Additional context
I tested my reproduction in
14.1.1-canary.38
and and I still got the error:Link github reproduction: nextjs-videojs-app-bug
The text was updated successfully, but these errors were encountered: