Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions examples/next-js/src/app/api/counter/route.ts

This file was deleted.

5 changes: 3 additions & 2 deletions examples/next-js/src/app/api/rivet/[...all]/route.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { handlers } from "@/lib/rivet-server";
import { toNextHandler } from "@rivetkit/next-js";
import { registry } from "@/rivet/registry";

export const { GET, POST, PUT, PATCH, HEAD, OPTIONS } = handlers.value;
export const { GET, POST, PUT, PATCH, HEAD, OPTIONS } = toNextHandler(registry);
11 changes: 0 additions & 11 deletions examples/next-js/src/lib/counter.ts

This file was deleted.

13 changes: 2 additions & 11 deletions examples/next-js/src/lib/rivet-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@
import { createClient, createRivetKit } from "@rivetkit/next-js/client";
import type { registry } from "@/rivet/registry";

const getOrigin = () => {
if (typeof window !== "undefined") {
return window.location.origin;
}
// Fallback for SSR or when window is not available
return process.env.NEXT_PUBLIC_BASE_URL || "http://localhost:3000";
};
// TODO: Auto-trigger start by sending request to health endpoint

const client = createClient<typeof registry>({
endpoint: `${getOrigin()}/api/rivet`,
transport: "sse",
});
const client = createClient<typeof registry>();
export const { useActor } = createRivetKit(client);
14 changes: 0 additions & 14 deletions examples/next-js/src/lib/rivet-server.ts

This file was deleted.

45 changes: 14 additions & 31 deletions packages/next-js/src/mod.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,27 @@
import type { Registry, RunConfigInput } from "rivetkit";

type RegistryInstance = {
fetch: (request: Request) => Response | Promise<Response>;
};

const createRegistryInstance = (
export const toNextHandler = (
registry: Registry<any>,
inputConfig: RunConfigInput = {},
): RegistryInstance => {
console.log("=== CREATE NEXT HANDLER ===");

) => {
// Don't run server locally since we're using the fetch handler directly
inputConfig.disableDefaultServer = true;
// inputConfig.disableActorDriver = true;

// Configure serverless
const publicUrl =
process.env.NEXT_PUBLIC_SITE_URL ??
process.env.NEXT_PUBLIC_VERCEL_URL ??
`http://127.0.0.1:${process.env.PORT ?? 8080}`;
inputConfig.runnerKind = "serverless";
inputConfig.runEngine = true;
inputConfig.autoConfigureServerless = {
url: `${publicUrl}/api/rivet/start`,
};

// Next logs this on every request
inputConfig.noWelcome = true;

const { fetch } = registry.start(inputConfig);
return { fetch };
};

declare global {
// eslint-disable-next-line no-var
var rivetRegistry: RegistryInstance | undefined;
}

export const toNextHandler = (
registry: Registry<any>,
inputConfig: RunConfigInput = {},
) => {
console.log("=== CREATE REGISTRY INSTANCE ===");

// Store registry instance globally to survive Next.js hot reloads in development.
// Without this, the registry would reinitialize on every code change, losing actor state.
const registryInstance =
global.rivetRegistry ?? createRegistryInstance(registry, inputConfig);
if (process.env.NODE_ENV !== "production") {
global.rivetRegistry = registryInstance;
}

const fetchWrapper = async (
request: Request,
Expand All @@ -50,7 +33,7 @@ export const toNextHandler = (
newUrl.pathname = all.join("/");
const newReq = new Request(newUrl, request);

return await registryInstance.fetch(newReq);
return await fetch(newReq);
};

return {
Expand Down
Loading