Skip to content

fix run fetch failed#199

Closed
labulakalia wants to merge 1 commit into
syumai:mainfrom
labulakalia:main
Closed

fix run fetch failed#199
labulakalia wants to merge 1 commit into
syumai:mainfrom
labulakalia:main

Conversation

@labulakalia

Copy link
Copy Markdown
Contributor

What

panic: JavaScript error: Illegal invocation: function called with incorrect this reference. See https://developers.cloudflare.com/workers/observability/errors/#illegal-invocation-errors for details.

goroutine 10 [running]:
syscall/js.Value.Call({{}, 0x7ff8000100000005, 0x0}, {0x5d3f2, 0x5}, {0x493dd8, 0x2, 0x2})
/home/labulakalia/sdk/go/src/syscall/js/js.go:443 +0x3b
github.com/syumai/workers/cloudflare/fetch.fetch({{}, 0x7ff8000100000005, 0x0}, 0x48c280, 0x493e60)
/home/labulakalia/go/pkg/mod/github.com/syumai/workers@v0.32.0/cloudflare/fetch/bind.go:18 +0x7
github.com/syumai/workers/cloudflare/fetch.(*Client).Do(...)
/home/labulakalia/go/pkg/mod/github.com/syumai/workers@v0.32.0/cloudflare/fetch/http.go:29
main.main.func1({0x9dd50, 0x482190}, 0x48c140)
/home/labulakalia/workspace/testworker/main.go:23 +0x10
net/http.HandlerFunc.ServeHTTP(0x6a698, {0x9dd50, 0x482190}, 0x48c140)
/home/labulakalia/sdk/go/src/net/http/server.go:2322 +0x4
net/http.(*ServeMux).ServeHTTP(0x2a7da0, {0x9dd50, 0x482190}, 0x48c140)
/home/labulakalia/sdk/go/src/net/http/server.go:2861 +0x2e
github.com/syumai/workers.handleRequest.func1()
/home/labulakalia/go/pkg/mod/github.com/syumai/workers@v0.32.0/handler_js.go:81 +0x4
created by github.com/syumai/workers.handleRequest in goroutine 9
/home/labulakalia/go/pkg/mod/github.com/syumai/workers@v0.32.0/handler_js.go:78 +0x29

illegal-invocation

@syumai

syumai commented May 4, 2026

Copy link
Copy Markdown
Owner

Thanks! However, overwriting the global fetch could introduce other unexpected issues, so I'll fix it on the caller side instead.
This problem may have been introduced in #190.

@labulakalia labulakalia closed this May 4, 2026
@labulakalia labulakalia reopened this May 4, 2026
@labulakalia

labulakalia commented May 4, 2026

Copy link
Copy Markdown
Contributor Author
[wrangler:error] TypeError: Cannot read properties of undefined (reading 'exports')
    at syscall/js.valueNew (file:///home/labulakalia/workspace/testgo/build/wasm_exec.js:409:24)
    at [object Object]
    at [object Object]
    at [object Object]
    at [object Object]
    at [object Object]
    at globalThis.Go._resume (file:///home/labulakalia/workspace/testgo/build/wasm_exec.js:567:23)
    at Object.handleRequest (file:///home/labulakalia/workspace/testgo/build/wasm_exec.js:578:8)
    at Object.fetch (file:///home/labulakalia/workspace/testgo/build/worker.mjs:42:18)
    at async jsonError (file:///home/labulakalia/workspace/testgo/node_modules/wrangler/templates/middleware/middleware-miniflare3-json-error.ts:22:10)
[wrangler:info] GET /hello 500 Internal Server Error (33ms)
⎔ Shutting down local server...
➜  testgo cat go.mod
module testfetch

go 1.25.9

require github.com/syumai/workers v0.32.1-0.20260504054939-ca82fc42c19a

I use main branch , but it's not fix this problem
@syumai

@labulakalia

Copy link
Copy Markdown
Contributor Author
func fetch(namespace js.Value, req *http.Request, init *RequestInit) (*http.Response, error) {
	if namespace.IsUndefined() {
		return nil, errors.New("fetch function not found")
	}
	fetchFunc := namespace.Get("fetch")
	promise := fetchFunc.Invoke(
		// The Request object to fetch.
		// Docs: https://developers.cloudflare.com/workers/runtime-apis/request
		jshttp.ToJSRequest(req),
		// The content of the request.
		// Docs: https://developers.cloudflare.com/workers/runtime-apis/request#requestinit
		init.ToJS(),
	)

this code is work

func fetch(namespace js.Value, req *http.Request, init *RequestInit) (*http.Response, error) {
	if namespace.IsUndefined() {
		return nil, errors.New("fetch function not found")
	}
	promise := namespace.Call("fetch",
		// The Request object to fetch.
		// Docs: https://developers.cloudflare.com/workers/runtime-apis/request
		jshttp.ToJSRequest(req),
		// The content of the request.
		// Docs: https://developers.cloudflare.com/workers/runtime-apis/request#requestinit
		init.ToJS(),
	)

this code is not work
go version

go version go1.25.9 linux/amd64

@syumai

syumai commented May 4, 2026

Copy link
Copy Markdown
Owner

I recognize that it is necessary to explicitly distinguish between calling the global fetch and the service bindings fetch when implementing the Go side.

It seems that the previous patch broke the global fetch while fixing the service bindings fetch.

I plan to implement the code in a way that distinguishes between the two so that both can be supported.

@labulakalia labulakalia closed this May 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants