Skip to content

Commit 0e6a46e

Browse files
committed
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
1 parent 3daac67 commit 0e6a46e

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
@@ -306,18 +306,12 @@ export const deleteNestedDataByPath = (
306306
// Adds a nonce to the style tag if needed
307307
export const setupStyleSheet = (nonce?: string, target?: ShadowRoot) => {
308308
if (!nonce) return
309-
const styleExists =
310-
document.querySelector('#_goober') || target?.querySelector('#_goober')
311-
312-
if (styleExists) return
309+
const root = target ?? document.head
310+
if (root.querySelector('#_goober')) return
313311
const styleTag = document.createElement('style')
314312
const textNode = document.createTextNode('')
315313
styleTag.appendChild(textNode)
316314
styleTag.id = '_goober'
317315
styleTag.setAttribute('nonce', nonce)
318-
if (target) {
319-
target.appendChild(styleTag)
320-
} else {
321-
document.head.appendChild(styleTag)
322-
}
316+
root.appendChild(styleTag)
323317
}

0 commit comments

Comments
 (0)