Skip to content
Merged
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
29 changes: 29 additions & 0 deletions .notes/justin/worklogs/2025-10-09-deduplicate-router-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Deduplicate Router Types

## Problem

The types `RwContext`, `DocumentProps`, and `LayoutProps` are defined in both `sdk/src/runtime/lib/router.ts` and `sdk/src/runtime/lib/rwContext.ts`. This duplication can lead to inconsistencies. The file `rwContext.ts` is also poorly named as it contains more than just the `RwContext` type.

## Plan

1. Create a single source of truth for these shared types.
2. Rename `sdk/src/runtime/lib/rwContext.ts` to a more appropriate name like `sdk/src/runtime/lib/types.ts`.
3. Compare the duplicate type definitions, merge any differences, and place the consolidated types in the new file.
4. Remove the duplicate type definitions from `sdk/src/runtime/lib/router.ts`.
5. Update all imports to reference the new types file.

## PR Description

This change consolidates duplicated types into a single source of truth. The `RwContext`, `DocumentProps`, and `LayoutProps` types were previously defined in both `sdk/src/runtime/lib/router.ts` and `sdk/src/runtime/lib/rwContext.ts`.

To resolve this, `sdk/src/runtime/lib/rwContext.ts` has been renamed to `sdk/src/runtime/lib/types.ts` to better reflect its broader content. The duplicate definitions in `sdk/src/runtime/lib/router.ts` have been removed, and all imports now point to the new `types.ts` file. This ensures these core types are maintained in a single location.

## Build Fixes

After the initial changes, the build failed due to missing imports and incomplete `RwContext` objects. The following fixes were applied:

1. Added missing React import to `types.ts`
2. Updated all import statements that referenced the old `router.ts` exports to use `types.ts`
3. Added missing `entryScripts` and `inlineScripts` properties to `RwContext` objects in `worker.tsx` and `router.test.ts`

The build now completes successfully.
4 changes: 3 additions & 1 deletion sdk/src/runtime/lib/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from "react";
import { describe, expect, it } from "vitest";

import type { RequestInfo } from "../requestInfo/types";
import type { RwContext } from "./router";
import {
defineRoutes,
layout,
Expand All @@ -11,6 +10,7 @@ import {
render,
route,
} from "./router";
import type { RwContext } from "./types.js";

describe("matchPath", () => {
// Test case 1: Static paths
Expand Down Expand Up @@ -105,6 +105,8 @@ describe("defineRoutes - Request Handling Behavior", () => {
ssr: true,
databases: new Map(),
scriptsToBeLoaded: new Set(),
entryScripts: new Set(),
inlineScripts: new Set(),
pageRouteResolved: undefined,
} as RwContext,
cf: {} as any,
Expand Down
23 changes: 1 addition & 22 deletions sdk/src/runtime/lib/router.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,7 @@
import type { Kysely } from "kysely";
import React from "react";
import { isValidElementType } from "react-is";
import { RequestInfo } from "../requestInfo/types";

export type DocumentProps<T extends RequestInfo = RequestInfo> = T & {
children: React.ReactNode;
};

export type LayoutProps<T extends RequestInfo = RequestInfo> = {
children?: React.ReactNode;
requestInfo?: T;
};

export type RwContext = {
nonce: string;
Document: React.FC<DocumentProps<any>>;
rscPayload: boolean;
ssr: boolean;
layouts?: React.FC<LayoutProps<any>>[];
databases: Map<string, Kysely<any>>;
scriptsToBeLoaded: Set<string>;
pageRouteResolved: PromiseWithResolvers<void> | undefined;
actionResult?: unknown;
};
import type { DocumentProps, LayoutProps } from "./types.js";

export type RouteMiddleware<T extends RequestInfo = RequestInfo> = (
requestInfo: T,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type Kysely } from "kysely";
import React from "react";
import { type RequestInfo } from "../requestInfo/types.js";

export type RwContext = {
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/runtime/render/renderDocumentHtmlStream.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type DocumentProps } from "../lib/router.js";
import { type DocumentProps } from "../lib/types.js";
import { type RequestInfo } from "../requestInfo/types.js";
import { Preloads } from "./preloads.js";
import { Stylesheets } from "./stylesheets.js";
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/runtime/render/renderToStream.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FC, ReactElement } from "react";
import { injectRSCPayload } from "rsc-html-stream/server";
import { DocumentProps } from "../lib/router";
import { DocumentProps } from "../lib/types.js";
import { requestInfo } from "../requestInfo/worker";
import { renderDocumentHtmlStream } from "./renderDocumentHtmlStream";
import { renderToRscStream } from "./renderToRscStream";
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/runtime/render/renderToString.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FC, ReactElement } from "react";
import { DocumentProps } from "../lib/router";
import { DocumentProps } from "../lib/types.js";
import { renderToStream } from "./renderToStream";

export interface RenderToStringOptions {
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/runtime/requestInfo/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RwContext } from "../lib/router";
import { RwContext } from "../lib/types.js";

export interface DefaultAppContext {}

Expand Down
5 changes: 4 additions & 1 deletion sdk/src/runtime/worker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
} from "./requestInfo/worker";

import { ssrWebpackRequire } from "./imports/worker";
import { Route, type RwContext, defineRoutes } from "./lib/router";
import { Route, defineRoutes } from "./lib/router";
import type { RwContext } from "./lib/types.js";
import { generateNonce } from "./lib/utils";

export * from "./requestInfo/types";
Expand Down Expand Up @@ -96,6 +97,8 @@ export const defineApp = <
ssr: true,
databases: new Map(),
scriptsToBeLoaded: new Set(),
entryScripts: new Set(),
inlineScripts: new Set(),
pageRouteResolved: undefined,
};

Expand Down
Loading