Replace the content of an element using HTMLRewriter rendering HTML

I’m trying to replace some content on an existing page, but it’s rendering in plain text not HTML, any tips, here my code:

const newContent = `
  <div id="main" class="site-main">
        <h2 class="page-title">New content</h2>
   </div>
`;

class newContentRewriter
  {
  element(element)
  {
  element.replace(newContent), { html: true };

  }
  }

const rewriter = new HTMLRewriter()
.on('div#main', new newContentRewriter())

async function handleRequest(req)
{
const res = await fetch(req);
return rewriter.transform(res);
}

addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
})