Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(start): supply links() with same context signature as meta() #2427

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
35 changes: 34 additions & 1 deletion packages/react-router/src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,40 @@ export interface UpdatableRouteOptions<
params: ResolveAllParamsFromParent<TParentRoute, TParams>
loaderData: ResolveLoaderData<TLoaderFn>
}) => Array<React.JSX.IntrinsicElements['meta']>
links?: () => Array<React.JSX.IntrinsicElements['link']>
links?: (ctx: {
matches: Array<
RouteMatch<
TRouteId,
TFullPath,
ResolveAllParamsFromParent<TParentRoute, TParams>,
ResolveFullSearchSchema<TParentRoute, TSearchValidator>,
ResolveLoaderData<TLoaderFn>,
ResolveAllContext<
TParentRoute,
TRouterContext,
TRouteContextFn,
TBeforeLoadFn
>,
TLoaderDeps
>
>
match: RouteMatch<
TRouteId,
TFullPath,
ResolveAllParamsFromParent<TParentRoute, TParams>,
ResolveFullSearchSchema<TParentRoute, TSearchValidator>,
ResolveLoaderData<TLoaderFn>,
ResolveAllContext<
TParentRoute,
TRouterContext,
TRouteContextFn,
TBeforeLoadFn
>,
TLoaderDeps
>
params: ResolveAllParamsFromParent<TParentRoute, TParams>
loaderData: ResolveLoaderData<TLoaderFn>
}) => Array<React.JSX.IntrinsicElements['link']>
scripts?: () => Array<React.JSX.IntrinsicElements['script']>
headers?: (ctx: {
loaderData: ResolveLoaderData<TLoaderFn>
Expand Down
10 changes: 8 additions & 2 deletions packages/react-router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1217,15 +1217,14 @@ export class Router<
loaderDeps,
invalid: false,
preload: false,
links: route.options.links?.(),
scripts: route.options.scripts?.(),
staticData: route.options.staticData || {},
loadPromise: createControlledPromise(),
fullPath: route.fullPath,
}
}

// If it's already a success, update the meta and headers
// If it's already a success, update the meta, links, and headers
// These may get updated again if the match is refreshed
// due to being stale
if (match.status === 'success') {
Expand All @@ -1236,6 +1235,13 @@ export class Router<
loaderData: match.loaderData,
})

match.links = route.options.links?.({
matches,
match,
params: match.params,
loaderData: match.loaderData,
})

match.headers = route.options.headers?.({
loaderData: match.loaderData,
})
Expand Down
12 changes: 11 additions & 1 deletion packages/start/src/client/serialization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,19 @@ export function afterHydrate({ router }: { router: AnyRouter }) {
})
: undefined

const links =
match.status === 'success'
? route.options.links?.({
matches: router.state.matches,
match,
params: match.params,
loaderData: match.loaderData,
})
: undefined

Object.assign(match, {
meta,
links: route.options.links?.(),
links,
scripts: route.options.scripts?.(),
})
})
Expand Down