Home / Function/ injectNonceAttributeTagHook() — vite Function Reference

injectNonceAttributeTagHook() — vite Function Reference

Architecture documentation for the injectNonceAttributeTagHook() function in html.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  7514180d_f3bd_4f07_5c4e_5911c2a70a4c["injectNonceAttributeTagHook()"]
  f8fe0737_718a_5509_b722_473f207d5906["html.ts"]
  7514180d_f3bd_4f07_5c4e_5911c2a70a4c -->|defined in| f8fe0737_718a_5509_b722_473f207d5906
  39f26be8_b1d9_a756_3043_474687a6bbb7["buildHtmlPlugin()"]
  39f26be8_b1d9_a756_3043_474687a6bbb7 -->|calls| 7514180d_f3bd_4f07_5c4e_5911c2a70a4c
  7a53712a_b13b_4bc2_85fc_b4de5cb705fb["createDevHtmlTransformFn()"]
  7a53712a_b13b_4bc2_85fc_b4de5cb705fb -->|calls| 7514180d_f3bd_4f07_5c4e_5911c2a70a4c
  5800888e_ad6f_73a4_812a_c831c6a9eebf["traverseHtml()"]
  7514180d_f3bd_4f07_5c4e_5911c2a70a4c -->|calls| 5800888e_ad6f_73a4_812a_c831c6a9eebf
  0a5ccc35_5940_e97a_b8cf_fa02cbb6f117["nodeIsElement()"]
  7514180d_f3bd_4f07_5c4e_5911c2a70a4c -->|calls| 0a5ccc35_5940_e97a_b8cf_fa02cbb6f117
  464380b5_e66a_0c5c_7d46_95469500bbf9["parseRelAttr()"]
  7514180d_f3bd_4f07_5c4e_5911c2a70a4c -->|calls| 464380b5_e66a_0c5c_7d46_95469500bbf9
  style 7514180d_f3bd_4f07_5c4e_5911c2a70a4c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/plugins/html.ts lines 1255–1300

export function injectNonceAttributeTagHook(
  config: ResolvedConfig,
): IndexHtmlTransformHook {
  const processRelType = new Set(['stylesheet', 'modulepreload', 'preload'])

  return async (html, { filename }) => {
    const nonce = config.html?.cspNonce
    if (!nonce) return

    const s = new MagicString(html)

    await traverseHtml(html, filename, config.logger.warn, (node) => {
      if (!nodeIsElement(node)) {
        return
      }

      const { nodeName, attrs, sourceCodeLocation } = node

      if (
        nodeName === 'script' ||
        nodeName === 'style' ||
        (nodeName === 'link' &&
          attrs.some(
            (attr) =>
              attr.name === 'rel' &&
              parseRelAttr(attr.value).some((a) => processRelType.has(a)),
          ))
      ) {
        // If we already have a nonce attribute, we don't need to add another one
        if (attrs.some(({ name }) => name === 'nonce')) {
          return
        }

        const startTagEndOffset = sourceCodeLocation!.startTag!.endOffset

        // if the closing of the start tag includes a `/`, the offset should be 2 so the nonce
        // is appended prior to the `/`
        const appendOffset = html[startTagEndOffset - 2] === '/' ? 2 : 1

        s.appendRight(startTagEndOffset - appendOffset, ` nonce="${nonce}"`)
      }
    })

    return s.toString()
  }
}

Domain

Subdomains

Frequently Asked Questions

What does injectNonceAttributeTagHook() do?
injectNonceAttributeTagHook() is a function in the vite codebase, defined in packages/vite/src/node/plugins/html.ts.
Where is injectNonceAttributeTagHook() defined?
injectNonceAttributeTagHook() is defined in packages/vite/src/node/plugins/html.ts at line 1255.
What does injectNonceAttributeTagHook() call?
injectNonceAttributeTagHook() calls 3 function(s): nodeIsElement, parseRelAttr, traverseHtml.
What calls injectNonceAttributeTagHook()?
injectNonceAttributeTagHook() is called by 2 function(s): buildHtmlPlugin, createDevHtmlTransformFn.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free