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

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.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 3
  • Comments: 16 (6 by maintainers)

Most upvoted comments

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.

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

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

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.

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.

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.

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