Skip to content

Commit 468a51d

Browse files
committed
chore: missing nonce in non shadow-dom context + doc
1 parent 55ec257 commit 468a51d

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ See the [Documentation](https://erangrin.github.io/vue-web-component-wrapper) fo
4949
- **Async Initialization**: Option to delay the initialization until its Promise resolves.
5050
- **Loader Support**: Support for loader spinner elements until the application is fully initialized.
5151
- **Hide slot content until the component is fully mounted**: Option to hide the content of named slots until the web-component is fully mounted.
52+
5253
## CSS Frameworks Examples
5354

5455
- **Tailwind CSS**: [Demo](https://stackblitz.com/edit/vue-web-component-wrapper?file=README.md&startScript=tailwind-demo)
@@ -167,6 +168,7 @@ createWebComponent({
167168
- **asyncInitialization**: Accepts a function that returns a Promise.
168169
- **loaderAttribute**: Defines the attribute used to mark loader spinner (default is `data-web-component-loader`).
169170
- **hideSlotContentUntilMounted**: Hide the content of named slots until the component is fully mounted.
171+
- **nonce**: Content Security Policy (CSP) nonce for your web component.
170172

171173
### asyncInitialization
172174

@@ -175,7 +177,7 @@ The `asyncInitialization` option accepts a function that returns a Promise. The
175177
#### Example Usage
176178

177179
```javascript
178-
const asyncPromise = () => {
180+
const asyncPromise = () => {
179181
return new Promise((resolve) => {
180182
setTimeout(() => {
181183
resolve()
@@ -192,7 +194,7 @@ createWebComponent({
192194
h,
193195
createApp,
194196
getCurrentInstance,
195-
asyncInitialization: asyncPromise, // default is Promise.resolve()
197+
asyncInitialization: asyncPromise, // default is Promise.resolve()
196198
loaderAttribute: 'data-web-component-loader',
197199
hideSlotContentUntilMounted: true, // default is false
198200
});
@@ -270,6 +272,10 @@ createWebComponent({
270272

271273
The `cssFrameworkStyles` option imports the CSS of your CSS framework or any other global CSS styles your application needs. By setting `replaceRootWithHostInCssFramework` to `true`, any `:root` selectors in your styles will be replaced with `:host`, ensuring correct scoping within the web component.
272274

275+
### nonce
276+
277+
The `nonce` option is used to set a Content Security Policy (CSP) nonce for your web component. This is useful when your application uses inline scripts or styles, as it allows you to specify a unique nonce value that can be used to whitelist the inline content.
278+
273279
### 4. Build Your Application
274280

275281
Tested bundlers to build the web-component application.

package/src/api-custom-element.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ export interface DefineCustomElementConfig {
4646
* @default true
4747
*/
4848
shadowRoot?: boolean
49+
/**
50+
* Nonce to use for CSP
51+
*/
52+
nonce?: string
4953
}
5054

5155
// defineCustomElement provides the same type inference as defineComponent
@@ -186,6 +190,7 @@ export const defineSSRCustomElement = ((
186190
config?: DefineCustomElementConfig
187191
) => {
188192

193+
189194
// @ts-expect-error
190195
return defineCustomElement(options, hydrate)
191196
}) as typeof defineCustomElement
@@ -525,6 +530,7 @@ export class VueElement extends BaseClass {
525530
styles.forEach(css => {
526531
const s = document.createElement('style')
527532
s.textContent = css
533+
if (this._config.nonce) s.setAttribute('nonce', this._config.nonce);
528534
this._root!.prepend(s)
529535
if (__DEV__) {
530536
;(this._styles || (this._styles = [])).push(s)

package/src/web-component-util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export const defineCustomElement = ({
171171
}
172172
);
173173
},
174-
}, disableShadowDOM && { shadowRoot: false })
174+
}, { shadowRoot: !disableShadowDOM, nonce })
175175

176176
return asyncInitialization().then(() => {
177177
return customElementConfig;

0 commit comments

Comments
 (0)