Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/devtools-setup-stylesheet-cross-target-dedup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/query-devtools': patch
---

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
13 changes: 13 additions & 0 deletions packages/query-devtools/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,19 @@ describe('Utils tests', () => {
expect(styleTags).toHaveLength(1)
expect(styleTags[0]?.getAttribute('nonce')).toBe('first-nonce')
})

it('should install the style tag into the "ShadowRoot" target even when "document.head" already has one', () => {
const host = document.createElement('div')
const shadow = host.attachShadow({ mode: 'open' })

setupStyleSheet('host-nonce')
setupStyleSheet('shadow-nonce', shadow)

expect(shadow.querySelector('#_goober')).not.toBeNull()
expect(shadow.querySelector('#_goober')?.getAttribute('nonce')).toBe(
'shadow-nonce',
)
})
})

describe('sortFns', () => {
Expand Down
12 changes: 3 additions & 9 deletions packages/query-devtools/src/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -306,18 +306,12 @@ export const deleteNestedDataByPath = (
// Adds a nonce to the style tag if needed
export const setupStyleSheet = (nonce?: string, target?: ShadowRoot) => {
if (!nonce) return
const styleExists =
document.querySelector('#_goober') || target?.querySelector('#_goober')

if (styleExists) return
const root = target ?? document.head
if (root.querySelector('#_goober')) return
const styleTag = document.createElement('style')
const textNode = document.createTextNode('')
styleTag.appendChild(textNode)
styleTag.id = '_goober'
styleTag.setAttribute('nonce', nonce)
if (target) {
target.appendChild(styleTag)
} else {
document.head.appendChild(styleTag)
}
root.appendChild(styleTag)
}
Loading