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
14 changes: 8 additions & 6 deletions packages/react-router/src/HeadContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ export const useTags = () => {
const resultMeta: Array<RouterManagedTag> = []
const metaByAttribute: Record<string, true> = {}
let title: RouterManagedTag | undefined
;[...routeMeta].reverse().forEach((metas) => {
;[...metas].reverse().forEach((m) => {
if (!m) return
for (let i = routeMeta.length - 1; i >= 0; i--) {
const metas = routeMeta[i]!
for (let j = metas.length - 1; j >= 0; j--) {
const m = metas[j]
if (!m) continue

if (m.title) {
if (!title) {
Expand All @@ -32,7 +34,7 @@ export const useTags = () => {
const attribute = m.name ?? m.property
if (attribute) {
if (metaByAttribute[attribute]) {
return
continue
} else {
metaByAttribute[attribute] = true
}
Expand All @@ -45,8 +47,8 @@ export const useTags = () => {
},
})
}
})
})
}
}

if (title) {
resultMeta.push(title)
Expand Down
11 changes: 5 additions & 6 deletions packages/router-core/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import invariant from 'tiny-invariant'
import {
createControlledPromise,
deepEqual,
findLast,
functionalUpdate,
last,
pick,
Expand Down Expand Up @@ -1441,13 +1442,11 @@ export class RouterCore<
undefined,
).matchedRoutes

const matchedFrom = [...allCurrentLocationMatches]
.reverse()
.find((d) => {
return comparePaths(d.fullPath, fromPath)
})
const matchedFrom = findLast(allCurrentLocationMatches, (d) => {
return comparePaths(d.fullPath, fromPath)
})

const matchedCurrent = [...allFromMatches].reverse().find((d) => {
const matchedCurrent = findLast(allFromMatches, (d) => {
return comparePaths(d.fullPath, currentLocation.pathname)
})

Expand Down
11 changes: 11 additions & 0 deletions packages/router-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,14 @@ export function isPromise<T>(
typeof (value as Promise<T>).then === 'function',
)
}

export function findLast<T>(
array: ReadonlyArray<T>,
predicate: (item: T) => boolean,
): T | undefined {
for (let i = array.length - 1; i >= 0; i--) {
const item = array[i]!
if (predicate(item)) return item
}
return undefined
}
Comment on lines +487 to +496
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@Sheraff Sheraff Aug 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.findLast is still a little bit recent (safari 15.4), and I didn't want this PR to be a breaking change. But I'd love to use the built-in array method instead.

15 changes: 9 additions & 6 deletions packages/solid-router/src/HeadContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ export const useTags = () => {
const resultMeta: Array<RouterManagedTag> = []
const metaByAttribute: Record<string, true> = {}
let title: RouterManagedTag | undefined
;[...routeMeta()].reverse().forEach((metas) => {
;[...metas].reverse().forEach((m) => {
if (!m) return
const routeMetasArray = routeMeta()
for (let i = routeMetasArray.length - 1; i >= 0; i--) {
const metas = routeMetasArray[i]!
for (let j = metas.length - 1; j >= 0; j--) {
const m = metas[j]
if (!m) continue

if (m.title) {
if (!title) {
Expand All @@ -33,7 +36,7 @@ export const useTags = () => {
const attribute = m.name ?? m.property
if (attribute) {
if (metaByAttribute[attribute]) {
return
continue
} else {
metaByAttribute[attribute] = true
}
Expand All @@ -46,8 +49,8 @@ export const useTags = () => {
},
})
}
})
})
}
}

if (title) {
resultMeta.push(title)
Expand Down