I'm receiving raw html snippets from a headless CMS that I need to render as-is in a Sapper application.
This also includes cases where I receive an opening tag and the corresponding closing tag as two pieces of HTML between which I add a Svelte component.
<script>
import MyComponent from './MyComponent.svelte'
// example snippets coming from headless CMS
const prefix = '<p class="test">'
const suffix = '</p>'
</script>
<!-- use snippets as-is -->
{@html prefix}
<MyComponent />
{@html suffix}
See https://svelte.dev/repl/4c1bf80ae00e476587344b6065e7346e?version=3.19.1
However, Svelte expects each item rendered with @html to be self contained element and tries to "fix" common errors. For example, it will add a closing tag to the prefix-snippet, moving the <MyComponent/>
-part out of the p
.
Is there a way to circumvent this behavior in general? Or more specifically - is it possible to surround rendered components with arbitrary raw HTML?
As a side node: this runs as a sapper app and the server side rendered version of the page does correctly emit the raw HTML. It's when the client side rendering kicks in that the behavior changes.