Skip to content

Commit f6bf85a

Browse files
fix : Fix Issue with Document Error in Microfrontend (#147)
* fix : Fix Issue with Document Error in Microfrontend * refactor(renderer): adapt micro-app env * refactor(renderer): adapt micro-app env * refactor: change console.log to error * refactor: catch error of set svg padding * fix: Cannot read properties of undefined (reading infographicFontLoaded) --------- Co-authored-by: alleschen <alleschen@tencent.com> Co-authored-by: Aaron <antvaaron@gmail.com>
1 parent 9f6778b commit f6bf85a

3 files changed

Lines changed: 37 additions & 23 deletions

File tree

src/renderer/fonts/loader.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function trackFontPromise(
6969
}
7070

7171
function isLinkLoaded(link: HTMLLinkElement): boolean {
72-
if (link.dataset.infographicFontLoaded === 'true') return true;
72+
if (link.getAttribute('data-infographic-font-loaded') === 'true') return true;
7373
try {
7474
return !!link.sheet;
7575
} catch {
@@ -91,7 +91,7 @@ function getFontLoadPromise(
9191

9292
const promise = new Promise<void>((resolve) => {
9393
const done = () => {
94-
link.dataset.infographicFontLoaded = 'true';
94+
link.setAttribute('data-infographic-font-loaded', 'true');
9595
resolve();
9696
};
9797
link.addEventListener('load', done, { once: true });

src/renderer/renderer.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,36 @@ export class Renderer implements IRenderer {
6464

6565
renderTemplate(svg, this.options);
6666
svg.style.visibility = 'hidden';
67+
const postRender = () => {
68+
setView(this.template, this.options);
69+
loadFonts(this.template);
70+
svg.style.visibility = '';
71+
};
72+
6773
const observer = new MutationObserver((mutations) => {
6874
mutations.forEach((mutation) => {
6975
mutation.addedNodes.forEach((node) => {
7076
if (node === svg || node.contains(svg)) {
7177
// post render
72-
setView(this.template, this.options);
73-
loadFonts(this.template);
78+
postRender();
7479

7580
// disconnect observer
7681
observer.disconnect();
77-
svg.style.visibility = '';
7882
}
7983
});
8084
});
8185
});
8286

83-
observer.observe(document, {
84-
childList: true,
85-
subtree: true,
86-
});
87+
try {
88+
observer.observe(document, {
89+
childList: true,
90+
subtree: true,
91+
});
92+
} catch (error) {
93+
// Fallback for micro-app environments that proxy document.
94+
postRender();
95+
console.error(error);
96+
}
8797

8898
this.rendered = true;
8999
return svg;

src/utils/padding.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,28 @@ export function setSVGPadding(svg: SVGSVGElement, padding: ParsedPadding) {
3030
if (document.contains(svg)) {
3131
setSVGPaddingInBrowser(svg, padding);
3232
} else {
33-
const observer = new MutationObserver((mutations) => {
34-
mutations.forEach((mutation) => {
35-
mutation.addedNodes.forEach((node) => {
36-
if (node === svg || node.contains(svg)) {
37-
waitForLayout(svg, () => {
38-
setSVGPaddingInBrowser(svg, padding);
39-
});
33+
try {
34+
const observer = new MutationObserver((mutations) => {
35+
mutations.forEach((mutation) => {
36+
mutation.addedNodes.forEach((node) => {
37+
if (node === svg || node.contains(svg)) {
38+
waitForLayout(svg, () => {
39+
setSVGPaddingInBrowser(svg, padding);
40+
});
4041

41-
observer.disconnect();
42-
}
42+
observer.disconnect();
43+
}
44+
});
4345
});
4446
});
45-
});
4647

47-
observer.observe(document, {
48-
childList: true,
49-
subtree: true,
50-
});
48+
observer.observe(document, {
49+
childList: true,
50+
subtree: true,
51+
});
52+
} catch {
53+
setSVGPaddingInNode(svg, padding);
54+
}
5155
}
5256
}
5357
}

0 commit comments

Comments
 (0)