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

Blazor Web App (.net8) - error: Cannot resolve '_blazor' when started by - electronize start #823

Open
Elangovan4ever opened this issue Nov 15, 2023 · 6 comments
Labels

Comments

@Elangovan4ever
Copy link

  • Version:
    .Net Version: Net8.0
    ElectronNET.API: 23.6.1
    ElectronNET.CLI Version: 23.6.1.0

.Net Version: Net8.0
Node.js version: v18.15.0

  • Target:
    Windows 10 Enterprise
    Windows 11 Pro
    Tried in both windows versions.

Issue:
When I run the blazor web app through electronize start command, the app starts in electron window, but no click event works. In the dev tools console window it shows the error as "blazor.web.js:1 Uncaught (in promise) Error: Cannot resolve '_blazor'.".

Test Project on GitHub: https://github.com/elandev4ever/SimpleBlazorWebApp

Steps to Reproduce:

  1. Create a "Blazor Web App" project in Visual studio 2022 version 17.8.0. Select "Net 8 Long Term Support" as framework while creating the project. Please note that for .Net8 Microsoft removed "Blazor Server" template and introduced "Blazor Web App" in project workloads. Now the Blazor script file also named "blazor.web.js" instead of "blazor.server.js".
  2. Run from Visual Studio as web app. it runs fine. This is default app code which has a Counter page. In the counter page, click on "Click Me" button. The number increases as expected.
  3. Added Nuget package "ElectronNET.API" to the project. Added electron create window code in program.cs [https://github.com/elandev4ever/SimpleBlazorWebApp/blob/master/SimpleBlazorWebApp/Program.cs]
  4. In Windows Command prompt navigate to app folder, and run the following commands:
    dotnet tool install ElectronNET.CLI -g
    electronize init
    electronize start
  5. Application opens in electron window. clicking on "Click Me" button in Counter page does not work. In the dev tools console window the following error is shown.
    blazor.web.js:1 Uncaught (in promise) Error: Cannot resolve '_blazor'.
    at xn._resolveUrl (blazor.web.js:1:93927)
    at new xn (blazor.web.js:1:83462)
    at Ln.build (blazor.web.js:1:99418)
    at Oo.startConnection (blazor.web.js:1:135970)
    at Oo.startCore (blazor.web.js:1:135408)
    at Oo.start (blazor.web.js:1:135061)
    at nr (blazor.web.js:1:151076)

Note: this issue might be related to the render modes that are introduced in .Net8. If I keep the render mode per page, and then remove the render mode attribute from razor page the error is not shown in console, but still the button click wont work because of no render mode.

since .net 8 just made official today, I guess the support for this might not exist yet. Until this sorted out, any temporary workaround would be great.

@peter-bozovic
Copy link

Same issue here ...

Blazor Server project that was working with net7, just changed the TargetFramework to net8 and have the same error !

Hope that can be fixed so that we can migrate to the latest LTS dotnet

@rubenmonteiro
Copy link

rubenmonteiro commented Dec 3, 2023

Hi all.
I have the same issue.
I created a project with the .net8 template "Blazor Web App", with auto render mode.
I was able to understand, at least in my case, that the framework inserts a file "_framework/blazor.web.js" on the site.
Besides other stuff done, this js file is responsible for creating websocket connections to the server.
The problem occurs in this creation.
The code in the js is the following, extracted from Developer tools created by Electron window
When this method is called, the parameter 'e' has the value '_blazor'

_resolveUrl(e) {
                if (0 === e.lastIndexOf("https://", 0) || 0 === e.lastIndexOf("http://", 0))
                    return e;
                if (!Wt.isBrowser)
                    throw new Error(`Cannot resolve '${e}'.`);
                const t = window.document.createElement("a");
                return t.href = e,
                this._logger.log(Ot.Information, `Normalizing '${e}' to '${t.href}'.`),
                t.href
            }

the problem is in the condition 'if (!Wt.isBrowser)', this condition returns false the anchor tag is not generated to return the href necessary.

the code of the Wt class is this one
class Wt { static get isBrowser() { return !Wt.isNode && "object" == typeof window && "object" == typeof window.document } static get isWebWorker() { return !Wt.isNode && "object" == typeof self && "importScripts"in self } static get isReactNative() { return !Wt.isNode && "object" == typeof window && void 0 === window.document } static get isNode() { return "undefined" != typeof process && process.release && "node" === process.release.name } }
the method isBrowser returns false because the condition in the method isNode is true.
If there is a way in Electron to set the process release name to something different to the value 'node' it could work, because the process class in the browser, chrome, edge, etc is undefined.

I don't know if this is related

Code in aspnetcore repo

this is the same condition

@peter-bozovic
Copy link

Thank you @rubenmonteiro for the deep dive into the source code of the issue !

So, the workaround that seem to work is to add
<script>process = undefined;</script>
just before the
<script src="_framework/blazor.web.js"></script>

@rubenmonteiro
Copy link

Thank you @rubenmonteiro for the deep dive into the source code of the issue !

So, the workaround that seem to work is to add <script>process = undefined;</script> just before the <script src="_framework/blazor.web.js"></script>

I will test and give the feedback.
I was searching for a workaround also.

@rubenmonteiro
Copy link

rubenmonteiro commented Dec 3, 2023

@peter-bozovic it seems to work, no issue found until now. thanks for the tip, the workarounds I thought were a lot more complicated then this

@kenZhou2
Copy link

kenZhou2 commented Jan 10, 2024

@peter-bozovic does anyone have any issue running the windows exe on release or debug? It seems that electron isnt being read

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

No branches or pull requests

4 participants