diff --git a/packages/react-router/src/HeadContent.tsx b/packages/react-router/src/HeadContent.tsx index fb9d995e43e..3115915ffbf 100644 --- a/packages/react-router/src/HeadContent.tsx +++ b/packages/react-router/src/HeadContent.tsx @@ -17,9 +17,11 @@ export const useTags = () => { const resultMeta: Array = [] const metaByAttribute: Record = {} 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) { @@ -32,7 +34,7 @@ export const useTags = () => { const attribute = m.name ?? m.property if (attribute) { if (metaByAttribute[attribute]) { - return + continue } else { metaByAttribute[attribute] = true } @@ -45,8 +47,8 @@ export const useTags = () => { }, }) } - }) - }) + } + } if (title) { resultMeta.push(title) diff --git a/packages/router-core/src/router.ts b/packages/router-core/src/router.ts index c4409961b83..745f6874b15 100644 --- a/packages/router-core/src/router.ts +++ b/packages/router-core/src/router.ts @@ -8,6 +8,7 @@ import invariant from 'tiny-invariant' import { createControlledPromise, deepEqual, + findLast, functionalUpdate, last, pick, @@ -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) }) diff --git a/packages/router-core/src/utils.ts b/packages/router-core/src/utils.ts index 4afd82d87bc..2e6a6e399cd 100644 --- a/packages/router-core/src/utils.ts +++ b/packages/router-core/src/utils.ts @@ -483,3 +483,14 @@ export function isPromise( typeof (value as Promise).then === 'function', ) } + +export function findLast( + array: ReadonlyArray, + 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 +} diff --git a/packages/solid-router/src/HeadContent.tsx b/packages/solid-router/src/HeadContent.tsx index 0484a92de33..820473e3b31 100644 --- a/packages/solid-router/src/HeadContent.tsx +++ b/packages/solid-router/src/HeadContent.tsx @@ -18,9 +18,12 @@ export const useTags = () => { const resultMeta: Array = [] const metaByAttribute: Record = {} 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) { @@ -33,7 +36,7 @@ export const useTags = () => { const attribute = m.name ?? m.property if (attribute) { if (metaByAttribute[attribute]) { - return + continue } else { metaByAttribute[attribute] = true } @@ -46,8 +49,8 @@ export const useTags = () => { }, }) } - }) - }) + } + } if (title) { resultMeta.push(title)