mirror of https://github.com/grafana/grafana
Frontend o11y: Load SharedWorkers for crash detection (#96673)
* Load SharedWorkers from blob
* Fix typo
* Update docs
* Add more docs
* Simplify extending CorsSharedWorker
* Revert "Simplify extending CorsSharedWorker"
This reverts commit 1603e5f02f
.
* Simplify extending CorsSharedWorker
* Remove ts-ignore
* Update betterer and add docs
* Update public/app/core/crash/index.ts
Co-authored-by: Sven Grossmann <sven.grossmann@grafana.com>
* Update public/app/core/utils/CorsSharedWorker.ts
Co-authored-by: Sven Grossmann <sven.grossmann@grafana.com>
* Update public/app/core/crash/index.ts
Co-authored-by: Sven Grossmann <sven.grossmann@grafana.com>
* Update public/app/core/utils/CorsSharedWorker.ts
Co-authored-by: Sven Grossmann <sven.grossmann@grafana.com>
* Simplify getting scriptsBasePathUrl
* Disable linting for SharedWorker type assertion
---------
Co-authored-by: Sven Grossmann <sven.grossmann@grafana.com>
pull/97096/head^2
parent
5f1fae8efd
commit
60f9ff0a07
@ -0,0 +1,33 @@ |
||||
// Almost identical to CorsWorker.ts. Main difference being it allows loading a SharedWorker if browser supports it
|
||||
|
||||
export function sharedWorkersSupported() { |
||||
return typeof window.SharedWorker !== 'undefined'; |
||||
} |
||||
|
||||
/** |
||||
* Creating CorsSharedWorker should be called only if sharedWorkersSupported() is truthy |
||||
*/ |
||||
export class CorsSharedWorker { |
||||
constructor(url: URL, options?: WorkerOptions) { |
||||
if (!sharedWorkersSupported()) { |
||||
throw new Error('SharedWorker is not supported'); |
||||
} |
||||
// by default, worker inherits HTML document's location and pathname which leads to wrong public path value
|
||||
// the CorsWorkerPlugin will override it with the value based on the initial worker chunk, ie.
|
||||
// initial worker chunk: http://host.com/cdn/scripts/worker-123.js
|
||||
// resulting public path: http://host.com/cdn/scripts
|
||||
|
||||
const scriptUrl = url.toString(); |
||||
const scriptsBasePathUrl = new URL('.', url).toString(); |
||||
|
||||
const importScripts = `importScripts('${scriptUrl}');`; |
||||
const objectURL = URL.createObjectURL( |
||||
new Blob([`__webpack_worker_public_path__ = '${scriptsBasePathUrl}'; ${importScripts}`], { |
||||
type: 'application/javascript', |
||||
}) |
||||
); |
||||
const worker = new SharedWorker(objectURL, options); |
||||
URL.revokeObjectURL(objectURL); |
||||
return worker; |
||||
} |
||||
} |
Loading…
Reference in new issue