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

PSA: Vite bundler HMR re-render bug #287

Open
hughdtt opened this issue Jul 30, 2023 · 1 comment
Open

PSA: Vite bundler HMR re-render bug #287

hughdtt opened this issue Jul 30, 2023 · 1 comment

Comments

@hughdtt
Copy link

hughdtt commented Jul 30, 2023

PSA for those following the React bootstrap instructions on site documentation

Issue: Vite's hot module reloader doesn't clean up React DosPlayer component properly

Context:

  • New to js-dos, so I decided to follow the React bootstrap instructions https://js-dos.com/v7/build/docs/react/ in the doco but using Vite instead.

  • Ran into the issue where the dos player component was re-rendering itself on every save, but only re-rendering once on first load.

  • I'd get stuck with something like this

image

  • Thought it was weird since the code provided already had an instance.stop()for cleanup.

Fix:

  • Turns out Vite HMR works differently from whatever bundler was intended.
  • You'll need to add a root.innerHTML = ''; to the DosPlayer return function to make sure it's cleaning up on every render properly.

Typescript component ends up looking a little like this:

import React, { useEffect, useRef, useState } from "react";
import { DosPlayer as Instance, DosPlayerFactoryType } from "js-dos";

declare const Dos: DosPlayerFactoryType;

interface PlayerProps {
    bundleUrl: string;
}

export default function DosPlayer(props: PlayerProps) {
    const rootRef = useRef<HTMLDivElement>(null);
    const [dos, setDos] = useState<Instance | null>(null);

    useEffect(() => {
        if (rootRef.current === null) {
            return;
        }

        const root = rootRef.current as HTMLDivElement;
        const instance = Dos(root);
        setDos(instance);

        return () => {
            instance.stop();
            root.innerHTML = ''; //fix
        };
    }, [rootRef]);

    useEffect(() => {
        if (dos !== null) {
            dos.run(props.bundleUrl); // ci is returned
        }
    }, [dos, props.bundleUrl]);

    return <div ref={rootRef} style={{ width: "100%", height: "100%" }}></div>;
}

End of the day, it's not a js-dos issue but a weird interaction with Vite.

Just thought it might be helpful for people who run into the same issue. It gave me a headache far longer than it should have 🤣

Good day

@caiiiycuk
Copy link
Owner

caiiiycuk commented Jul 31, 2023

Wow. Thank you very much, I think we need to add this to docs. Cleaning the DOM tree is good idea in any case.

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

No branches or pull requests

2 participants