Skip to content
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

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

Open
cxjava opened this issue Nov 7, 2023 · 7 comments

Comments

@cxjava
Copy link

cxjava commented Nov 7, 2023

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!

@syumai
Copy link
Owner

syumai commented Nov 7, 2023

I'll check this later 👀

@syumai
Copy link
Owner

syumai commented Nov 7, 2023

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

@cxjava
Copy link
Author

cxjava commented Nov 8, 2023

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.

@kampfmodz
Copy link

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

@syumai
Copy link
Owner

syumai commented Dec 31, 2023

@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
Copy link
Owner

syumai commented Dec 31, 2023

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

@syumai
Copy link
Owner

syumai commented Dec 31, 2023

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:

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

No branches or pull requests

3 participants