supabase icon indicating copy to clipboard operation
supabase copied to clipboard

Error when deploying to cloudflare workers

Open luizzappa opened this issue 1 year ago • 6 comments

Version

@nuxtjs/supabase: v1.2.0 nuxt: 3.8.1

Steps to reproduce

When using the useSupabaseUser() function on a CloudFlare Worker it returns the following error:

The 'cache' field on 'RequestInitializerDict' is not implemented

The problem started to occur in the new version v1.2.0 and from what I investigated the problem was due to an update to the supabase library as reported here. The whole problem is that the fetch used by the CloudFlare Worker does not support the cache parameter as described here.

In the case of supabase-js, the workaround is to remove the fetch cache header as described here.

However, as @nuxtjs/supabase uses the clientOptions property in nuxt.config.ts, the functions defined there do not work at run time. In other words, the code below does not work:

  supabase: {
    url: process.env.SUPABASE_URL,
    key: process.env.SUPABASE_KEY,
    serviceKey: process.env.SUPABASE_SERVICE_KEY,
    clientOptions: {
      global: {
+        fetch: (...args) => {
+          //// TEMP
+          // https://github.com/cloudflare/workerd/issues/698
+          if (args[1]?.cache) {
+            delete args[1].cache
+
+            if (!args[1]?.headers)
+              args[1].headers = {}
+
+            // @ts-ignore
+            args[1].headers['Cache-Control'] = 'no-store'
+          }
+          ////
+          return fetch(...args)
+        },
      }
    }
  },

So, this came to me with a question.. The way the library is currently implemented, how can we pass a custom fetch implementation, which is a feature of supabase-js?

luizzappa avatar Apr 01 '24 04:04 luizzappa

For anyone facing the same problem as me, the only alternative I could find was to downgrade at package.json

    "@nuxtjs/supabase": "1.1.7"

luizzappa avatar Apr 01 '24 04:04 luizzappa

In my case I had the error while trying to acces to serverSupabaseUser() on a server route using Cloudflare Pages.

What finally worked for me was to downgrade the auth-js package adding this line in the package.json file: "overrides": { "@supabase/auth-js": "^2.62.0" }

If it's still not working try to delete .nuxt and node_modules and package-lock.json file. Then run npm install.

This was the way to make it work for me. Hope it helps.

marcbejar avatar Apr 11 '24 23:04 marcbejar

This also causes issues with route protection on SSR, but it works fine on client only with Cloudflare pages.

"@nuxtjs/supabase": "1.1.7"

Fixed this for me also.

anthonyfranc avatar Apr 18 '24 16:04 anthonyfranc

Do you have any idea to work around this problem without downgrading the dependency?

vinceglb avatar Apr 19 '24 15:04 vinceglb

This PR seems to fix the problem: https://github.com/supabase/auth-js/pull/886

They upgrade the version of @supabase/auth-js in @supabase/supabase-js https://github.com/supabase/supabase-js/pull/1027 to fix https://github.com/supabase/supabase-js/issues/1001

By upgrading @supabase/supabase-js v2.42.7, this should fix the problem.

vinceglb avatar Apr 26 '24 09:04 vinceglb

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar May 06 '25 13:05 github-actions[bot]