Giter Site home page Giter Site logo

Dynamic imports about remix HOT 6 CLOSED

remix-run avatar remix-run commented on May 3, 2024 10
Dynamic imports

from remix.

Comments (6)

lkostrowski avatar lkostrowski commented on May 3, 2024

Sometimes its perf reason (loading when its needed), sometimes its just a must.
I struggle to implement Leaflet maps in Remix now, I definitely need something like next's dynamic() or react lazy, otherwise its hacking. Some libraries are unsafely trying to access DOM...

from remix.

jacob-ebey avatar jacob-ebey commented on May 3, 2024

You can currently do Client Only dynamic component imports with the following pattern:

import { Suspense, lazy, useEffect, useState } from "react";
import type { ReactNode } from "react";

let LazyImported = lazy(() => import("./lazy-comp"));

export function ClientOnly({ children }: { children: ReactNode }) {
  let [mounted, setMounted] = useState(false);
  useEffect(() => {
    setMounted(true);
  }, []);
  return mounted ? <>{children}</> : null;
}

export default RouteComponent() {
  return (
    <h1>YAY!</h1>
    <ClientOnly>
      <Suspense fallback="">
        <LazyImported />
      </Suspense>
    </ClientOnly>
  );
}

from remix.

lkostrowski avatar lkostrowski commented on May 3, 2024

Thanks @jacob-ebey , it work indeed, but its a hacky way. I assume it will be gone when React 18 and SSR suspense is shipped.

What is worth to mention IMO is

  1. Add this to the documentation. There are plenty of examples how to do it in Next and Remix is sort of more high level Next alternative. I think problems with SSR/hydration is one of the biggest challenges for devs
  2. Dynamic imports require TS to set a valid module flag in tsconfig. For me it works with es2022 but I can't be sure if I'm not breaking Remix internal builds right now

Regarding initial issue by Ryan, my opinion is to use native React way (lazy and suspense) without re-exporting it, but ensure internal build system is configured well to support it

from remix.

k1sul1 avatar k1sul1 commented on May 3, 2024

Dynamic imports require TS to set a valid module flag in tsconfig

Dynamic imports seem to work just fine with "module": "es6" in tsconfig even though TS complains about the availability.

Older browsers probably break entirely but that's to be expected with ES6. Changing module to something else is bound to break newish browsers, like Safari from last "good' MacOS version, so that isn't really an option?

Would be nice if there was some sort of official guidance on the subject. I was able to implement a lazy loading admin overlay, but I'm a bit afraid for it's longevity.

from remix.

jacob-ebey avatar jacob-ebey commented on May 3, 2024

tsconfig isn't really used and there's nothing you configure there that will break how remix complies your code currently. Don't worry about changing it, especially when it's to include a config for language settings you are trying to use such as dynamic import. Anything you can do today such as putting suspense behind a client only won't break in the future as react is only adding the ability for the server to use this, not removing anything. Don't worry about "longevity" in terms of things breaking around the pattern I've outlined above. And I wouldn't call it hacky, it's literally how you delay execution for client only using state in React. Nothing strange about it.

As for a "dynamic" component, React.lazy will essentially become this when 18 drops, but I'm skeptical about implementing it today as every version of it will be a combo of the compiler turning async imports into sync imports for the server, and "hacks" for the client around delaying hydration until these chunks have loaded to avoid hydration issues. This means we'd have to expose some sort of "waitTillDynamicHasLoaded()" you have to call in your entry.client, or hide this away very hacky under the hood. I don't think this is worth it currently.

from remix.

k1sul1 avatar k1sul1 commented on May 3, 2024

Thanks for the clarification! I can get rid of // @ts-expect-error comments by just switching the module flag. I had gotten a bit of misinformation from somewhere, thinking things would break in the future.

from remix.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.