workers icon indicating copy to clipboard operation
workers copied to clipboard

JavaScript error: The 'credentials' field on 'RequestInitializerDict' is not implemented."

Open cxjava opened this issue 1 year ago β€’ 7 comments

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!

cxjava avatar Nov 07 '23 08:11 cxjava

I'll check this later πŸ‘€

syumai avatar Nov 07 '23 08:11 syumai

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

syumai avatar Nov 07 '23 13:11 syumai

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.

cxjava avatar Nov 08 '23 01:11 cxjava

i have the same error, have you fixed this ? @cxjava

kampfmodz avatar Dec 29 '23 09:12 kampfmodz

@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))
		})

syumai avatar Dec 31 '23 06:12 syumai

Maybe the problem occurs around here... https://github.com/golang/go/blob/go1.21.5/src/net/http/roundtrip_js.go#L82-L86

syumai avatar Dec 31 '23 06:12 syumai

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:

syumai avatar Dec 31 '23 08:12 syumai