Comments (6)
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.
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.
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
- 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
- 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.
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.
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.
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)
- process.env.DATABASE_URL is hard coded after vite build and will that considered as leakage? HOT 1
- Environment variables not available in AWS ECS instance. HOT 2
- "Hydration failed ..." in standard vite template application HOT 4
- The import type { MetaFunction } and import { type MetaFunction } have different behaviors when build HOT 3
- Empty JS chunk created for resource route
- Error: Cannot reach Remix dev server at localhost:3001/ping HOT 5
- `npx create-remix@latest` says `Oh no! Failed to initialize git.` HOT 5
- Vite custom express migration docs don't mention 'source-map-support'
- using the default .eslintrc.js does not lint any files that exist in .server or .client folders HOT 4
- Wall of errors after upgrading to vite and navigating to index HOT 19
- Cannot import "exports": { "module": ... } without type=module configured packages, instead we could get an error: Cannot use import statement outside a module
- @remix/node should not be forced to install polyfills HOT 1
- SyntaxError: Unexpected token ',' HOT 1
- vite template doesn't start (dev server origin not set) HOT 6
- Vite thinks certain modules are CommonJS with Remix HOT 1
- TypeError: Unknown file extension ".ts". Not working dev/build with express server and import files HOT 1
- ✘ [ERROR] Could not resolve "lightningcss" ONLY when running `remix dev` HOT 1
- `No match URL` error occurs. (CloudFlare Pages) HOT 2
- docs: Invalid link for `Splat Route` at `Not found handling` HOT 1
- Vite hangs on fresh npm install when using `installGlobals` HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from remix.