Speed Up Your React Apps With Code Splitting

Поділитися
Вставка
  • Опубліковано 22 лис 2024

КОМЕНТАРІ • 271

  • @LockeAG4
    @LockeAG4 Рік тому +253

    7:53 You can always simulate slow connections via Web Console > Network > throttling > Slow3G

    • @pearl911
      @pearl911 Рік тому +11

      I was wondering why he didn't do that also because he's already used it in videos such as ua-cam.com/video/ZVug65gW-fc/v-deo.html

    • @Flash136
      @Flash136 Рік тому +16

      @@pearl911 I'd say using a wait function makes the wait time more deterministic. And you can change the wait time to however long you want.

    • @lleytonmorris6305
      @lleytonmorris6305 Рік тому +2

      Thought the exact same thing. Best part IMO of the network tab is the ability to set a custom TTFB which is a very real and practical scenario to test.

    • @victormog
      @victormog Рік тому +9

      Kyle showed an example of a useful Promise 😉

    • @Pete133
      @Pete133 Рік тому +3

      That is pretty useful, but for what it's worth doing this particular task with dev tools throttling would have required waiting much longer for react itself to download than for the component to download... the way he did it was actually a lot better for the demonstration.

  • @theyreMineralsMarie
    @theyreMineralsMarie Рік тому +378

    Asynchronous imports aren't just a cheat code to making your app faster. You should only use it if there are portions of the app that are not needed until the user initiates some process. For example if some component is hidden until the user clicks a button, then you could asynchronously import that component.

    • @gabrielemarino1360
      @gabrielemarino1360 Рік тому +16

      There is also a prefetch variable for webpack which download that resource when the engine is idling. It is not quite the same situation but i wanted to point that out

    • @mithunkumar-vm5iw
      @mithunkumar-vm5iw Рік тому +11

      Like lazy loading in angular?

    • @jackykwan6534
      @jackykwan6534 Рік тому +5

      @@gabrielemarino1360 this should be better, let the engine do the job for u

    • @Cotita
      @Cotita Рік тому +3

      And why would you not load portions of the app that are not needed yet? Why not just load them from the beginning? To make the app faster, genius.

    • @theyreMineralsMarie
      @theyreMineralsMarie Рік тому +1

      @@Cotita yes but my point was that it's important to know how and why to use this feature. Just throwing around async imports everywhere isn't necessarily going to improve anything.

  • @Amine-yx5vc
    @Amine-yx5vc 10 місяців тому +1

    Code Splitting is Splitting bundle into multiple parts that can be downloaded over time ('lazy loading") . That help us to easy optomize our application , fix real performance issues...

  • @LilAlbus
    @LilAlbus Рік тому +5

    The quality of these vids has gone way up over the years man! Well spoken.

  • @balajiganesh4246
    @balajiganesh4246 3 місяці тому

    Uff.Thanks so mu h for this lovely video. I was so dejected thinking i needed to read Webpack from scratch to understand Code splitting. You just cleared it up for me in the first 5 mins itself. Thanks so much

  • @kaylee_reed
    @kaylee_reed Рік тому +3

    That's the best thing ever. I didn't even think that this could be a thing in React at all. Thanks for making this video! 💕

  • @ahmad98282
    @ahmad98282 Рік тому +72

    At 07:52 To depict Loading... you don't need to add wait. You can simply open up your console, Go into Network Tab. In the bottom layer, you'll see "No throttling", click that and change it to "Slow 3G" and there you go. You can actually see how your fallback will work when there is a delay in your network.

  • @yevhenlysenko7444
    @yevhenlysenko7444 Рік тому

    I watched this lesson 2 month ago, but always try to revise your videos. Thanks!

  • @piyushaggarwal5207
    @piyushaggarwal5207 Рік тому +2

    I was struggling with permission based access for the user in my app.
    Things weren't working very well.
    Now, I think things will be fine when I use the useTransition hook.
    Great!

  • @yadneshkhode3091
    @yadneshkhode3091 Рік тому +41

    Please bring more videos related to performance improvement, aria, testing related, logging, debugging issues, improving css loading time etc

    • @techjourney2754
      @techjourney2754 Рік тому +2

      this guy is a busy guy bro, probably has a full time dev job, he runs his own business and he makes videos, 😂, work ethic i aspire to have

  • @aarona3144
    @aarona3144 Рік тому +82

    Kyle, this is a great video. However, I think there are a few additions that would've made it better:
    1) In addition to showing files being downloaded piece by piece, demonstrating this using network tab in the dev console would've also be a great way to show people how to also make the most out of the dev console by debugging network latency, Showing them that you're downloading the entire client app in one go versus downloading it bit by bit as the user uses the site over time.
    2) Another dev console trick would've been to demo the way emulation of 4g internet works by using the throttling feature in the network tab as well. That wait function is pretty cool but these tools were built for developers in mind.
    3) Finally, if you added a build script and built the app, showing people how the bundle was created as just one large js file before lazy loading but after adding lazy loading, it bundles itself into multiple smaller bundles.

    • @lima91rs
      @lima91rs Рік тому +8

      "multiple smaller bundles" - a.k.a. chunks

    • @guybenda1
      @guybenda1 Рік тому +3

      3 was really missing!
      thanks :)

    • @xbsidesx
      @xbsidesx Рік тому +1

      Nice suggestions indeed!

    • @filipniklas
      @filipniklas Рік тому +1

      Great suggestions!

  • @JoaoPaulo-ox6pr
    @JoaoPaulo-ox6pr Рік тому +1

    So useful! My react app was taking long minutes to be loaded it weights something around 700MB, and my computer is a little weak, i thought it could be this, cause, until now, i didn't even know this was possible.

  • @xbsidesx
    @xbsidesx Рік тому +7

    In the lazyLoad function, you don’t really need an else statement since you’re returning from the if. Without an else that would be an else regardless. I know some people prefer verbosity, but that’s actually confusing for some. Great video regardless, you’re a very good communicator!

  • @GevKerobyan
    @GevKerobyan Рік тому +1

    Good God, finally a valid explanation of this topic. Thanx a lot sir.

  • @JamalKhan-zl8lu
    @JamalKhan-zl8lu 4 місяці тому +1

    16:20 Important to NOTE when you implement a lazy with a separate file.

  • @nekromenzer
    @nekromenzer Рік тому

    Simply WOW , this may super useful when we working on complex applications

  • @yordanov5.0
    @yordanov5.0 Рік тому

    Man, you are a true hero! Thank you very very much for the amazing content!

  • @joe_xyz
    @joe_xyz Рік тому +122

    I think it's important to note that the helper function at 15:06 will not work in production builds depending on the build tool you're using because the transpiler can't guess ahead of time what you're going to import through that function (Vite even warns you about that). I tested it with Vite + TypeScript (no SWR) and while it does work fine in dev mode, it will not work in production mode. The transpiler doesn't create the imported JS files in the destination directory and the app will try to dynamically import "/assets/components/Something(.js/.ts)" which obviously doesn't exist. Unfortunately, I don't think there's a workaround for this in JS alone, but there could be build plugins for it.

    • @lijason3766
      @lijason3766 Рік тому +2

      which means you are expected to leave your code un-bundled as if in dev mode if you are going to use this dynamic import cheat.

    • @nicholasdickrell5118
      @nicholasdickrell5118 Рік тому +2

      100% this answer. When you run a large app, the last thing you want is failed to load dynamically imported module clogging up your logging and getting calls from your Bridge.

    • @IAmLesleh
      @IAmLesleh Рік тому +4

      Just pass the import("whatever.js") to the helper function instead of just the filename, problem solved.

    • @botchu4597
      @botchu4597 6 місяців тому

      Thank you so much. i wasted so much time not knowing this.

    • @botchu4597
      @botchu4597 6 місяців тому

      This worked for me
      function lazyLoad(fileName, isNamed) {
      return lazy(() => {
      const promise = import(`./${fileName}`);
      if (isNamed) {
      return promise.then(module => ({ default: module[fileName] }));
      }
      return promise;
      });
      }

  • @sob515
    @sob515 Рік тому +5

    You forgot to add this is only suitable for really huge components. If you "lazyload" all the things you are actually slowing down your application at runtime to gain faster load at the start. This way your page/app will become laggy on slower connection speeds and this is not advised in production. It has terrible impact on look&feel. You want your apps to be responsive and snappy even if it loads 0.2s slower at the beginning, you will probably never reload this page (SPA) and most of the things will get cached.

  • @rajeshdavide
    @rajeshdavide Рік тому +4

    Great video. You can use both default and named export at the same time, so technically you can use default for the main component that needs to be loaded in the route. I always use both!

    • @tan2cang93
      @tan2cang93 Рік тому

      indeed, I always use the way like that,

    • @likatest7718
      @likatest7718 Рік тому

      If i use default should i adding second paraneter in lazyload function?

  • @eyobsamuel8722
    @eyobsamuel8722 Рік тому +1

    Thanks, Kyle. Instead of writing wait promise and more codes (because this is web dev simplified), it was better to use fast or slow 3G throttling from network tab.

  • @MatthewWeiler1984
    @MatthewWeiler1984 Рік тому +3

    Thank you, this is a great feature of React that I wasn't aware of.
    But when using TypeScript, passing the path to the component into your lazyLoad function doesn't work.
    But if I changed the lazyLoad function to accept a Promise, then pass into it the import('...') and then the namedExport, it works fine.

  • @thesickandwounded
    @thesickandwounded Рік тому +2

    Always things i've never heard about before. Great stuff!

  • @gabruCoder
    @gabruCoder Рік тому

    I was waiting for your new video, hanging around the playlist section .

  • @pankaj-kumar-yadav-67
    @pankaj-kumar-yadav-67 2 місяці тому

    add lazy loading on routes file is game changer. TY!

  • @nhutquangphan4160
    @nhutquangphan4160 Рік тому

    I don't know why but somehow I can understand your video with my not really good English. Thank you so much mate !😁

  • @MuhammadAvicena
    @MuhammadAvicena Рік тому +2

    I always wait for your content about React JS. Thank you for the knowledge 🙏

  • @PowderedToastMaaaan
    @PowderedToastMaaaan Рік тому

    Great video Mr. Simplified. Thank you!

  • @caiocavalcan
    @caiocavalcan Рік тому

    Nicely done friend. I learn so much from your videos. Thank you.

  • @ayushpal9704
    @ayushpal9704 Рік тому +1

    Great Explanation Bro

  • @webtech7242
    @webtech7242 Рік тому +1

    Have learned a lot from you bro, ❤️ from Bangladesh

  • @Byte-flow
    @Byte-flow 9 місяців тому

    the way you explain >>>>

  • @ramuramasamy7018
    @ramuramasamy7018 Рік тому

    By the way, you can also simulate slow internet using your chrome browser by setting the network speed in the developer tools.

  • @bulentgercek
    @bulentgercek Рік тому +1

    Kyle you have a problem your audio output on your videos. Use +0db or not use lower then -2db and alsoe there is audio balancer tools on video editing tools if you worried about audio changes. I just have to turn up the volume almost all the way for your videos and I can't turn anything else on, because any other audio output, video music etc. it's terribly loud.

  • @amauryperalta4364
    @amauryperalta4364 Рік тому

    I loved this video. I understood way better the concept of code splitting. I can now apply this knowledge, Thank you very much Kile😁

  • @shadowangel8005
    @shadowangel8005 Рік тому

    That's interesting. Qwik is doing something like this. It listens to the global window object then brings in the data when there are actions. It could certainly help bring code down on first load.

  • @tan2cang93
    @tan2cang93 Рік тому

    I have changed many files from named export to default export in order to apply lazy loading. Then, I found a lib react-lazily which help me do the job. Anyways, thank for sharing the lazyLoading function. cool!

  • @JosprimaSihombing
    @JosprimaSihombing Рік тому +3

    Great video clear explanation and example as usual ⭐

  • @dinghanlim7735
    @dinghanlim7735 Рік тому +5

    considering its pros, should we always use it? there wasn’t much discussion about its disadvantages so i’m not sure if it’s bad to always split code like that

  • @pablonavarro2523
    @pablonavarro2523 8 місяців тому

    Hi Kyle, TY for the content, very useful videos
    I usually use Lazy loading and suspense, but something that I didn't fully understand from your video was: What is the difference between using the fallback prop in VS useTransition ?

  • @shinaspk9276
    @shinaspk9276 5 місяців тому

    Great explanation, bro!

  • @mukulsinghbisht2434
    @mukulsinghbisht2434 7 місяців тому

    Thats some real advanced knowledge

  • @hamishboddy1405
    @hamishboddy1405 8 місяців тому

    Hey everyone! I found that when you use the `lazyLoad()` function that Kyle created, vite cannot analyze the dynamic path for the import and therefore will not be able to properly build the js and file paths for these components when you run the build command.
    To get around this I found that you can pass the whole import function into the custom function like this `lazyLoad(import('./components/AdminData.jsx'), 'AdminData')` and use that instead of assigning the promise variable. This keeps the import path static so vite can work with it during build time.
    `export const lazyLoad = (importCall, namedExport) => {
    return lazy(() => {
    if (namedExport == null) return importCall;
    return importCall.then(module => ({ default: module[namedExport] }));
    });
    }`

  • @atejap
    @atejap Рік тому +1

    Another awesome video. Tks.

  • @maksymkyryliuk492
    @maksymkyryliuk492 Рік тому

    Thanks for this video. Design is very human, easy to use) Thanks ❤

  • @memoryleakerz
    @memoryleakerz Рік тому

    Thats basically a Resumable Hydration (Up to 4:25), that is what Qwik is doing under the hood

  • @clarkdnro
    @clarkdnro Рік тому

    Nice, ive learn something new. Good job

  • @mirazking7694
    @mirazking7694 Рік тому

    That's why we Use Nextjs For production ready app.

  • @hussainbharmal5998
    @hussainbharmal5998 Рік тому

    you can also throttle your network from the chrome dev tools

  • @1Chitus
    @1Chitus Рік тому

    Great video clear explanation and example as usual

  • @serginho445
    @serginho445 Рік тому

    Who really wants to keep the user waiting for a component/content to be downloaded on click ?
    lazy loading is nice, but what would really help, is that after you load the necessary code (like HomePage), the lazy load should already start downloading async the rest of the code (like Store and About component). There was someone writing about a prefetch, that would really be useful. And if the user really navigates to that tab before it's downloaded async, only then they will see the Loading content ( animation, blur , whatever).

  • @engelshernandez5898
    @engelshernandez5898 Рік тому

    Great as always. Thank you, Kyle!

  • @danielradosa
    @danielradosa Рік тому

    Great video, thank you. Splitting stuff into more bits is not just making app faster, but its also good for separation of concerns.

  • @bernhardsmuts2265
    @bernhardsmuts2265 Рік тому

    Great video as always! Keep it up, you're an essential instructor in the Javascript ecosystem 😎

  • @paperinflames
    @paperinflames Рік тому

    Thanks for sharing your knowledge ❤ Please make videos abour Angular alsoo... Please 😢

  • @cyclelife2.076
    @cyclelife2.076 Рік тому

    Thanks from Kerala

  • @LordBoltagon
    @LordBoltagon Рік тому +1

    Nice work! 👍🏼

  • @praveen_sinnur
    @praveen_sinnur Рік тому

    Explained perfectly. Thank you

  • @iamasifimam
    @iamasifimam Рік тому

    thankyou for this useful information really appreciate you for this.

  • @ishere_ranjan
    @ishere_ranjan Рік тому

    How to get difference between two clock time
    Example :-
    Let start = 10:30AM
    Let end = 11:30AM
    Answer: 1 hour
    But how through javascript

  • @FlashLim
    @FlashLim Рік тому

    They are great strategy for specific need, however if u required to do so much of the code splitting on so many components, u might as well go for micro front-end, solve it entirely instead of pieces here and there

  • @felipecouto9044
    @felipecouto9044 Рік тому +1

    Great content! 👏

  • @sairfan06
    @sairfan06 Рік тому

    Thanks for an other amazing video, it would be great if you also add link to in description to related blog on your website. thanks

  • @faldinurikhsan5048
    @faldinurikhsan5048 Рік тому

    as always, great video 👍

  • @jackykwan6534
    @jackykwan6534 Рік тому

    Most of the apps are not really need "FAST" onboard loading, 1-2 secs is acceptable
    Or you can keep landing page simple and fast for SEO, and only open actual app when user click 'Open App'

  • @labhamjain3915
    @labhamjain3915 Рік тому

    Huge love from India Kyle :) ❤️

  • @abdussametkaradeniz3596
    @abdussametkaradeniz3596 Рік тому

    this video... golden!!!!!!

  • @acearvingando6848
    @acearvingando6848 Рік тому

    appreciation from the Philippines 🇵🇭 we’re learning a lot

  • @victormog
    @victormog Рік тому +1

    Very useful and clear!

  • @guieltorres
    @guieltorres Рік тому

    You are incredible thx for the video ❤

  • @alexrusin
    @alexrusin Рік тому +1

    Great video. Thank you.

  • @abidibnazam4462
    @abidibnazam4462 Рік тому

    That rhymed well 4:31

  • @inspiredomkar1239
    @inspiredomkar1239 3 місяці тому

    Much appreciated video

  • @everyonecanbefascist
    @everyonecanbefascist Рік тому

    Learn so many stuff in just 15 mins, thanks a lot.

  • @yusufaltundal3482
    @yusufaltundal3482 Рік тому

    Incredible 👏

  • @johnflavian
    @johnflavian Рік тому

    Wow...
    Thanks a bunch. This will help a lot.
    There's a project that I'm currently working on; after building it up... One of the main js files is about 24mb... How can I optimise it more... It takes a lot of data and time to load from the web server.

  • @zachsanchez1644
    @zachsanchez1644 Рік тому +1

    Love Suspense, found out about it from @PedroTech’s channel. This is nice to do at a component level when you want parts or the page to be interactive prior to loading an item

  • @rafageist
    @rafageist Рік тому

    If you build everything in a single JS file, what is a dynamic import for you? Is a "dynamic import" but it is not a "dynamic download"... so, the optimization is for the RAM ? ... you download everything in one time for SPA

  • @shervangh9660
    @shervangh9660 Рік тому

    tnx for your tipe's bro

  • @yoyo26-34
    @yoyo26-34 Рік тому

    Excellent, thanks

  • @techtalk7671
    @techtalk7671 Рік тому

    Very useful tutorial! Thanks

  • @TrishalWalia
    @TrishalWalia Рік тому

    Great tutorial

  • @v1ntee
    @v1ntee Рік тому

    7:30 There is a option in Chrome to slow down your connection to test this kind of things ;)

  • @Richard_Nixon-mr6rq
    @Richard_Nixon-mr6rq Рік тому +1

    Could you make some videos about node.js eventemmiters, the resources for them have always sucked

  • @MerrickKing
    @MerrickKing Рік тому +2

    A nice method I use for simulating a slow connection is to run my React server out of a docker container, but use an Nginx container to proxy there configured to slow down the connection

    • @christian2409
      @christian2409 Рік тому +5

      You can also use the network speed in the browser development tools to slow down network requests

    • @christian2409
      @christian2409 Рік тому +1

      Firefox gives a lot of presets for different speeds

  • @Lion-mh9rq
    @Lion-mh9rq Рік тому

    This is really helpful , thank you!

  • @firewatermoonsun
    @firewatermoonsun Рік тому

    Excellent!

  • @elton-react-dev
    @elton-react-dev Рік тому

    Amazing video, thanks!!

  • @Kay8B
    @Kay8B 11 місяців тому

    I only ever worry about this once my bundles get bigger then 500kb otherwise, I'd prefer to load data ahead of time for a smoother app experience. If you do this with every component your app will just be loading states everywhere and it doesn't look/feel smooth.

  • @solomonowusu-ansah1751
    @solomonowusu-ansah1751 Рік тому

    This was really helpful

  • @ColinRichardson
    @ColinRichardson Рік тому

    Something Vue3 has improved. Using "defineAsyncComponent(()=> import(....));
    To make it much more apparent what is going on.

    • @ColinRichardson
      @ColinRichardson Рік тому +1

      @@internet4543 yeah, defineAsyncComponent is a function. Just a generic function wrote in JavaScript.. you import it like any other function.
      Unless you wanna say importing a function from a module is not vanilla enough for you.
      Can I also recommend some anger management. You seem to have gone from zero to 11 for no reason.

    • @permanar_
      @permanar_ Рік тому

      Go back to your Vue world, I guess?
      I mean, why do you feel the need to let everyone know there's something improved on Vue 3 specifically? Sure, you probably just wanna share it with everybody here but, again, just go back to your Vue world would be some kind action of you.
      Because why, you ask? Honestly speaking tho, we don't care about your shit and just go F urself ok?

    • @ColinRichardson
      @ColinRichardson Рік тому

      @@internet4543 Sorry, I was unaware you were unable to read my first message.. the anger makes sense now. You must feel the world is a scary place when you are unable to understand the simple things that go on around you. Just know we are all here to be friends and no one is laughing at you.

  • @germain1984
    @germain1984 Рік тому

    Is the lazy method in your routing example used by the Next.js team when using the component?

  • @Alessandro-nq3tm
    @Alessandro-nq3tm Рік тому

    Have you ever tried webpack's module federation plugin ?

  • @willyhorizont8672
    @willyhorizont8672 Рік тому

    Great. I use the lazy Suspense in my router too.
    But what about error baoundary? How and where to use it?

  • @kaayamarvin6755
    @kaayamarvin6755 Рік тому

    i have a question...... Can the fallback prop on the Suspense component take a component for example

  • @atirpok3
    @atirpok3 Рік тому

    Why not use default exports? What’s the use case for using named exports?

  • @valentineedesiriefagene7565

    Thank you

  • @Ram-sc6or
    @Ram-sc6or Рік тому

    Very helpful

  • @foreigninfluence
    @foreigninfluence Рік тому

    I was wiping my iPad screen during the intro because i thought my screen was dirty then i realize it is just the sweater lol

  • @FightFestAnime
    @FightFestAnime Рік тому

    Sure it speeds things up, however when its fully downloaded on the first go, it should be blazingly fast the next time, right?