@xuda.io/xuda-worker-bundle 1.3.2694 → 1.3.2695

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +75 -6
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -291,7 +291,30 @@ func.runtime.platform.emit = function (name, data) {
291
291
  };
292
292
 
293
293
  // ── Platform helpers for DOM-independent resource loading ──
294
- func.runtime.platform.load_script = function (url, type, callback) {
294
+ func.runtime.platform.apply_element_attributes = function (node, attributes, excluded_keys = []) {
295
+ if (!node?.setAttribute || !attributes) {
296
+ return node;
297
+ }
298
+
299
+ const excluded = new Set(excluded_keys || []);
300
+ const attr_keys = Object.keys(attributes);
301
+ for (let index = 0; index < attr_keys.length; index++) {
302
+ const key = attr_keys[index];
303
+ if (!key || excluded.has(key)) {
304
+ continue;
305
+ }
306
+
307
+ const value = attributes[key];
308
+ if (value === false || typeof value === 'undefined') {
309
+ continue;
310
+ }
311
+
312
+ node.setAttribute(key, value === null ? '' : `${value}`);
313
+ }
314
+
315
+ return node;
316
+ };
317
+ func.runtime.platform.load_script = function (url, type, callback, attributes) {
295
318
  const doc = func.runtime.platform.get_document();
296
319
  if (!doc?.createElement || !doc?.head?.appendChild) {
297
320
  if (callback) {
@@ -299,19 +322,63 @@ func.runtime.platform.load_script = function (url, type, callback) {
299
322
  }
300
323
  return;
301
324
  }
325
+ const find_existing_script = function () {
326
+ const asset_key = attributes?.['data-xuda-asset-key'];
327
+ const scripts = doc.querySelectorAll ? Array.from(doc.querySelectorAll('script')) : [];
328
+ return scripts.find(function (script) {
329
+ if (asset_key && script.getAttribute('data-xuda-asset-key') === asset_key) {
330
+ return true;
331
+ }
332
+ return !!(url && script.getAttribute('src') === url);
333
+ }) || null;
334
+ };
335
+
336
+ const existing_script = find_existing_script();
337
+ if (existing_script) {
338
+ if (callback) {
339
+ if (existing_script.getAttribute('data-xuda-loaded') === 'true' || !url) {
340
+ callback();
341
+ } else {
342
+ existing_script.addEventListener('load', callback, { once: true });
343
+ existing_script.addEventListener('error', callback, { once: true });
344
+ }
345
+ }
346
+ return existing_script;
347
+ }
348
+
302
349
  const script = doc.createElement('script');
303
350
  script.src = url;
304
351
  if (type) script.type = type;
305
- script.onload = callback;
352
+ func.runtime.platform.apply_element_attributes(script, attributes, ['src', 'type']);
353
+ script.onload = function () {
354
+ script.setAttribute('data-xuda-loaded', 'true');
355
+ if (callback) {
356
+ callback();
357
+ }
358
+ };
359
+ script.onerror = function () {
360
+ if (callback) {
361
+ callback();
362
+ }
363
+ };
306
364
  doc.head.appendChild(script);
365
+ return script;
307
366
  };
308
- func.runtime.platform.load_css = function (href) {
367
+ func.runtime.platform.load_css = function (href, attributes) {
309
368
  const doc = func.runtime.platform.get_document();
310
369
  if (!doc?.createElement || !doc?.head) {
311
370
  return;
312
371
  }
313
372
  try {
314
- if (doc.querySelector('link[href="' + href + '"]')) return;
373
+ const asset_key = attributes?.['data-xuda-asset-key'];
374
+ const existing_links = doc.querySelectorAll ? Array.from(doc.querySelectorAll('link')) : [];
375
+ const existing = existing_links.find(function (link) {
376
+ if (asset_key && link.getAttribute('data-xuda-asset-key') === asset_key) {
377
+ return true;
378
+ }
379
+ return !!(href && link.getAttribute('href') === href);
380
+ });
381
+ if (existing) return existing;
315
382
  } catch (err) {
316
383
  return;
317
384
  }
@@ -319,7 +386,9 @@ func.runtime.platform.load_css = function (href) {
319
386
  link.rel = 'stylesheet';
320
387
  link.type = 'text/css';
321
388
  link.href = href;
389
+ func.runtime.platform.apply_element_attributes(link, attributes, ['href']);
322
390
  doc.head.insertBefore(link, doc.head.firstChild);
391
+ return link;
323
392
  };
324
393
  func.runtime.platform.remove_js_css = function (filename, filetype) {
325
394
  const doc = func.runtime.platform.get_document();
@@ -1154,10 +1223,10 @@ func.runtime.resources.load_cdn = async function (SESSION_ID, resource) {
1154
1223
  await func.utils.load_js_on_demand(normalized_resource.src);
1155
1224
  break;
1156
1225
  case 'css':
1157
- await func.utils.load_js_on_demand(normalized_resource.src);
1226
+ func.runtime.platform.load_css(normalized_resource.src);
1158
1227
  break;
1159
1228
  case 'module':
1160
- func.utils.load_js_on_demand(normalized_resource.src, 'module');
1229
+ await func.utils.load_js_on_demand(normalized_resource.src, 'module');
1161
1230
  break;
1162
1231
  default:
1163
1232
  await func.utils.load_js_on_demand(normalized_resource.src);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/xuda-worker-bundle",
3
- "version": "1.3.2694",
3
+ "version": "1.3.2695",
4
4
  "description": "xuda framework",
5
5
  "main": "index.js",
6
6
  "scripts": {