Skip to content

Conversation

@drew-harris
Copy link
Contributor

@drew-harris drew-harris commented Oct 31, 2025

The PR adds a utility function exported from core, that allows the user to sync auth state to a cookie that a server can read.

It also creates a Framework class in core that makes http requests to get triples and keeps a cache of results. It's designed to be framework agnostic, if we want to add ssr to other frameworks, other cool things.

I created a new init function / suspense provider under a /nextjs import in the react package.

Important

I renamed a backend route from /runtime/query to /runtime/framework/query to make it more clear that the route is meant to only be used with the FrameworkClient class. It also changes so that if an incorrect refresh token is used, it will ignore the refresh token all together and query anonymously.

Example Repo Readme:

Version to try: 0.22.92-experimental.drewh-ssr.20348370870.1

InstantDB Next.JS SSR Example

This repo demonstrates NextJS SSR and cookie sync. Built on top of create-instant-app's NextJS todo example.

Setup Cookie Sync (Optional)

  1. Create a file at src/app/api/instant/route.ts like so:
import { createInstantRouteHandler } from "@instantdb/react/nextjs";

export const { POST } = createInstantRouteHandler({
  appId: process.env.NEXT_PUBLIC_INSTANT_APP_ID!,
});
  1. Add the endpoint to your database config in src/lib/db.ts
import { init } from "@instantdb/react/nextjs";
import schema from "../instant.schema";

export const db = init({
  appId: process.env.NEXT_PUBLIC_INSTANT_APP_ID!,
  firstPartyPath: "/api/instant",
  schema,
  useDateObjects: true,
});

Setup SSR

Create an InstantProvider

"use client"

import { InstantSuspenseProvider } from "@instantdb/react/nextjs";
import { type User } from "@instantdb/react";
import { db } from "@/lib/db"

export default function InstantProvider({
  children,
  user,
}: {
  children: React.ReactNode;
  user: User;
}) {
  return (
    <InstantSuspenseProvider user={user} db={db}>
      {children}
    </InstantSuspenseProvider>
  );
}```

### Use InstantProvider in `layout` 

// src/app/layout.tsx 

```typescript
import InstantProvider from "./InstantProvider";
import { getUserOnServer } from "@instantdb/react/nextjs";

export default async function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  const user = await getUserOnServer(process.env.NEXT_PUBLIC_INSTANT_APP_ID!);
  return (
    <html lang="en">
      <body className="antialiased">
        <InstantProvider user={user}>{children}</InstantProvider>
      </body>
    </html>
  );
}

Using SSR

Instead of using db.useQuery you can use db.useSuspenseQuery instead.

If a page is rendering on the server, the server will make an HTTP request to Instant to retrieve the results before rendering.

If the page is running on the client, it will re-use the existing webhook connection.

SSR and Auth

The db.useAuth function works as normal, except that if you provide the user object to InstantSuspenseProvider higher in the component tree, it will skip a loading state.

This also works with <db.SignedIn> and <db.SignedOut> allowing you to skip any flashes of loading spinners or blank pages.

@github-actions
Copy link
Contributor

View Vercel preview at instant-www-js-drewh-ssr-jsv.vercel.app.

@drew-harris drew-harris force-pushed the drewh/ssr branch 8 times, most recently from 7ce8b7d to cf4c75d Compare November 5, 2025 17:42
Copy link
Contributor

@stopachka stopachka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work @drew-harris !

@drew-harris drew-harris force-pushed the drewh/ssr branch 4 times, most recently from 72e391b to 9d33768 Compare November 5, 2025 19:32
Copy link
Contributor

@stopachka stopachka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good to me!

@drew-harris drew-harris force-pushed the drewh/ssr branch 6 times, most recently from 332a95b to 4e70126 Compare November 9, 2025 02:28
Copy link
Contributor

@stopachka stopachka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ship it sir!!!

@drew-harris drew-harris merged commit 1b654c5 into main Dec 19, 2025
27 checks passed
@drew-harris drew-harris deleted the drewh/ssr branch December 19, 2025 23:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants