-
Notifications
You must be signed in to change notification settings - Fork 1
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
Uncaught TypeError: Cannot read properties of undefined (reading 'main')
when calling golang functions in subpages.
#5
Comments
Great observation! Based on your second solution I have came up with this solution which is to be placed on every route's >> NOTE: The following code has few problems. Please refer to the updated code two comments down. << import { browser } from "$app/environment";
if (browser && !window.hasOwnProperty("wailsbindings")) {
// Inject missing /wails/ipc.js and /wails/runtime.js
let wails_ipc = document.createElement("script");
wails_ipc.setAttribute("src", "/wails/ipc.js");
let wails_runtime = document.createElement("script");
wails_runtime.setAttribute("src", "/wails/runtime.js");
document.head.appendChild(wails_ipc);
document.head.appendChild(wails_runtime);
} This avoids repeated injection of the scripts. Looks like this solves this issue! |
Thanks for this! I'll double check this solution in wails and browser this weekend and update the repo accordingly. Both Svelte & JS are new to me but I had considered that there may be a way to check and avoid the repeat. I've shared this issue with the Wails discord due to the targets of v3; such as multi-window applications and local network web-apps. Which are certainly situations where both the user may choose to start at a location other than root and where it doesn't even necessarily make sense to launch from the root. It may be more beneficial to contain this snippet in the associated |
UpdateLooks like a better approach is to have the script injection code on the head tag itself: <svelte:head>
<script>
if (!window.hasOwnProperty("wailsbindings")) {
let wails_ipc = document.createElement("script");
wails_ipc.setAttribute("src", "/wails/ipc.js");
let wails_runtime = document.createElement("script");
wails_runtime.setAttribute("src", "/wails/runtime.js");
document.head.appendChild(wails_ipc);
document.head.appendChild(wails_runtime);
}
</script>
</svelte:head> Why was previous code a problem?Consider the above code:
<script lang="ts">
import { browser } from "$app/environment";
if (browser && !window.hasOwnProperty("wailsbindings")) {
let wails_ipc = document.createElement("script");
wails_ipc.setAttribute("src", "/wails/ipc.js");
let wails_runtime = document.createElement("script");
wails_runtime.setAttribute("src", "/wails/runtime.js");
document.head.appendChild(wails_ipc);
document.head.appendChild(wails_runtime);
}
</script>
<slot />
<script>
import { Greet } from "$lib/wailsjs/go/main/App.js";
import { onMount } from "svelte";
onMount(() => {
console.log(Greet("foo"));
})
</script> The result would be: Looks like the ipc connection gets established after the The new code
<svelte:head>
<script>
if (!window.hasOwnProperty("wailsbindings")) {
let wails_ipc = document.createElement("script");
wails_ipc.setAttribute("src", "/wails/ipc.js");
let wails_runtime = document.createElement("script");
wails_runtime.setAttribute("src", "/wails/runtime.js");
document.head.appendChild(wails_ipc);
document.head.appendChild(wails_runtime);
}
</script>
</svelte:head>
<slot /> The result will be: |
It's been 6 months. Are you still using the |
Situationally, yes. One of my projects needs it in the +layout and the
other doesn't. I've gone back and forth on including it.
…On Sun, Jul 30, 2023, 4:27 PM Isaac Andrade ***@***.***> wrote:
It's been 6 months. Are you still using the svelte:head solution above to
avoid routing issues?
—
Reply to this email directly, view it on GitHub
<#5 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ARUJFUP7LQFVA7EDKWE4SFLXS3NVVANCNFSM6AAAAAAUINWE5M>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Description
Calling golang functions in subpages such as
localhost:34115/foo
produces following error:But if we are to goto
/foo
from the homepage (/
) using an anchor tag as<a href="/foo">Goto foo</a>
the function call works as expected but as soon as we refresh the page, it breaks again.To Reproduce
Now browse
http://localhost:34115/foo
, enter a name and click onGreet
:Using anchor tag from homepage
Browse the home page, scroll to bottom and click on
Goto /foo
Scroll to bottom, enter a name and click on
Greet
:This works as expected. But if we refresh the page and again enter a name and click on
Greet
:Expected behaviour
System Details
Additional message
I have tried this only on linux.
The text was updated successfully, but these errors were encountered: