Skip to content

Commit

Permalink
feat(react-router-dom): support future options (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daydreamer-riri committed Dec 8, 2024
1 parent 053c8d6 commit 5e88714
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/client/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ export function ViteReactSSG(
const isClient = typeof window !== 'undefined'

const BASE_URL = routerOptions.basename ?? '/'
const { v7_startTransition, ...routerFeature } = routerOptions.future ?? {}

async function createRoot(client = false, routePath?: string) {
const browserRouter = client ? createBrowserRouter(convertRoutesToDataRoutes(routerOptions.routes, transformStaticLoaderRoute), { basename: BASE_URL }) : undefined
const browserRouter = client
? createBrowserRouter(
convertRoutesToDataRoutes(routerOptions.routes, transformStaticLoaderRoute),
{ basename: BASE_URL, future: routerFeature },
)
: undefined

const appRenderCallbacks: Function[] = []
const onSSRAppRendered = client
Expand Down Expand Up @@ -106,7 +112,7 @@ export function ViteReactSSG(
const { router } = await createRoot(true)
const app = (
<HelmetProvider>
<RouterProvider router={router!} />
<RouterProvider router={router!} future={{ v7_startTransition }} />
</HelmetProvider>
)
const isSSR = document.querySelector('[data-server-rendered=true]') !== null
Expand Down
4 changes: 2 additions & 2 deletions src/node/router-adapter/remix.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class RemixAdapter implements IRouterAdapter<ViteReactSSGContext> {
}

async render(path: string) {
const { base, routes, getStyleCollector } = this.context
const { base, routes, getStyleCollector, routerOptions } = this.context
const fetchUrl = `${withTrailingSlash(base)}${removeLeadingSlash(path)}`
const request = createRequest(fetchUrl)
const styleCollector = getStyleCollector ? await getStyleCollector() : null
Expand All @@ -34,7 +34,7 @@ export class RemixAdapter implements IRouterAdapter<ViteReactSSGContext> {
throw _context

routerContext = _context
const router = createStaticRouter(dataRoutes, routerContext)
const router = createStaticRouter(dataRoutes, routerContext, { future: routerOptions.future })
let app = (
<HelmetProvider context={helmetContext}>
<StaticRouterProvider router={router} context={routerContext} />
Expand Down
12 changes: 10 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ReactElement, ReactNode } from 'react'
import type { IndexRouteObject, NonIndexRouteObject, createBrowserRouter } from 'react-router-dom'
import type { FutureConfig as CompFutureConfig, IndexRouteObject, NonIndexRouteObject, createBrowserRouter } from 'react-router-dom'
import type { Options as BeastiesOptions } from 'beasties'

type Router = ReturnType<typeof createBrowserRouter>
Expand Down Expand Up @@ -198,10 +198,18 @@ export type IndexRouteRecord = IndexRouteObject & CommonRouteOptions

export type RouteRecord = NonIndexRouteRecord | IndexRouteRecord

export interface RouterFutureConfig {
v7_fetcherPersist?: boolean
v7_normalizeFormMethod?: boolean
v7_partialHydration?: boolean
v7_relativeSplatPath?: boolean
v7_skipActionErrorRevalidation?: boolean
}

export interface RouterOptions {
routes: RouteRecord[]
createFetchRequest?: <T>(req: T) => Request
basename?: string
future?: Partial<RouterFutureConfig & CompFutureConfig>
}

export interface StyleCollector {
Expand Down

0 comments on commit 5e88714

Please sign in to comment.