Skip to content

feat: expose remote function query data to a global variable #16191

Description

@jdgamble555

Describe the problem

In Classic SvelteKit, you can get all page data anywhere in your app through the page variable (previously $page). This is specifically available under page.data for data that is returned.

You cannot do this with a Remote Function query(). The data is NOT available globally, although it can be cached in multiple components.

Imagine this scenario:

  • Layout (Breadcrumb) - no await
    • Users (await getUserList())
      • Jonathan (await getUser())
    • Posts (await getPostList())
      • My Post 1 (await getPostById(params.id))
        • Comment 2 (await getCommentById(params.comment_id))
    • About -- no await

And I want to display a simple breadcrumb:

Home -> Users -> Jonathan
Home -> Posts -> SvelteKit Remote Functions Tips
Home -> About

I can set the Breadcrumbs in the +layout.svelte file, BUT I am actually querying the data on the page itself... (username, page title, comment title, or no query at all, etc)

This CAN be solved using +page.server.ts for the query, and setting the crumb data in the +layout.svelte with page.data.

However, this is impossible to work using Remote Function queries.

Describe the proposed solution

Either:

  1. Expose the query data globally in page.query... like page.query.myFunc...
  2. Expose the query data globally in query or query.data... like query.myFunc...

Alternatives considered

  1. Use +page.server.ts instead of query() to set the data using page.data... this can mess up your query refreshes etc, since it DOES NOT use query() and has you revert back to old paradigms.
  2. Use hooks.server.ts to replace your entire HTML with something like %crumbs% that you query in query() and set like event.locals.query... etc... basically just hack the HTML right before render.

Importance

would make my life easier

Additional Information

Basically I can't use Remote Functions without it, since I have shared data in a layout.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions