From d0d09fd3a5171406b5e6dcc31fafd3a7c60fccc0 Mon Sep 17 00:00:00 2001 From: Echo Date: Mon, 27 Oct 2025 18:36:01 +0100 Subject: [PATCH] Emoji: Fix Web Worker import (#36603) --- app/javascript/mastodon/features/emoji/index.ts | 7 +++---- app/javascript/mastodon/polyfills/index.ts | 12 +++++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/app/javascript/mastodon/features/emoji/index.ts b/app/javascript/mastodon/features/emoji/index.ts index 3701ad676..11ee26aac 100644 --- a/app/javascript/mastodon/features/emoji/index.ts +++ b/app/javascript/mastodon/features/emoji/index.ts @@ -1,8 +1,9 @@ import { initialState } from '@/mastodon/initial_state'; -import { loadWorker } from '@/mastodon/utils/workers'; import { toSupportedLocale } from './locale'; import { emojiLogger } from './utils'; +// eslint-disable-next-line import/default -- Importing via worker loader. +import EmojiWorker from './worker?worker&inline'; const userLocale = toSupportedLocale(initialState?.meta.locale ?? 'en'); @@ -16,9 +17,7 @@ export function initializeEmoji() { log('initializing emojis'); if (!worker && 'Worker' in window) { try { - worker = loadWorker(new URL('./worker', import.meta.url), { - type: 'module', - }); + worker = new EmojiWorker(); } catch (err) { console.warn('Error creating web worker:', err); } diff --git a/app/javascript/mastodon/polyfills/index.ts b/app/javascript/mastodon/polyfills/index.ts index 1abfe0a93..00da2042e 100644 --- a/app/javascript/mastodon/polyfills/index.ts +++ b/app/javascript/mastodon/polyfills/index.ts @@ -2,9 +2,6 @@ // If there are no polyfills, then this is just Promise.resolve() which means // it will execute in the same tick of the event loop (i.e. near-instant). -// eslint-disable-next-line import/extensions -- This file is virtual so it thinks it has an extension -import 'vite/modulepreload-polyfill'; - import { loadIntlPolyfills } from './intl'; function importExtraPolyfills() { @@ -17,6 +14,7 @@ export function loadPolyfills() { const needsExtraPolyfills = !window.requestIdleCallback; return Promise.all([ + loadVitePreloadPolyfill(), loadIntlPolyfills(), // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- those properties might not exist in old browsers, even if they are always here in types needsExtraPolyfills ? importExtraPolyfills() : Promise.resolve(), @@ -31,5 +29,13 @@ async function loadEmojiPolyfills() { } } +// Loads Vite's module preload polyfill for older browsers, but not in a Worker context. +function loadVitePreloadPolyfill() { + if (typeof document === 'undefined') return; + // @ts-expect-error -- This is a virtual module provided by Vite. + // eslint-disable-next-line import/extensions + return import('vite/modulepreload-polyfill'); +} + // Null unless polyfill is needed. export let emojiRegexPolyfill: RegExp | null = null;