dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.37k stars 9.99k forks source link

MVC view with _framework/blazor.web.js doesn't import *.lib.module.js #54397

Closed nfplee closed 7 months ago

nfplee commented 7 months ago

I've followed the instructions here to add the PageScript Blazor component (which wraps a page-script web component) to my Blazor component pages.

This works great, however my application also contains some MVC views and whilst scripts added using the page-script web component (to my MVC views) initially appeared to be working fine, I have found this is only the case when the landing page is from a Blazor component page.

Please see here for a minimal repo which shows the problem:

BlazorTest.zip

If you navigate to the home page, you'll see in the developer tools that "Loaded" is written to the console. Also clicking on MVC will correctly write MVC Loaded to the console. Now if you refresh the app (when on the MVC page), it will no longer output anything to the console.

Looking into this further, the _content/BlazorPageScript/BlazorPageScript.lib.module.js is imported when the landing page is a Blazor component page, but it's not imported when landing on an MVC page. I'm not sure why this would be the case as I have referenced _framework/blazor.web.js within my MVC master page, similar to how I do in the App.razor file.

javiercn commented 7 months ago

@nfplee thanks for contacting us.

This is by design. Blazor Web can't be mixed with MVC as the rendering engines on the server are completely different and Blazor Web relies on its SSR engine for many of the features.

nfplee commented 7 months ago

@javiercn thanks for the response, but aren't module scripts (from class libraries) automatically imported on the client from the blazor.web.js file? If so then why would this not work if that same script is referenced from MVC?

Edit

After looking into this further I realized the client needs to be aware of BlazorPageScript.lib.module.js file, looking at the source I noticed the following:

<!--Blazor-Web-Initializers:Ww0KICAiX2NvbnRlbnQvQmxhem9yUGFnZVNjcmlwdC9CbGF6b3JQYWdlU2NyaXB0LmxpYi5tb2R1bGUuanMiDQpd-->

This Base64 encoded string and translates to the following:

[
  "_content/BlazorPageScript/BlazorPageScript.lib.module.js"
]

Therefore I have made sure my MVC application does something similar.

zuizuihao commented 2 months ago

debug the usage of var file = _environment.WebRootFileProvider.GetFileInfo($"{_environment.ApplicationName}.modules.json");, see if modules.json exists.