DOMException:无法执行“importScripts”在“WorkerGlobalScope”上:脚本加载失败

发布于 2025-01-09 17:54:36 字数 2383 浏览 0 评论 0原文

我正在尝试使用网络工作者来计算一些需要一些时间的数据。它位于尚未弹出的 React 应用程序中。我粘贴了一堆代码,以防它有助于解决问题,但我遇到的主要问题是我无法将模块导入到工作程序中。下面是我的工作人员及其调用方式。

CalendarEvents.worker.js

importScripts('ImportWorkerModules.js')

onmessage = ({ data: { alarms, startDate, endDate, calendarView } }) => {
  const getEventsForAlarmsInRange = self.AlarmUtils.getEventsForAlarmsInRange
  const events = getEventsForAlarmsInRange(
    alarms,
    startDate,
    endDate,
    calendarView
  )
  postMessage({
    events
  })
}

ImportWorkerModules

// my-app-shared is my own package contained in a monorepo that contains shared business logic between the mobile app and the web app.
import { AlarmUtils } from 'my-app-shared'

WebWorkerUtils.js

import CalendarEventsWorker from 'worker-loader!./CalendarEvents.worker.js'

const WebUtils = (function () {
  const getEventsForCalendar = (alarms, startDate, endDate, calendarView) => {
    const promise = new Promise(resolve => {
      const worker = new CalendarEventsWorker()
      worker.postMessage({
        alarms,
        startDate,
        endDate,
        calendarView
      })
      worker.onmessage = ({ data: { events } }) => {
        resolve(events)
      }
    })
    return promise
  }

  return {
    getEventsForCalendar
  }
})()

export default WebUtils

这是它的调用方式:

  useEffect(() => {
    WebWorkerUtils.getEventsForCalendar(
      alarmsToConsider,
      dateRange.start,
      dateRange.end,
      calendarView
    ).then(events => {
      events.forEach(event => {
        const alarmCategory = AlarmUtils.getAlarmCategoryForAlarm(
          alarmCategories,
          event.id
        )
        event.category = alarmCategory
      })
      setEvents(events)
    })
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [
    dateRange,
    allAlarms,
    alarmCategories,
    categorizedAlarmView,
    currentlySelectedAlarmCategoryId
  ])

我在 Chrome 开发人员工具上看到以下错误: DOMException: 无法在 'WorkerGlobalScope' 上执行 'importScripts': The script at 'http://localhost:3000 /static/js/ImportWorkerModules.js' 加载失败。

Chrome 开发人员工具错误

如何修复此错误?我尝试在网上寻找此错误的可能解决方案,但没有找到任何具体的解决方案。

I am trying to use a web worker to compute some data that takes some time. It is in a React app that has not been ejected. I have pasted a bunch of code in case it helps in figuring the problem but the main problem I am having is that I am not able to import modules into the worker. Below is my worker and how it is invoked.

CalendarEvents.worker.js

importScripts('ImportWorkerModules.js')

onmessage = ({ data: { alarms, startDate, endDate, calendarView } }) => {
  const getEventsForAlarmsInRange = self.AlarmUtils.getEventsForAlarmsInRange
  const events = getEventsForAlarmsInRange(
    alarms,
    startDate,
    endDate,
    calendarView
  )
  postMessage({
    events
  })
}

ImportWorkerModules

// my-app-shared is my own package contained in a monorepo that contains shared business logic between the mobile app and the web app.
import { AlarmUtils } from 'my-app-shared'

WebWorkerUtils.js

import CalendarEventsWorker from 'worker-loader!./CalendarEvents.worker.js'

const WebUtils = (function () {
  const getEventsForCalendar = (alarms, startDate, endDate, calendarView) => {
    const promise = new Promise(resolve => {
      const worker = new CalendarEventsWorker()
      worker.postMessage({
        alarms,
        startDate,
        endDate,
        calendarView
      })
      worker.onmessage = ({ data: { events } }) => {
        resolve(events)
      }
    })
    return promise
  }

  return {
    getEventsForCalendar
  }
})()

export default WebUtils

This is how it is invoked:

  useEffect(() => {
    WebWorkerUtils.getEventsForCalendar(
      alarmsToConsider,
      dateRange.start,
      dateRange.end,
      calendarView
    ).then(events => {
      events.forEach(event => {
        const alarmCategory = AlarmUtils.getAlarmCategoryForAlarm(
          alarmCategories,
          event.id
        )
        event.category = alarmCategory
      })
      setEvents(events)
    })
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [
    dateRange,
    allAlarms,
    alarmCategories,
    categorizedAlarmView,
    currentlySelectedAlarmCategoryId
  ])

I see the following error on Chrome Developer Tools: DOMException: Failed to execute 'importScripts' on 'WorkerGlobalScope': The script at 'http://localhost:3000/static/js/ImportWorkerModules.js' failed to load.

Chrome Developer Tools Error

How can I fix this error? I have tried searching for possible solutions for this error on the web but haven't found anything concrete.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文