Skip to content

Pages: one pages/_app setup hook for Vue, React, Svelte, and Solid #941

Backlog

Description

@hyfdev

#915 asks for a place to install Vue plugins such as Pinia, and #936 adds one for Vue only.

React, Svelte, and Solid apps need the same for providers such as TanStack Query, so the hook should have one shape across all four.

Proposal: pages/_app.ts, or .tsx/.jsx for JSX, default-exports a synchronous setup. Void calls it once per request on the server, and in the browser once for the page and once per island.

export default function setup({ app }) { // Vue gets { app }; React, Svelte, and Solid get {}
  return {
    Providers, // a component rendered around every page and island
    props, // props for Providers
    dehydrate, // called after the whole response is rendered
    hydrate, // called before each browser root mounts
  };
}

Every field is optional. In React, for example:

// pages/_app.tsx
import { QueryClient, QueryClientProvider, dehydrate, hydrate } from '@tanstack/react-query';

export default function setup() {
  const client = new QueryClient();
  return {
    Providers: ({ children }) => <QueryClientProvider client={client}>{children}</QueryClientProvider>,
    dehydrate: () => dehydrate(client),
    hydrate: (state) => hydrate(client, state),
  };
}

Svelte needs props, because a Providers component written in .svelte can't reach objects created in setup.

The state goes in its own script at the end of the body, after React's stream ends, so it stays out of prerendered page data.

For Vue, this changes #936's setup(app) to setup({ app }).

#936 implements it for all four frameworks.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions