mirror of
https://gitlab.com/PronounsPage/PronounsPage.git
synced 2025-09-24 05:05:20 -04:00
fix $loadScript/$loadStylesheet race condition
This commit is contained in:
parent
ace8d26be0
commit
2a5399d5a3
@ -80,13 +80,38 @@ const plugin: Plugin = ({ app, store }, inject) => {
|
||||
store.commit('showTranslationMode');
|
||||
}
|
||||
|
||||
const awaitLoadedClass = (node: Element): Promise<void> => {
|
||||
return new Promise((resolve) => {
|
||||
if (node.classList.contains('loaded')) {
|
||||
resolve();
|
||||
} else {
|
||||
const observer = new MutationObserver((mutations) => {
|
||||
mutations.forEach((mutation) => {
|
||||
const target = mutation.target as Element;
|
||||
if (target.classList.contains('loaded')) {
|
||||
observer.disconnect();
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
observer.observe(node, { attributes: true, attributeFilter: ['class'] });
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
inject('loadScript', (name: string, src: string, nonce: string | undefined = undefined): Promise<void> => {
|
||||
if (!process.client || document.querySelectorAll(`script.${name}-script`).length > 0) {
|
||||
if (!process.client) {
|
||||
return new Promise((resolve) => {
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
|
||||
const existingScriptNode = document.querySelector(`script.${name}-script`);
|
||||
if (existingScriptNode) {
|
||||
return awaitLoadedClass(existingScriptNode);
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const script = document.createElement('script');
|
||||
script.setAttribute('src', src);
|
||||
@ -107,12 +132,17 @@ const plugin: Plugin = ({ app, store }, inject) => {
|
||||
});
|
||||
|
||||
inject('loadStylesheet', (name: string, src: string): Promise<void> => {
|
||||
if (!process.client || document.querySelectorAll(`link.${name}-stylesheet`).length > 0) {
|
||||
if (!process.client) {
|
||||
return new Promise((resolve) => {
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
|
||||
const existingLinkNode = document.querySelector(`link.${name}-stylesheet`);
|
||||
if (existingLinkNode) {
|
||||
return awaitLoadedClass(existingLinkNode);
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const link = document.createElement('link');
|
||||
link.setAttribute('rel', 'stylesheet');
|
||||
|
Loading…
x
Reference in New Issue
Block a user