The Biggest ISSUE With Blazor (and how to fix it)

Поділитися
Вставка
  • Опубліковано 8 чер 2024
  • One of the biggest issues with Blazor in .NET 8 is that it doesn't offer a smooth transition from the pre-rendering to interactive rendering. In practice, we end up having the OnInitialized method being called twice. Which can render our applications unusable. However, there's an easy fix for that! In this video I will show you exactly what the problem is and how to solve it!
    #dotnet #blazor #aspnetcore
    JOIN the Codewrinkles newsletter: codewrinkles.eo.page/newsletter
    Join this channel to get source code access and other perks:
    / @codewrinkles
    CONTENT
    1. Intro: 00:00
    2. Regular SSR: 00:36
    3. SSR + interactivity = TROUBLE: 01:52
    4. Let's fix it! 04:00
  • Наука та технологія

КОМЕНТАРІ • 59

  • @Codewrinkles
    @Codewrinkles  6 місяців тому +7

    A short clarification. According MS documentation, there is a way for stateful reconnects that could potentially also be applied to this scenarios. And this would be to use some kind of memory stream to keep track of the loaded data and when the render process goes the second time through OnInitialized you can get the data from the stream directly. However, I don't think that a simple task in all modern SPA frameworks (like initializing data) should required this level of complexity in our applications.

  • @StateHasChanged
    @StateHasChanged 6 місяців тому +9

    I love Blazor 8, but MS needs to do a better job of dog fooding their stuff. When I ported my Blazor Server app to the Blazor Web template, I had this same issue. Took me a good hour of internet research to find this same workaround. For my app, I set this globally on the routes component, as it is a SPA.

    • @Tekner436
      @Tekner436 6 місяців тому +1

      it's crazy that other ssr frameworks have things like this figured out

  • @majormartintibor
    @majormartintibor 6 місяців тому +3

    Very helpful video, thanks!

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

    Thank you for the valuable information. I have spent many hours to resolve this problem

  • @AmerBakeer
    @AmerBakeer 6 місяців тому +2

    Wonderful !! Thank you for the valuable information. It will help me solve many problems.

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

    nice workaround. I'm sure I'll use it

  • @Duelweb
    @Duelweb 6 місяців тому +1

    Super stuff, thanks!

  • @liammcmullen4497
    @liammcmullen4497 2 місяці тому

    thanks for a big heads-up

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

    I tried to use this, but I found out that my login page is blank (with background) if prerendering is disabled. Is it a bug or is it something wrong? Thank you for all your hard work!

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

    Nice. I'm sure I will use this soon. You have also lost a l ot of weight, whatever u are doing is working. Great job man

  • @abhayprince
    @abhayprince 6 місяців тому +4

    For this issue,I have discussed the PersistComponentState approach in detail ua-cam.com/video/qQ91rdMgIMM/v-deo.html

    • @Codewrinkles
      @Codewrinkles  6 місяців тому +1

      Nice one. Thanks for sharing.

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

    how can i sync between sqlite on mobile offline and remote database (restapi with sql server ) ?

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

    how to manually authenticate with blazor SSR

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

    Could you do a video about Authentification without using Identity in Blazor?

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

    Thank you very much! In a global server mode project I successfully applied the changes in App.Razor component in order to avoid OnInitialized be called twice. It worked fine but if I use Individual Accounts Authentication a problem with login has occurs. After searching, I found that the problem is that http context is null and the login page is not loading. What can be done?

    • @Codewrinkles
      @Codewrinkles  6 місяців тому +1

      Yes, activating global serber mode effectively turns your app in a Blazor Server app. Please look up the Blazor Server playlist on the channel since I have plenty of videos on Blazor Server Authentication.

  • @AryaDinesh-jd5du
    @AryaDinesh-jd5du 28 днів тому +1

    When the page is refreshed, the issue is not resolved even though it works for the initial rendering.

    • @justfair4181
      @justfair4181 4 дні тому +1

      You're right. This video is not up to date or even correct. All comments saying 'thank you' to the author are owner's friends probably.

  • @geraldmaale
    @geraldmaale 6 місяців тому +3

    For whatever reason, I couldn't reproduce this behaviour using the Blazor Web App with server interactivity location set to Per page/component. The breakpoint set on the OnInitializedAsync method was not hit twice but once.
    Did you miss something or I missed something?

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

      I'm not the only one complaining about this issue. I'm not sure why you couldn't reproduce it. It's just as easy as having the template and making the delay 5 seconds and adding @rendermode InteractiveServer. It's weird however and gives me food for thought.

    • @Codewrinkles
      @Codewrinkles  6 місяців тому +1

      Are you sure you added @rendermode InteractiveServer? The rendermode needs to be activated manually. By default everything is SSR. The first demo I made in the video, everything was working, but it was without interactivity

    • @geraldmaale
      @geraldmaale 6 місяців тому +1

      @@Codewrinkles Yeah my bad., I overlooked that part. After that correction, it worked as in your demo.
      Based on the advise given by DanRoth and Co., I decided to always make a component and not slap the @rendermode in them.

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

      Yeah, but this is not always possible. I mentioned this approach in the "Everything you need to know about Blazor" video. My approach is to also try to keep everything as much SSR as possible. However, sometimes you need to communicate between parent and child components and in that case you can't escape this problem every time.

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

      @@geraldmaale This way it will not render correctly when calling the component from a link or menu. You'll need the @rendermode specified within the component if you also route to it.

  • @AmerBakeer
    @AmerBakeer Місяць тому +1

    Unfortunately, I think disabling/bypassing Pre-rendering harms SEO and prevents search engines from seeing/indexing page content, even though the solution is useful from a UX point of view.
    I eventually had to re-enable it because search engines saw pages as empty.
    Are you aware of a better solution that still allows prerendering to remain enabled?

    • @justfair4181
      @justfair4181 4 дні тому +1

      You're right: prerendering can help with search engine optimization (SEO) since the initial HTML content is generated on the server and can be crawled by search engines

  • @carolinacordero6616
    @carolinacordero6616 2 місяці тому +1

    On a fresh new application based on the default Blazor Web App Template, I never see the "Loading..." message at 1:28, and at Network tab, the request shows as: Status (pending), not immediately 200 as in your demo. Can anyone help me?

    • @justfair4181
      @justfair4181 4 дні тому +1

      Yes , you are right. This video is a modified Blazor app and totally incorrect.

  • @user-fqlt
    @user-fqlt 5 місяців тому

    Thanks very useful. +1 sub

  • @user-ff7ei4ob8b
    @user-ff7ei4ob8b 6 місяців тому +1

    why not just make preconfigured global static class for configured render modes? same as RenderMode set in _imports.razor

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

      I don't quite get what this would do other then my entire app would become interactively rendered. What am mI missing?

  • @justfair4181
    @justfair4181 5 днів тому +1

    The video is not up to date or is a modified Blazor Server app or even a Blazor Server app created from .NET 7 migrated to .NET8 later. There is no InteractiveServer rendermode in Blazor Server in .NET8 . Also under Pages in the video I am not seeing _Host.cstml file.

    • @Codewrinkles
      @Codewrinkles  5 днів тому

      Sorry, but you're totally off on this one. The entire model has changed in .NET 8 and everything is SSR by default. Even if youwant to create a Blazor Server application it just creates a Blazor SSR one (for auth components) and adds server interactivity globally via an if check. Same goes for assembly. I also have a video on getting started with Blazor on .NET 8. I think that one might be really useful.

    • @justfair4181
      @justfair4181 4 дні тому

      @@Codewrinkles You are right on this word: you're 'totally' wrong. Please read my comments again. I am using .NET since 2003 and these days I'm under . NET 8. so 100% you're wrong.

  • @user-uo7ch2lf3z
    @user-uo7ch2lf3z 6 місяців тому +1

    Thank you.
    I hope Microsoft would take this as a bug and it properly fixed.

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

      Strictly speaking, this is not a bug. This is how things are supposed to work in this model. You have a server predener and then an interactive render process. But I agree it's a fairly big inconvenience and should at least be documented in a more proper way.

    • @user-uo7ch2lf3z
      @user-uo7ch2lf3z 6 місяців тому

      @@Codewrinkles Besides documentation, there should be a formal way to prevent OnInitalizedAsync() from executing twice.
      Maybe just certain syntatic sugar like:
      @rendermode InteractiveServerNotPrerendered
      Nice quick fix, anyway.

  • @emcwealth
    @emcwealth 4 місяці тому

    You can actually move the data load and delay into onafterrender instead oninit, first render only of course

    • @Codewrinkles
      @Codewrinkles  4 місяці тому

      That is not an optimal experience in my opinion since at that point the HTML is already rendered. There will be strange flickers of the UI that end users will experience.

    • @emcwealth
      @emcwealth 4 місяці тому

      @Codewrinkles there are no strange flickers... I write 100% of my code this way in blazor wasm... screen renders with loading or spinner while data loads and then is presented to user on screen... ux is wonderful at that point... in fact doing data loads in onitnit causes the odd behaviors

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

    I'm making an app where SEO is very important so don't think this will be a viable work around for me unfortunately...

    • @Codewrinkles
      @Codewrinkles  6 місяців тому +5

      I guess you will need to define which is more important: SEO or data loading twice. An alternate workaround would be to move the Weather into an entire new component. Then have the @rendermode set on the component, not on the page level. This will allow you to still be SEO-friendly and also avoi loading data twice.

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

    what is the down side of this approach? There may be a reason why Microsoft set pretender default value equals true.

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

    How To Add Localization In Blazor | Multi-Language Support with arabic and english or any languages

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

    hi @codewrinkles does blazor run on IIS or some new webserver?

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

      Blazor is An #aspnetcore app and therefore it can run wherever you want it to run, just like any other aspnetcore app.

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

      I mean as is what webserver on windows? IIS? or is there a new webserver?

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

    👍 "promosm"