wailsapp / wails

Create beautiful applications using Go
https://wails.io
MIT License
24.12k stars 1.15k forks source link

Random "TypeError: undefined is not an object (evaluating 'window['go']['backend']')" #2262

Open sithembiso opened 1 year ago

sithembiso commented 1 year ago

Description

I am getting this error every time the app hot-reloads. First run is fine, but if I change anything on the frontend, I get a blank screen with this error in the console:

TypeError: undefined is not an object (evaluating 'window['go']['backend']')

Full output:

[Error] TypeError: undefined is not an object (evaluating 'window['go']['backend']')
    IsDark (Model.js:22)
    Module Code (index.tsx:33)
    evaluate
    moduleEvaluation
    (anonymous function)
    promiseReactionJob

The files it's pointing at are auto-generated.

To Reproduce

Honestly, I am not sure as it seemed to just appear out of the blue. For me it happens consistently. Whenever I update the front end, I have to kill wails dev and re-run it. Otherwise, I'm stuck with a blank screen.

Expected behaviour

I expected it to reload like before without any errors.

Screenshots

Screenshot 2022-12-31 at 07 56 33

Attempted Fixes

No response

System Details

Wails CLI v2.3.1

Scanning system - Please wait (this may take a long time)...Done.

# System

OS           | MacOS
Version      | 13.0.1
ID           | 22A400
Go Version   | go1.19.3
Platform     | darwin
Architecture | arm64

# Wails

Version | v2.3.1

# Dependencies

Dependency                | Package Name | Status    | Version
Xcode command line tools  | N/A          | Installed | 2396
npm                       | N/A          | Installed | 8.11.0
*Xcode                    | N/A          | Available |
*upx                      | N/A          | Available |
*nsis                     | N/A          | Installed | v3.08
* - Optional Dependency

# Diagnosis

Your system is ready for Wails development!
Optional package(s) installation details:
  - Xcode: Available at https://apps.apple.com/us/app/xcode/id497799835
  - upx : Available at https://upx.github.io/

 ♥   If Wails is useful to you or your company, please consider sponsoring the project:
https://github.com/sponsors/leaanthony


### Additional context

This happens both on macOS and Windows as well.
diamondap commented 1 year ago

I started seeing a similar error on Linux. Same behavior, started out of the blue and was happening consistently, but the error message was:

TypeError: undefined is not an object (evaluating 'window['go']['application']')

This turned out to be an error in my code. I had a link that was supposed to look like this:

<a href="#" data-func="myFunction">

I have a listener dispatching events. When a link with data-func is clicked, the dispatcher finds and calls the function.

The problem was, I had actually written my link like this:

<a href="myFunction">

When I clicked it, I got a blank screen and the error message above. The web view was trying to navigate to a non-existent URL, hence the blank screen and the loss of reference to window['go']. There was no obvious error telling me the webview was trying to load an unloadable resource.

If your error appeared out of the blue and occurs consistently like mine, comb back through your code for recent changes. The problem may not even be in JavaScript. It could be an HTML error, like mine.

I'm not trying to say you haven't found a legitimate bug. I'm just trying to help out. I hope this info is useful to someone.

gwynforthewyn commented 1 year ago

@sithembiso wails dev exposes two ports, one on localhost:3000 and the other on localhost:34115. Not sure if this is your issue, but if you choose localhost:3000 you'll get this problem happening.

Any chance that's your issue?

diamondap commented 1 year ago

I don't think so. But again, the problem was in my code, not in wails.

gwynforthewyn commented 1 year ago

I don't think so. But again, the problem was in my code, not in wails.

I've miscommunicated, sorry! My question was aimed at the original reporter. I've updated it to clarify that.

I love your report and fix, though! It's interesting how there's no error coming through for your case.

ldlac commented 1 year ago

Similar behavior here.

I have a reproductible case, looks like wails doesn't like refreshing when it's not on /

I have react-router installed and everytime it refreshes when I am not on my main page /, backend functions are undefined, I need to go back to home / then everything is fine.

and it matches with the href error of @diamondap

Using a memory router, solves this

firelizzard18 commented 1 year ago

I'm seeing the same behavior as @ldlac. The error I get is undefined is not an object (evaluating 'window['go']['main']') and it goes away if I execute location.replace('/'). So it appears that something weird happens if the app is refreshed and the path is not /.

firelizzard18 commented 1 year ago

I worked around this by adding if (!('go' in window)) location.replace('/') to main.jsx

stffabi commented 1 year ago

That's a known issue, currently the runtime is only injected for / or /index.html. If a SPA framework is used, the runtime is injected for / if it then internally changes the state to something like /myroute everything work fine. If now a reload is done with /myroute the runtime is not injected anymore and results in this error.

Please also see https://github.com/wailsapp/wails/issues/2047 for more information. There are plans to give the developer more freedom to specify where it should be injected, maybe also give them the possibility to trigger the injection with a meta-tag.

AF250329 commented 1 year ago

I am using Angular template and this error always happens. So I added

<head>
 ...
...
  <!-- Wails runtime: start -->
  <script src="/wails/ipc.js"></script>
  <script src="/wails/runtime.js"></script>
  <!-- Wails runtime: until here -->
</head>

to always inject wails runtime.

sgx-wb commented 1 year ago

I have the same problem. Is there a solution? I use vue+vue-router. image

sgx-wb commented 1 year ago

image image

W1M0R commented 11 months ago

I've also hit this issue. It's quite serious, and effectively makes it impossible to embed MPA projects (such as those built by Astro) with Wails, especially if your app uses redirects. This affects the built production bundle and the dev experience.

leaanthony commented 11 months ago

I can't comment on how each front-end project hads been built to run but you can just add script tags to your pages to load the necessary code: https://wails.io/docs/guides/frontend/#script-injection

firelizzard18 commented 11 months ago

@leaanthony That works for me

bjesuiter commented 3 months ago

I can't comment on how each front-end project hads been built to run but you can just add script tags to your pages to load the necessary code: https://wails.io/docs/guides/frontend/#script-injection

THANKS for pointing that out! I was ready to pull my hair out.

Now it works flawlessly! <3

wsndshx commented 1 month ago

I have the same problem. Is there a solution? I use vue+vue-router. image

我想你可以使用 createWebHashHistory 或者是 createMemoryHistory 模式来解决这个问题。只需修改 frontend\src\router\index.ts 文件即可。 I think you can solve this issue by using the createWebHashHistory or createMemoryHistory mode. Simply modify the frontend\src\router\index.ts file.

image

我比较推荐使用 createWebHashHistory 模式,因为 createMemoryHistory 模式会导致刷新后回到首页。 I would recommend using the createWebHashHistory mode, as the createMemoryHistory mode will cause the page to return to the home page after a refresh.