wails icon indicating copy to clipboard operation
wails copied to clipboard

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

Open sithembiso opened this issue 2 years ago • 16 comments

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.

sithembiso avatar Dec 31 '22 06:12 sithembiso

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.

diamondap avatar Jan 23 '23 19:01 diamondap

@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?

gwynforthewyn avatar Jan 24 '23 04:01 gwynforthewyn

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

diamondap avatar Jan 24 '23 09:01 diamondap

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.

gwynforthewyn avatar Jan 24 '23 13:01 gwynforthewyn

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

ldlac avatar Feb 14 '23 19:02 ldlac

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 avatar Apr 10 '23 18:04 firelizzard18

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

firelizzard18 avatar Apr 10 '23 18:04 firelizzard18

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.

stffabi avatar Apr 10 '23 18:04 stffabi

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.

AF250329 avatar Jun 04 '23 15:06 AF250329

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

sgx-wb avatar Aug 11 '23 10:08 sgx-wb

image image

sgx-wb avatar Aug 11 '23 10:08 sgx-wb

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.

W1M0R avatar Sep 18 '23 20:09 W1M0R

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

leaanthony avatar Sep 18 '23 21:09 leaanthony

@leaanthony That works for me

firelizzard18 avatar Sep 23 '23 04:09 firelizzard18

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

bjesuiter avatar Apr 29 '24 17:04 bjesuiter

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.

wsndshx avatar Jun 29 '24 09:06 wsndshx