When using "window.addEventListener" the build fails for server side
See original GitHub issueš Bug Report
When building the doc website, the build is failing for the server if there is a use for the window since it doesnāt exist in the server.
To Reproduce
(Write your steps here:)
- Create a custom page
- Add an event using
window.addEventListener - Run
npm run buildoryarn build
Expected behaviour
The build is completed with success.
Actual Behavior
The build fails for the server (but it is completed for the client and it works fine with npm run start
Reproducible Demo
Code used for the custom page
import React, { useRef } from "react";
import Layout from "@theme/Layout";
import useBaseUrl from "@docusaurus/useBaseUrl";
const Page = props => {
const { location, history } = props;
const iframeSrc = useBaseUrl("storybook-frame") + location.search;
const title = useRef("Storybook");
window.addEventListener("message", onMessage, false);
function onMessage(message) {
if (message.data.source === "CUSTOM_IFRAME") {
title.current = message.data.kind
.replace(/-/gi, " ")
.replace(" ", " - ");
history.pushState("", title.current, "?path=/story/" + message.data.kind);
}
}
return (
<Layout title={title.current}>
<main id="storybook-frame" style={{ width: "100%", height: "calc(100vh - 100px)", position: "relative" }}>
<iframe
src={iframeSrc}
title="Storybook"
style={{ width: "100%", height: "100%", border: 0, position: "absolute", overflow: "hidden" }}
sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"
/>
</main>
</Layout>
);
};
export default Page;
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
"window" is not available during server side rendering
Trying to use Isotope on Gatsby. Installed via npm install isotope-layout , then getting this error when gatsby-build : failed Building static ......
Read more >EventTarget.addEventListener() - Web APIs | MDN
The addEventListener() method of the EventTarget interface sets up a function that will be called whenever the specified event is deliveredĀ ...
Read more >How to solve "window is not defined" errors in React and Next.js
First solution: typeofāā Rogier Nitschelm for reminding me about this. I initially tried to do if (typeof window !== undefined) and this failed...
Read more >Solve ādocument is not definedā errors in Next.js | by WebTutPro
You can import your Scroll component using dynamic imports and the srr: false option. This way your component won't even be rendered on...
Read more >How to Fix "window is not defined" in Next.js - Upmostly
This issue has to do with Server-Side Rendering in Next.js. Next.js will by default try to use SSR for your site.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Itās also an issue for me. I tried to create some live demo by using jsx components. And my component has an import of some component that has an import of browser-only code.
How to overcome that? Iām fine with having JSX components not being server-rendered.
UPD: found answers here
https://github.com/facebook/docusaurus/issues/2494 https://v2.docusaurus.io/docs/docusaurus-core/#browseronly
Oh yeah, I mean, I did it, but I got an error from server build, but since it is minified I am assuming that the error came from the same file. BTW, is there a way to build without minification? Like
docusaurus build --development?