@@ -204,8 +204,12 @@ export default {
204204 GTAG_PAYMENT ,
205205 GTAG_PDF ,
206206 progress: 0 ,
207+ pdfWorker: null ,
207208 };
208209 },
210+ beforeUnmount () {
211+ this .closePdfWorker ();
212+ },
209213 methods: {
210214 handleFreePdfClick () {
211215 this .downloadSample ();
@@ -225,9 +229,12 @@ export default {
225229 },
226230 onError () {},
227231 async download (isSample = false ) {
228- if (process .browser ) {
229- this .isLoading = true ;
230- this .progress = 0 ;
232+ if (! import .meta.client) return;
233+
234+ this .isLoading = true ;
235+ this .progress = 0 ;
236+
237+ try {
231238 // the graphs need to be converted to an image beforehand, as the web worker has no access to document
232239 const chatTimeline = await loadImage (" #chat-timeline" );
233240 const messagesPerTimeOfDay = await loadImage (
@@ -237,16 +244,17 @@ export default {
237244 const radarMonth = await loadImage (" #radar-month" );
238245 const radarDay = await loadImage (" #radar-day" );
239246
240- const worker = new PDFWorker ();
241- worker .addEventListener (" message" , this .workerResponseHandler );
247+ this .pdfWorker = new PDFWorker ();
248+ this .pdfWorker .addEventListener (" message" , this .workerResponseHandler );
249+ this .pdfWorker .addEventListener (" error" , this .pdfErrorHandler );
242250
243251 const chat = objectToDictionary (this .chat ); // remove functions
244252 chat .funFacts = await this .chat .getFunFacts (); // set funfacts beforehand instead of using function call
245253
246- worker .postMessage ({
254+ this . pdfWorker .postMessage ({
247255 // pass all data to service worker
248256 chat: chat,
249- attachments: this .attachments ,
257+ attachments: objectToDictionary ( this .attachments ) ,
250258 ego: this .ego ,
251259 isSample,
252260 chatTimeline,
@@ -255,6 +263,8 @@ export default {
255263 radarMonth,
256264 radarDay,
257265 });
266+ } catch (error) {
267+ this .pdfErrorHandler (error);
258268 }
259269 },
260270 downloadSample () {
@@ -269,11 +279,27 @@ export default {
269279 const blob = new Blob ([data .data ], { type: " application/pdf" });
270280 saveAs (blob, " WhatsAnalyze - " + this .ego );
271281 this .isLoading = false ;
282+ this .closePdfWorker ();
272283 }
273284 if (data .type === " progress" ) {
274285 this .progress = data .data ;
275286 }
276287 },
288+ pdfErrorHandler (error ) {
289+ console .error (" PDF generation failed" , error);
290+ this .$sentry ? .captureException (error);
291+ this .isLoading = false ;
292+ this .progress = 0 ;
293+ this .closePdfWorker ();
294+ },
295+ closePdfWorker () {
296+ if (! this .pdfWorker ) return ;
297+
298+ this .pdfWorker .removeEventListener (" message" , this .workerResponseHandler );
299+ this .pdfWorker .removeEventListener (" error" , this .pdfErrorHandler );
300+ this .pdfWorker .terminate ();
301+ this .pdfWorker = null ;
302+ },
277303 gtagEvent,
278304 },
279305};
0 commit comments