Urgent.News

What's breaking now, across hundreds of outlets.

Tech

How to Embed a Widget Without Breaking the Host Page (CSS Conflicts, Script Errors and CSP)

An embedded component can hurt its host in three ways: its styles collide with the page's, its errors spread, or the page's Content-Security-Policy refuses it. Here is the defence for each one, and a test you can run in ten minutes. When you put a widget on a page you do not own, three things can go wrong. The page's CSS and yours collide. An error in your code leaves a hole in somebody else's…

Embedding a widget into a host page can cause three problems: style collisions, script errors, and content security policy (CSP) blocks. Each issue has a known solution: Shadow DOM for styles, error boundaries with fallback content for errors, and a script without inline content for CSP.

Shadow DOM isolates the widget's styles from the host page, preventing conflicts. If errors occur in the embedded script, catch them at the edge of the widget and provide a fallback content. The fallback should be the HTML code inside the widget tag, which needs no additional resources from the widget.

Content Security Policy blocks inline scripts, styles, and `eval()`. To comply with a strict policy, ship one external script allowed by the page origin and nonce. Apply styles through adoptedStyleSheets instead of a style tag, and always build the DOM with nodes, not HTML strings.

In the provided test, a hostile page with forced borders, padding, and button colors proves the effectiveness of these defenses. The widget remains styled, error-free, and functional, demonstrating the importance of these precautions when embedding components on external pages.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

More from Dev.to on urgent.news

Report a problem with this story

Get the day's news by email. Daily, weekly or monthly. Sign up

More in Tech

More from Thursday, 3 December 2026 →