Skip to content

Commit

Permalink
fix(react-router): ensure that meta/links/scripts/headers are execute…
Browse files Browse the repository at this point in the history
…d after match resolves
  • Loading branch information
schiller-manuel committed Dec 19, 2024
1 parent 253284a commit ffdd2ca
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions packages/react-router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2587,31 +2587,13 @@ export class Router<

await potentialPendingMinPromise()

const headFnContent = route.options.head?.({
matches,
match: this.getMatch(matchId)!,
params: this.getMatch(matchId)!.params,
loaderData,
})
const meta = headFnContent?.meta
const links = headFnContent?.links
const scripts = headFnContent?.scripts

const headers = route.options.headers?.({
loaderData,
})

updateMatch(matchId, (prev) => ({
...prev,
error: undefined,
status: 'success',
isFetching: false,
updatedAt: Date.now(),
loaderData,
meta,
links,
scripts,
headers,
}))
} catch (e) {
let error = e
Expand Down Expand Up @@ -2678,12 +2660,32 @@ export class Router<
loadPromise?.resolve()
}

updateMatch(matchId, (prev) => ({
...prev,
isFetching: loaderRunningAsync ? prev.isFetching : false,
loaderPromise: undefined,
invalid: false,
}))
updateMatch(matchId, (prev) => {
const route = this.looseRoutesById[prev.routeId]!
const headFnContent = route.options.head?.({
matches,
match: prev,
params: prev.params,
loaderData: prev.loaderData,
})
const meta = headFnContent?.meta
const links = headFnContent?.links
const scripts = headFnContent?.scripts

const headers = route.options.headers?.({
loaderData: prev.loaderData,
})
return {
...prev,
isFetching: loaderRunningAsync ? prev.isFetching : false,
loaderPromise: undefined,
invalid: false,
meta,
links,
scripts,
headers,
}
})
return this.getMatch(matchId)!
})(),
)
Expand Down

0 comments on commit ffdd2ca

Please sign in to comment.