2

Is it possible to load third-party javascript dynamically in client side blazor without having to include them in index.or host.cshtml file?

Does it make sense to include all js in index file even if project uses that script on only one page? Or that is just the way SPA works?

mko
  • 6,638
  • 12
  • 67
  • 118
  • Take a look [here](https://stackoverflow.com/questions/66598936/selective-loading-of-javascript) – DanielD Nov 14 '22 at 10:52

4 Answers4

2

As you have just one page in SPA, yes, that's make sense.
At the moment Blazor Client-Side doesnt' support lazy loading, so all the code is retrieve on application startup.

agua from mars
  • 16,428
  • 4
  • 61
  • 70
1

If you have a large script that is only used very infrequently by a small percentage of users (let's say advanced reports), it makes sense to load it dynamically.

Blazor will likely add support for this in the future.

In the meantime you can try a work around.

Sire
  • 4,086
  • 4
  • 39
  • 74
  • LazyLoad might be a good alternative. It's quite old, but is very simple to use and is only 1kb. see: https://github.com/rgrove/lazyload – Yogi Mar 07 '22 at 01:18
1

you need to load it by yourself, you can dynamicly add your script ( mostly usefull for external js ) or use javascript isolation ( for all local stuff ) - there are a lot of tutorials for both approaches.

Puschie
  • 138
  • 9
-1

In recent Blazor update, Blazor Web Assembly project support lazy loading the Assemblies at client side project. For more details refer Blazor Documentation.

Refer the article, https://visualstudiomagazine.com/articles/2020/08/27/blazor-updates.aspx

Suriya
  • 848
  • 10
  • 15
  • This seems unrelated to the question, which asks how to dynamically load JavaScript rather than assemblies. – Yogi Mar 07 '22 at 01:04