question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom fetch per request

See original GitHub issue

Feature request

When using the load function in Svelte Kit, it provides a fetch implementation that can be used.

From the official docs:

SvelteKit’s load receives an implementation of fetch, which has the following special properties:

  • it has access to cookies on the server
  • it can make requests against the app’s own endpoints without issuing an HTTP call
  • it makes a copy of the response when you use it, and then sends it embedded in the initial page load for hydration

Describe the solution you’d like

I think that it would be necessary to specify the fetch on a “per request” basis. Something like this:

supabase.withFetch(fetch).from('mytable').select();

Full example:

<script context="module">
  export const prerender = true;
  import { supabase } from "$lib/initSupabase";

  export async function load({ params, fetch, session, stuff }) {
    let { data, error } = await supabase.from("users").select();

    if (error) {
      return {
        status: 500,
      };
    } else {
      return {
        props: {
          users: data,
        },
      };
    }
  }
</script>

Describe alternatives you’ve considered

Maybe I’m overthinking this. The way I’ve solved this so far is by creating a function:

export const getSupabase = (fetch) => createClient(url, anonKey, { fetch });

I then use it in my load function.

Is there a downside in creating multiple clients like this? If not, this issue can probably be closed.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
5hee75commented, Oct 11, 2022

There shouldn’t be an issue with using multiple clients, so closing this.

@soedirgo I have definitely encountered an issue with multiple clients, specifically when using auth listeners. I created a client factory, similar to OP, where each request to the SDK would create a new client. I did this so I could inject dynamic headers and settings into the client for each execution.

const supa = (...params) => createClient(...params);

supa().from...
supa().auth...
// etc

The issue I found was that auth listeners/events are registered to a specific client instance, not to the auth events themselves. I’m not sure if this is intended or not.

So in my example above, if I do this:

const supa = () => createClient(...);

supa().auth.onAuthStateChange((event, session) => {
  if (event === 'SIGNED_OUT') {
    console.log('Auth Event', event);
  }
});

supa().auth.signOut();

The listener is never called, presumably because I signed out from a different client instance. The auth token and user details are still removed from localStorage though.

0reactions
enyocommented, Oct 11, 2022

Oh man @5hee75 I was wondering forever why this happened in my app. This explains it perfectly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Create A Custom React Hook To Fetch And Cache Data
Throughout this article, we'll be making use of Hacker News Search API to build a custom hook which we can use to fetch...
Read more >
Using the Fetch API - MDN Web Docs
Supplying request options · Sending a request with credentials included · Uploading JSON data · Uploading a file · Uploading multiple files.
Read more >
Fetch API requesting multiple get requests - Stack Overflow
You can rely on Promises to execute them all before your then resolution. If you are used to jQuery, you can use jQuery...
Read more >
Create custom fetch hook for multiple Axios instances - ITNEXT
Customize how to fetch data with React hooks. francisrod01 ... Here is the easiest way to get rid of using setLoading in every...
Read more >
Building custom hooks in React to fetch Data
We retrieve the data using the API call and using built-in React hooks like useState, useEffect, and useReducer, the retrieved data is set...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found