admin管理员组文章数量:1431037
I am currently using an iframe to load a Next.js application into my JavaScript application using its domain. However, I would like to migrate from using an iframe to rendering the Next.js application within a Shadow DOM. Unfortunately, I am facing issues rendering the Next.js application inside the Shadow DOM.
I’m trying to fetch the domain’s content and load it into the Shadow DOM, like this:
function loadNextJsContent(shadowRoot) {
return new Promise((resolve, reject) => {
// Fetch the HTML content from the Next.js app hosted at
fetch('http://localhost:3333/page-1-uifndi')
.then(response => {
if (!response.ok) {
reject(new Error('Failed to fetch Next.js content'));
} else {
return response.text();
}
})
.then(htmlContent => {
const headContent = htmlContent.match(/<head[^>]*>([\s\S]*?)<\/head>/i)[1];
console.log('HTML Content:', htmlContent, headContent);
const template = document.createElement('template');
template.innerHTML = htmlContent;
shadowRoot.appendChild(template.content.cloneNode(true));
resolve();
})
.catch(error => {
// Handle errors and inject failure message into the Shadow DOM
console.error('Error loading Next.js content:', error);
shadowRoot.innerHTML = '<p>Failed to load content.</p>';
reject(error);
});
});
}
loadNextJsContent(shadowRoot)
.then(() => {
console.log('Next.js content loaded successfully!');
})
.catch(error => {
console.error('Failed to load content:', error);
});
HTML Elements in inspect
I am currently able to retrieve only the HTML content, but I am unable to load the associated CSS, scripts, and fonts. Even though the content is loaded into the Shadow DOM, the styles and scripts do not appear in the network tab or take effect. What I am aiming for is functionality similar to an iframe, which automatically loads all the CSS and scripts when provided with a URL. Since Shadow DOM doesn’t support direct URL rendering, is there a way to load the complete domain content and render it within the Shadow DOM?
本文标签:
版权声明:本文标题:javascript - How to Load and Execute Styles and Scripts from an External HTML into a Web Component (Shadow DOM)? - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745569769a2663976.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论