Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue rendering after the first time #187

Open
bradleyables22 opened this issue Oct 21, 2024 · 14 comments
Open

Issue rendering after the first time #187

bradleyables22 opened this issue Oct 21, 2024 · 14 comments
Assignees

Comments

@bradleyables22
Copy link

bradleyables22 commented Oct 21, 2024

Hey! First off want to say what a great project and this already renders much better than BlazorMonaco.

The issue I'm facing is in a .NET 8 interactive page which is used to edit an item's content for display. The first time I visit the edit page (with your component in it) this works perfectly! But if I navigate away from the page and then back the editor does not render fully. If I refresh the page this resolves.

Should I be using this component in a global manor?

Current Logic Snippets:

<CodeMirror6Wrapper @ref=@editor @rendermode="InteractiveServer" 
    IsWASM =false
    Editable =true
    Language=CodeMirrorLanguage.Html
    Theme=ThemeMirrorTheme.XCodeDark
    AllowVerticalResize="false" 
    AutoFormatMarkdown="true" 
    Height="800"
    Doc=@content 
    DocChanged="(x)=>ContentChanged(x)" />

    protected override async Task OnInitializedAsync()
    {
        var state = await _auth.GetAuthenticationStateAsync();
        updatedBy = state?.User?.Identity?.Name ?? "Unknown User";

        using var _context = DbFactory.CreateDbContext();

        var end = await _context.Endpoints
        .Where(x => x.EndpointID == EndpointID)
        .FirstOrDefaultAsync();

        if (end is not null)
        {
            service = end.Service;
            content = end.Content ?? "Start Building Your Documentation Content";
            ready = true;
        }
        else
            _nav.NavigateTo("admin/endpoints");
    }

    private async Task ContentChanged(string c)
    {
    //set for preview page to snatch up
        content = c;
        await _local.SetAsync("endpoint", new PreviewContentItem { Content = content, Service = service });
    }

First Page Visit:

render1

Navigate await
Then navigate back

render2

Browser Console:
console

gaelj added a commit that referenced this issue Oct 22, 2024
This was referenced Oct 22, 2024
@gaelj
Copy link
Owner

gaelj commented Oct 22, 2024

Hi @bradleyables22, thanks for the feedback !

I edited the issue to fix the formatting of the code snippet - I hope you don't mind.

So I've just released 0.8.11 which exposes an InitializeAsync method (accessible via a ref to the component). It allows manually triggering the component's internal OnInitializedAsync lifecycle method. The idea is to allow you to call this method yourself to force a re-initialization of the component whenever needed.

See usage example in PR

If this does not solve your problem, would you mind submitting a PR in which you have adapted one of the example projects to reproduce the problem?

@gaelj gaelj self-assigned this Oct 22, 2024
@bradleyables22
Copy link
Author

Fast response time! But not seeming to do the trick. When I get a free day I'll pull and create an isolated page

@MadsatItide
Copy link

Bumping since I have the same issue. The InitializeAsync did not work for me either.

Has an page reproducing the problem been sent to you?

@gaelj
Copy link
Owner

gaelj commented Nov 5, 2024

No, but if you can I'll be happy to look into the issue.

As mentioned already, ideally I would need a pull request with one of the example projects in this repo adapted (or an additional example project created) to showcase the problem in a way where I can immediately work on it. It will also be helpful to test and avoid any regressions in the future.

@MadsatItide
Copy link

Sounds good, I will try and see if I can reproduce it in your examples. I will make another project where I have it set up and use it kind of like how I'm using it in my own solution, same settings for the Wrapper, etc.

Also a thing Ive noticed that is quite weird, its not so much after first render it breaks. Like I can close my component that has it embedded, as long as I stay on the same Page component, it is alright, but if I navigate to any other page and then back it breaks.

@MadsatItide
Copy link

I have managed to reproduce it, how do I make the pull request? I am not allowed to push to your repo.

@gaelj
Copy link
Owner

gaelj commented Nov 5, 2024

Great news !

So you need to fork this repo (use the GitHub gui) in order to be able to submit your changes in a pull request over here.

As your local changes have already been done on a clone of this repo, you will just need to modify your local repo's remote url to point to your fork, so that you can push to your GitHub fork and then (via GitHub gui) you can open a pull request to merge your changes over here.

More info on how to do this here https://kodekloud.com/blog/change-remote-origin-in-git/

@MadsatItide
Copy link

Thank you so much, you should be able to see the pull request with the sample project.

The sample project should be easy to find since it doesnt follow your naming conventions at all haha

@gaelj
Copy link
Owner

gaelj commented Nov 5, 2024

@bradleyables22 @MadsatItide

The problem is that Blazor's enhanced navigation doesn't play well with CM6's optimized DOM interactions.

One way to work around the problem is to "force reload" on all navigations to a page containing a CM6 editor. Not ideal, but better than nothing.

This can be achieved by adding the attribute data-enhance-nav="false" to the relevant Navlinks in NavMenu.razor.

If navigating with NavigationManager.NavigateTo(...), the forceLoad optional parameter should be set to true.


I noticed that Blazor requires a data-permanent attribute to be set to any element that will be modified by JS so I did that but it did not solve the issue. I'll release that change shortly.

I'm still investigating for a fix that would not require full page reload, but it's not the easiest problem to solve.

@gaelj
Copy link
Owner

gaelj commented Nov 5, 2024

Another way to avoid the problem is to have

<HeadOutlet @rendermode="InteractiveServer" />

in App.razor

@bradleyables22
Copy link
Author

Sorry I've been swamped at my 9-5! Glad someone is joining in on this! Will put some thought on the above statements. In blazor I see a future where global interactive mode is used sparingly.

@MadsatItide
Copy link

You're a legend Gael. Thank you so much.

@davhdavh
Copy link
Contributor

You probably need to register event listener on the enhancedload events blazor.addEventListener("enhancedload", callback).

Docs are here: https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/static-server-rendering?view=aspnetcore-9.0

gaelj added a commit that referenced this issue Nov 14, 2024
This was referenced Nov 14, 2024
@gaelj
Copy link
Owner

gaelj commented Nov 14, 2024

Thanks for the insight, @davhdavh . We now don't require any <HeadOutlet>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants