question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:)

  1. Create a custom page
  2. Add an event using window.addEventListener
  3. Run npm run build or yarn 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:closed
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
maksnestercommented, Jul 14, 2020

There’s no window on the server, naturally it will fail.

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.

// some.mdx
import MyFancyDemo from './MyFancyDemo'

<MyFancyDemo />
// MyFancyDemo.jsx
import problem from 'browser-only-library'

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

0reactions
prma85commented, Jan 29, 2020

You need to use both useEffect() AND the window check within it.

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found