From ec412e17484620c5fd23f2e3cfd6b1e6b96d5d7f Mon Sep 17 00:00:00 2001 From: Echo Date: Thu, 24 Jul 2025 09:14:27 +0200 Subject: [PATCH] [Glitch] Enables cross-origin Web Workers Port 7f9ad7eabf9d86731ce4af33a8d4a8d35ca3e077 to glitch-soc Signed-off-by: Claire --- .../flavours/glitch/features/emoji/index.ts | 4 +-- .../flavours/glitch/features/emoji/loader.ts | 9 +++--- .../flavours/glitch/utils/workers.ts | 29 +++++++++++++++++++ 3 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 app/javascript/flavours/glitch/utils/workers.ts diff --git a/app/javascript/flavours/glitch/features/emoji/index.ts b/app/javascript/flavours/glitch/features/emoji/index.ts index 27e88f74b3..a55e7ab812 100644 --- a/app/javascript/flavours/glitch/features/emoji/index.ts +++ b/app/javascript/flavours/glitch/features/emoji/index.ts @@ -1,4 +1,5 @@ import initialState from '@/flavours/glitch/initial_state'; +import { loadWorker } from '@/flavours/glitch/utils/workers'; import { toSupportedLocale } from './locale'; @@ -9,9 +10,8 @@ let worker: Worker | null = null; export async function initializeEmoji() { if (!worker && 'Worker' in window) { try { - worker = new Worker(new URL('./worker', import.meta.url), { + worker = loadWorker(new URL('./worker', import.meta.url), { type: 'module', - credentials: 'omit', }); } catch (err) { console.warn('Error creating web worker:', err); diff --git a/app/javascript/flavours/glitch/features/emoji/loader.ts b/app/javascript/flavours/glitch/features/emoji/loader.ts index ff77f721f6..747d9be529 100644 --- a/app/javascript/flavours/glitch/features/emoji/loader.ts +++ b/app/javascript/flavours/glitch/features/emoji/loader.ts @@ -36,15 +36,16 @@ async function fetchAndCheckEtag( ): Promise { const locale = toSupportedLocaleOrCustom(localeOrCustom); - let uri: string; + // Use location.origin as this script may be loaded from a CDN domain. + const url = new URL(location.origin); if (locale === 'custom') { - uri = '/api/v1/custom_emojis'; + url.pathname = '/api/v1/custom_emojis'; } else { - uri = `/packs${isDevelopment() ? '-dev' : ''}/emoji/${locale}.json`; + url.pathname = `/packs${isDevelopment() ? '-dev' : ''}/emoji/${locale}.json`; } const oldEtag = await loadLatestEtag(locale); - const response = await fetch(uri, { + const response = await fetch(url, { headers: { 'Content-Type': 'application/json', 'If-None-Match': oldEtag ?? '', // Send the old ETag to check for modifications diff --git a/app/javascript/flavours/glitch/utils/workers.ts b/app/javascript/flavours/glitch/utils/workers.ts new file mode 100644 index 0000000000..02dd66d86e --- /dev/null +++ b/app/javascript/flavours/glitch/utils/workers.ts @@ -0,0 +1,29 @@ +/** + * Loads Web Worker that is compatible with cross-origin scripts for CDNs. + * + * Returns null if the environment doesn't support web workers. + */ +export function loadWorker(url: string | URL, options: WorkerOptions = {}) { + if (!('Worker' in window)) { + return null; + } + + try { + // Check if the script origin and the window origin are the same. + const scriptUrl = new URL(import.meta.url); + if (location.origin === scriptUrl.origin) { + // Not cross-origin, can just load normally. + return new Worker(url, options); + } + } catch (err) { + // In case the URL parsing fails. + console.warn('Error instantiating Worker:', err); + } + + // Import the worker script from a same-origin Blob. + const contents = `import ${JSON.stringify(url)};`; + const blob = URL.createObjectURL( + new Blob([contents], { type: 'text/javascript' }), + ); + return new Worker(blob, options); +}