Skip to content

Custom fetch per request #438

Open
Open
@enyo

Description

@enyo

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.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions