|
1 | | -export default () => { |
| 1 | +import { fireError } from 'helpers/fireEvent'; |
| 2 | +import { getDocName, getDocumentExtension, isOfficeExtension, isPDFExtension } from 'helpers/loadDocument'; |
| 3 | + |
| 4 | +export default store => { |
2 | 5 | window.addEventListener('error', e => { |
3 | 6 | const { src } = e.target; |
4 | | - |
| 7 | + |
5 | 8 | if (!src) { |
6 | 9 | // in Safari, an error occurred in web workers will also trigger this event |
7 | 10 | // since we handle worker error in loaderror event, we just return here |
8 | 11 | return; |
9 | 12 | } |
10 | 13 |
|
| 14 | + const docExtension = getDocumentExtension(getDocName(store.getState())); |
11 | 15 | if (src.endsWith('nmf')) { |
12 | | - testMIMEType(['nmf']); |
| 16 | + testMIMEType(['nmf']) |
| 17 | + .then(() => { |
| 18 | + // when the server has correct setup for MIME type but SimpleWorker.nmf is missing |
| 19 | + // we don't want to fire error since in this case we will fallback to use other worker files and can still load the document successfully |
| 20 | + if (src.indexOf('SimpleWorker.nmf') === -1) { |
| 21 | + errorMissingWorkerFiles(docExtension); |
| 22 | + } |
| 23 | + }) |
| 24 | + .catch(errorMIMEType); |
13 | 25 | } |
14 | 26 | if (src.endsWith('pexe')) { |
15 | | - testMIMEType(['pexe']); |
| 27 | + testMIMEType(['pexe']) |
| 28 | + .then(() => { |
| 29 | + if (src.indexOf('SimpleWorker.pexe') === -1) { |
| 30 | + errorMissingWorkerFiles(docExtension); |
| 31 | + } |
| 32 | + }) |
| 33 | + .catch(errorMIMEType); |
16 | 34 | } |
17 | 35 | }, true); |
18 | 36 |
|
19 | | - window.addEventListener('loaderror', e => { |
20 | | - if (missFilesToLoad(e)) { |
21 | | - testMIMEType(['res', 'mem', 'wasm', 'xod']); |
| 37 | + window.addEventListener('loaderror', ({ detail }) => { |
| 38 | + const docExtension = getDocumentExtension(getDocName(store.getState())); |
| 39 | + |
| 40 | + if (detail.startsWith('Error retrieving file:') && detail.includes('.xod')) { |
| 41 | + testMIMEType(['xod']) |
| 42 | + .then(() => errorMissingWorkerFiles(docExtension)) |
| 43 | + .catch(errorMIMEType); |
| 44 | + } |
| 45 | + if (detail === 'The worker has encountered an error') { |
| 46 | + testMIMEType(['mem', 'wasm']) |
| 47 | + .then(() => errorMissingWorkerFiles(docExtension)) |
| 48 | + .catch(errorMIMEType); |
| 49 | + } |
| 50 | + if (detail === `Couldn't fetch resource file.`) { |
| 51 | + testMIMEType(['res']) |
| 52 | + .then(() => errorMissingWorkerFiles(docExtension)) |
| 53 | + .catch(errorMIMEType); |
22 | 54 | } |
23 | 55 | }); |
24 | 56 | }; |
25 | 57 |
|
26 | 58 | const testMIMEType = fileExtensions => { |
27 | | - fileExtensions.forEach(extension => { |
28 | | - fetch(`${window.CoreControls.getWorkerPath()}/assets/mime-types/test.${extension}`) |
29 | | - .then(({ status }) => { |
| 59 | + const fetchingTestFiles = fileExtensions.map(extension => { |
| 60 | + return new Promise((resolve, reject) => { |
| 61 | + const URL = `${window.CoreControls.getWorkerPath()}/assets/mime-types/test.${extension}`; |
| 62 | + |
| 63 | + fetch(URL).then(({ status }) => { |
30 | 64 | if (status === 404) { |
31 | | - console.error(`Your server does not have a MIME type set for extension ${extension}. Please see https://www.pdftron.com/documentation/web/guides/basics/troubleshooting-document-loading/#mime-types for more information.`); |
| 65 | + reject(extension); |
| 66 | + } else { |
| 67 | + resolve(); |
32 | 68 | } |
33 | 69 | }); |
| 70 | + }); |
34 | 71 | }); |
| 72 | + |
| 73 | + return Promise.all(fetchingTestFiles); |
| 74 | +}; |
| 75 | + |
| 76 | +const errorMissingWorkerFiles = docExtension => { |
| 77 | + let errorMessage; |
| 78 | + |
| 79 | + if (isOfficeExtension(docExtension)) { |
| 80 | + errorMessage = 'Failed to find Office worker files. This project is not set up to work with Office files.'; |
| 81 | + } |
| 82 | + if (isPDFExtension(docExtension)) { |
| 83 | + errorMessage = 'Failed to find PDF worker files. This project is not set up to work with PDF files.'; |
| 84 | + } |
| 85 | + |
| 86 | + fireError(errorMessage); |
| 87 | + console.error(errorMessage); |
35 | 88 | }; |
36 | 89 |
|
37 | | -const missFilesToLoad = ({ detail }) => { |
38 | | - return detail === `Couldn't fetch resource file.` |
39 | | - || detail === 'The worker has encountered an error' |
40 | | - || (detail.startsWith('Error retrieving file:') && detail.includes('.xod')); |
| 90 | +const errorMIMEType = fileExtension => { |
| 91 | + fireError(`Your server does not have a MIME type set for extension ${fileExtension}. Open developer console to see the link for correct MIME type setup`); |
| 92 | + console.error(`Your server does not have a MIME type set for extension ${fileExtension}. Please see https://www.pdftron.com/documentation/web/guides/basics/troubleshooting-document-loading/#mime-types for more information.`); |
41 | 93 | }; |
0 commit comments