Skip to content

Commit c3af3f0

Browse files
committed
refactor: improve head content link deduplication by using a Set and lowercasing rel values.
1 parent 5e3a906 commit c3af3f0

3 files changed

Lines changed: 24 additions & 15 deletions

File tree

packages/react-router/src/headContentUtils.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export const useTags = () => {
9292
select: (state) => {
9393
const constructedLinks: Array<RouterManagedTag> = []
9494
const relsToDedupe = new Set(['canonical'])
95-
const linksByRel: Record<string, true> = {}
95+
const linksByRel = new Set<string>()
9696

9797
for (let i = state.matches.length - 1; i >= 0; i--) {
9898
const match = state.matches[i]!
@@ -101,11 +101,14 @@ export const useTags = () => {
101101

102102
for (let j = matchLinks.length - 1; j >= 0; j--) {
103103
const link = matchLinks[j]!
104-
if (link.rel && relsToDedupe.has(link.rel)) {
105-
if (linksByRel[link.rel]) {
106-
continue
104+
if (link.rel) {
105+
const rel = link.rel.toLowerCase()
106+
if (relsToDedupe.has(rel)) {
107+
if (linksByRel.has(rel)) {
108+
continue
109+
}
110+
linksByRel.add(rel)
107111
}
108-
linksByRel[link.rel] = true
109112
}
110113

111114
constructedLinks.push({

packages/solid-router/src/headContentUtils.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export const useTags = () => {
9393
select: (state) => {
9494
const constructedLinks: Array<RouterManagedTag> = []
9595
const relsToDedupe = new Set(['canonical'])
96-
const linksByRel: Record<string, true> = {}
96+
const linksByRel = new Set<string>()
9797

9898
for (let i = state.matches.length - 1; i >= 0; i--) {
9999
const match = state.matches[i]!
@@ -102,11 +102,14 @@ export const useTags = () => {
102102

103103
for (let j = matchLinks.length - 1; j >= 0; j--) {
104104
const link = matchLinks[j]!
105-
if (link.rel && relsToDedupe.has(link.rel)) {
106-
if (linksByRel[link.rel]) {
107-
continue
105+
if (link.rel) {
106+
const rel = link.rel.toLowerCase()
107+
if (relsToDedupe.has(rel)) {
108+
if (linksByRel.has(rel)) {
109+
continue
110+
}
111+
linksByRel.add(rel)
108112
}
109-
linksByRel[link.rel] = true
110113
}
111114

112115
constructedLinks.push({

packages/vue-router/src/headContentUtils.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export const useTags = () => {
7777
select: (state) => {
7878
const constructedLinks: Array<RouterManagedTag> = []
7979
const relsToDedupe = new Set(['canonical'])
80-
const linksByRel: Record<string, true> = {}
80+
const linksByRel = new Set<string>()
8181

8282
for (let i = state.matches.length - 1; i >= 0; i--) {
8383
const match = state.matches[i]!
@@ -86,11 +86,14 @@ export const useTags = () => {
8686

8787
for (let j = matchLinks.length - 1; j >= 0; j--) {
8888
const link = matchLinks[j]!
89-
if (link.rel && relsToDedupe.has(link.rel)) {
90-
if (linksByRel[link.rel]) {
91-
continue
89+
if (link.rel) {
90+
const rel = link.rel.toLowerCase()
91+
if (relsToDedupe.has(rel)) {
92+
if (linksByRel.has(rel)) {
93+
continue
94+
}
95+
linksByRel.add(rel)
9296
}
93-
linksByRel[link.rel] = true
9497
}
9598

9699
constructedLinks.push({

0 commit comments

Comments
 (0)