gaelj / BlazorCodeMirror6

Blazor wrapper for CodeMirror 6
MIT License
20 stars 2 forks source link

CSS is lost on page navigation in static SSR with page-scoped interactive server directive #81

Closed Gaz83 closed 9 months ago

Gaz83 commented 9 months ago

So this issue I am finding very difficult to replicate, tried in your examples and it just works fine.

Anyway, the issue.

This is all in Blazor Web App targeting .Net 8

I have the control on a page and it wall works great with theme applied etc. Now, if I navigate to another page which also has the control on or if navigate away and come back, the control looks like it looses all css formatting i.e. you have the line numbers followed by content, all of this just stacked vertically with overflow happening, all theming loss etc.

I can't attach an example project as it's all confidential stuff and producing an example is not working.

Just wondered if you may know of something that may cause this. All other controls on the pages work fine.

Pressing F5/refresh does fix it

Gaz83 commented 9 months ago

I managed to create an example! it seems to happen if you have the control hidden and then show it after navigating.

Gaz83 commented 9 months ago

BlazorApp1.zip

Attached project.

Steps Run it and use nav on left and go to Index1 Click button "Reset text" to show the Control, it will have json content. Use nav on left and go to Index2 Click button "Reset text" to show the Control. Observe that all styling is not applied.

gaelj commented 9 months ago

Thanks for creating an example of your issue. I will look into it.

gaelj commented 9 months ago

The sample can be fixed by replacing, in App.razor, <HeadOutlet /> with <HeadOutlet @rendermode="InteractiveServer" />.

Would that be acceptable ?

Edit: fyi the problematic behavior is unrelated to your "reset text" button ; I could reproduce it without.

Code Mirror 6 generates its CSS dynamically and uses this library https://github.com/marijnh/style-mod/blob/master/src/style-mod.js to append the CSS to the page's head.

I find it actually quite surprising that the CSS works on the first page load.

I can try to extract this dynamic CSS into a static file but it will have to be thoroughly tested, as I don't know right now if the CSS can get updated later than during the init, and changing the theme dynamically definitely won't be possible.

gaelj commented 9 months ago

So I confirm that the 2nd approach seems to work fine, although it could be viewed as a horrible hack.

I cannot - for now - guarantee this won't have any other side effects, than being unable to switch themes, so I currently don't feel super comfortable with providing this CSS in the wrapper library. Of the 2, I'd recommend using <HeadOutlet @rendermode="InteractiveServer" />, if possible.

Gaz83 commented 9 months ago

I have just taken a look and I can confirm that HeadOutlet works fine for me in my situation. Not sure if this will work for someone who wishes to use Static server-side rendering.

Have you tried adding @rendermode="InteractiveServer" to the CodeMirror component/wrapper?

gaelj commented 9 months ago
gaelj commented 9 months ago

@Gaz83 can we close this issue ? I added an example project with the new hosting model.

gaelj commented 9 months ago

@Gaz83 feel free to reopen if needed

Gaz83 commented 9 months ago

@gaelj Sorry for late reply, been a bit tied up with other things in life. Yes I was happy for this to be closed, thank you.