DOMException:无法执行“importScripts”在“WorkerGlobalScope”上:脚本加载失败
我正在尝试使用网络工作者来计算一些需要一些时间的数据。它位于尚未弹出的 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' 加载失败。
如何修复此错误?我尝试在网上寻找此错误的可能解决方案,但没有找到任何具体的解决方案。
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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论