workers
workers copied to clipboard
JavaScript error: The 'credentials' field on 'RequestInitializerDict' is not implemented."
Hi @syumai , Thank you for building this great repository!
I try to deploy this tool to Cloudflare Pages.
When I want to deploy my repository into pages, I got this issue JavaScript error: The 'credentials' field on 'RequestInitializerDict' is not implemented." in my local and remote pages ENV.
I search this error, like this issue, seems the Cloudflare Workers add the credentials automatically, Do you know how to disable it? Or can we manually deploy it to pages without use wrangler.
Can you help to check it? Thank you!
I'll check this later π
@cxjava Thank you for reporting this issue. I cloned your repository and succeeded to deploy. https://cf-pages-demo-cxjava.pages.dev/
If it's a bug of wrangler, please send issue to wrangler's repository. (In that case, the same error will occur without syumai/workers)
Do you know how to disable it? Or can we manually deploy it to pages without use wrangler.
I'm sorry, I don't know about it.
Hi @syumai , Thank you for digging this issue!
Do you know how to disable it? Or can we manually deploy it to pages without use wrangler.
Let's skip it, it is my assumption.
I don't describe it very detail, make you confused! Sorry about that!
I update the code, When I want to call third party API in the pages API, it will throw the JavaScript error: The 'credentials' field on 'RequestInitializerDict' is not implemented.
package main
import (
"fmt"
"io"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/syumai/workers"
)
func main() {
r := chi.NewRouter()
r.Route("/api", func(r chi.Router) {
r.Get("/hello", func(w http.ResponseWriter, req *http.Request) {
name := req.URL.Query().Get("name")
if name == "" {
name = "Pages Functions"
}
fmt.Fprintf(w, "Hello, %s!", name)
})
r.Get("/hello2", func(w http.ResponseWriter, req *http.Request) {
// βπhere I call other service API, such as your deploy to pages.β
resp, err := http.DefaultClient.Get(`https://cf-pages-demo-cxjava.pages.dev/api/hello`)
if err != nil {
fmt.Fprintf(w, "Hello, Hello world! plus "+err.Error())
}
defer resp.Body.Close()
body, err := io.ReadAll(req.Body)
fmt.Fprintf(w, "Hello, Hello world! plus remote "+string(body))
})
r.Get("/hello3", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "Hello, Hello, Hello world!")
})
})
workers.Serve(r)
}
In the /api/hello2 , I call https://cf-pages-demo-cxjava.pages.dev/api/helloit will return Hello, Pages Functions!,
if I call my service in the local curl http://localhost:8788/api/hello2
expected it will return Hello, Hello world! plus remote Hello, Pages Functions!
but it throw exception.
i have the same error, have you fixed this ? @cxjava
@cxjava @kampfmodz
I couldn't figure out why the The 'credentials' field on 'RequestInitializerDict' is not implemented error occurs, but instead of using http.DefaultClient.Get, you can use fetch.Client.Do as follows.
import "github.com/syumai/workers/cloudflare/fetch"
r.Get("/hello2", func(w http.ResponseWriter, req *http.Request) {
const url = "https://cf-pages-demo-cxjava.pages.dev/api/hello"
fetchReq, err := fetch.NewRequest(req.Context(), http.MethodGet, url, nil)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "err: %v", err)
return
}
cli := fetch.NewClient()
resp, err := cli.Do(fetchReq, nil)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "err: %v", err)
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
fmt.Fprintf(w, "Hello, Hello world! plus remote "+string(body))
})
Maybe the problem occurs around here... https://github.com/golang/go/blob/go1.21.5/src/net/http/roundtrip_js.go#L82-L86
The problem is:
- Cloudflare Workers does not support "credentials" field in the fetch function's RequestInit arg option.
- Go's (*http.Transport).RoundTrip implicitly adds "credentials" field to RequestInit.
- This causes the error: "The 'credentials' field on 'RequestInitializerDict' is not implemented"
The solution is:
- Please use alternative package instead of
net/http.