Skip to content

Commit

Permalink
fix: cache cspNonce
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-tay committed Jul 10, 2023
1 parent 5685732 commit c97e627
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/vite/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ if ('document' in globalThis) {
// all css imports should be inserted at the same position
// because after build it will be a single css file
let lastInsertedStyle: HTMLStyleElement | undefined
let cspNonce: string | undefined | null = undefined

export function updateStyle(id: string, content: string): void {
let style = sheetsMap.get(id)
Expand All @@ -405,9 +406,11 @@ export function updateStyle(id: string, content: string): void {
style.setAttribute('data-vite-dev-id', id)
style.textContent = content

const cspNonce = document.querySelector<HTMLMetaElement>(
'meta[property=csp-nonce]',
)?.content
if (cspNonce === undefined) {
cspNonce =
document.querySelector<HTMLMetaElement>('meta[property=csp-nonce]')
?.content ?? null
}
if (cspNonce) {
style.setAttribute('nonce', cspNonce)
}
Expand Down

0 comments on commit c97e627

Please sign in to comment.