Skip to content

Commit ecd89c8

Browse files
authored
fix(query-devtools/utils): scope the 'setupStyleSheet' dedup check to the target so a 'shadowDOMTarget' still receives its own '#_goober' style tag when 'document.head' already has one (#10815)
1 parent 01c7634 commit ecd89c8

3 files changed

Lines changed: 21 additions & 9 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/query-devtools': patch
3+
---
4+
5+
fix(query-devtools/utils): scope the 'setupStyleSheet' dedup check to the target so a 'shadowDOMTarget' still receives its own '#_goober' style tag when 'document.head' already has one

packages/query-devtools/src/__tests__/utils.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,19 @@ describe('Utils tests', () => {
10421042
expect(styleTags).toHaveLength(1)
10431043
expect(styleTags[0]?.getAttribute('nonce')).toBe('first-nonce')
10441044
})
1045+
1046+
it('should install the style tag into the "ShadowRoot" target even when "document.head" already has one', () => {
1047+
const host = document.createElement('div')
1048+
const shadow = host.attachShadow({ mode: 'open' })
1049+
1050+
setupStyleSheet('host-nonce')
1051+
setupStyleSheet('shadow-nonce', shadow)
1052+
1053+
expect(shadow.querySelector('#_goober')).not.toBeNull()
1054+
expect(shadow.querySelector('#_goober')?.getAttribute('nonce')).toBe(
1055+
'shadow-nonce',
1056+
)
1057+
})
10451058
})
10461059

10471060
describe('sortFns', () => {

packages/query-devtools/src/utils.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -308,18 +308,12 @@ export const deleteNestedDataByPath = (
308308
// Adds a nonce to the style tag if needed
309309
export const setupStyleSheet = (nonce?: string, target?: ShadowRoot) => {
310310
if (!nonce) return
311-
const styleExists =
312-
document.querySelector('#_goober') || target?.querySelector('#_goober')
313-
314-
if (styleExists) return
311+
const root = target ?? document.head
312+
if (root.querySelector('#_goober')) return
315313
const styleTag = document.createElement('style')
316314
const textNode = document.createTextNode('')
317315
styleTag.appendChild(textNode)
318316
styleTag.id = '_goober'
319317
styleTag.setAttribute('nonce', nonce)
320-
if (target) {
321-
target.appendChild(styleTag)
322-
} else {
323-
document.head.appendChild(styleTag)
324-
}
318+
root.appendChild(styleTag)
325319
}

0 commit comments

Comments
 (0)