@@ -22,35 +22,46 @@ export async function workerThread(sys: System) {
2222 delete ( globalThis as any ) . __qwik ;
2323 const ssgOpts = sys . getOptions ( ) ;
2424 const pendingPromises = new Set < Promise < any > > ( ) ;
25+ const log = await sys . createLogger ( ) ;
2526
2627 const opts : SsgHandlerOptions = {
2728 ...ssgOpts ,
29+ // TODO export this from server
2830 render : ( await import ( pathToFileURL ( ssgOpts . renderModulePath ) . href ) ) . default ,
31+ // TODO this should be built-in
2932 qwikRouterConfig : ( await import ( pathToFileURL ( ssgOpts . qwikRouterConfigModulePath ) . href ) )
3033 . default ,
3134 } ;
3235
33- sys . createWorkerProcess ( async ( msg ) => {
34- switch ( msg . type ) {
35- case 'render' : {
36- return new Promise < SsgWorkerRenderResult > ( ( resolve ) => {
37- workerRender ( sys , opts , msg , pendingPromises , resolve ) ;
38- } ) ;
39- }
40- case 'close' : {
41- const promises = Array . from ( pendingPromises ) ;
42- pendingPromises . clear ( ) ;
43- await Promise . all ( promises ) ;
44- return { type : 'close' } ;
36+ sys
37+ . createWorkerProcess ( async ( msg ) => {
38+ switch ( msg . type ) {
39+ case 'render' : {
40+ log . debug ( `Worker thread rendering: ${ msg . pathname } ` ) ;
41+ return new Promise < SsgWorkerRenderResult > ( ( resolve ) => {
42+ workerRender ( sys , opts , msg , pendingPromises , resolve ) . catch ( ( e ) => {
43+ console . error ( 'Error during render' , msg . pathname , e ) ;
44+ } ) ;
45+ } ) ;
46+ }
47+ case 'close' : {
48+ if ( pendingPromises . size ) {
49+ log . debug ( `Worker thread closing, waiting for ${ pendingPromises . size } pending renders` ) ;
50+ const promises = Array . from ( pendingPromises ) ;
51+ pendingPromises . clear ( ) ;
52+ await Promise . all ( promises ) ;
53+ }
54+ log . debug ( `Worker thread closed` ) ;
55+ return { type : 'close' } ;
56+ }
4557 }
46- }
47- } ) ;
58+ } )
59+ ?. catch ( ( e ) => {
60+ console . error ( 'Worker process creation failed' , e ) ;
61+ } ) ;
4862}
4963
5064export async function createSingleThreadWorker ( sys : System ) {
51- // Special case: we allow importing qwik again in the same process, it's ok because we just needed the serializer
52- // TODO: remove this once we have vite environment API and no longer need the serializer separately
53- delete ( globalThis as any ) . __qwik ;
5465 const ssgOpts = sys . getOptions ( ) ;
5566 const pendingPromises = new Set < Promise < any > > ( ) ;
5667
@@ -63,7 +74,9 @@ export async function createSingleThreadWorker(sys: System) {
6374
6475 return ( staticRoute : SsgRoute ) => {
6576 return new Promise < SsgWorkerRenderResult > ( ( resolve ) => {
66- workerRender ( sys , opts , staticRoute , pendingPromises , resolve ) ;
77+ workerRender ( sys , opts , staticRoute , pendingPromises , resolve ) . catch ( ( e ) => {
78+ console . error ( 'Error during render' , staticRoute . pathname , e ) ;
79+ } ) ;
6780 } ) ;
6881 } ;
6982}
@@ -270,6 +283,13 @@ async function workerRender(
270283 console . error ( 'Error during request handling' , staticRoute . pathname , e ) ;
271284 }
272285 } )
286+ . catch ( ( e ) => {
287+ console . error ( 'Unhandled error during request handling' , staticRoute . pathname , e ) ;
288+ result . error = {
289+ message : String ( e ) ,
290+ stack : e . stack || '' ,
291+ } ;
292+ } )
273293 . finally ( ( ) => {
274294 pendingPromises . delete ( promise ) ;
275295 callback ( result ) ;
0 commit comments