Custom fetch per request
See original GitHub issueFeature 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:
- Created a year ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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.
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:
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.
Oh man @5hee75 I was wondering forever why this happened in my app. This explains it perfectly.