Skip to content

How to stream queries to Pages/Components? #22

@dihmeetree

Description

@dihmeetree

Instead of doing the following (to prerender queries on the server):

// +page.ts
import { trpc } from '$lib/trpc/client';
import type { PageLoad } from './$types';

export const load: PageLoad = async (event) => {
  const { queryClient } = await event.parent();
  const api = trpc(event, queryClient);
  return {
    queries: await api.createServerQueries((t) => [
      t.authed.todos.all(),
      t.public.hello.get()
    ])
  };
};

I'd like to be able to stream the result of queries for example on my page. I'm new to Svelte, but I know that Svelte does support streaming data via promises. Apparently if you don't await the data in the server load function, you can use the await block on the client to show a loading fallback while the promise resolves.

For example (From: https://kit.svelte.dev/docs/load#streaming-with-promises):

// +page.svelte.ts
{#await data.streamed.comments}
  Loading...
{:then value}
  {value}
{/await}

In relation to createServerQueries, is there a way I can do streaming with it, so I can have faster page loads? Any advice/guidance would be super appreciated!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions