Closed mizzao closed 3 years ago
Jake and Ghost will be able to provide more insight into this- but in the meantime, some things that help me:
$ready()
after an async function to tell tossr when it's ok to trigger the SSR. This usually fixes issues with SSR'ing async code.window.routify.inBrowser
boolean to avoid running certain code in SSR.Not sure any of that helps me debug what is going on...
More info: If I manually run routify
in one terminal and rollup -cw
in another terminal, then the rollup thread running tossr
/ spassr
just dies silently when the error is thrown.
Currently, it seems that if I amend the code a bit from here: https://github.com/roxiness/spassr/blob/master/src/spassr.js#L37:
const foo = await tossr(entrypoint, script, req.url, ssrOptions);
console.log(foo);
return res.send(foo)
The value of foo
gets printed to the console, but then express just abruptly quits. It is very different than a route that is handled properly. I have no idea why...it's very mysterious because the tossr
promise seems to return a value, not an error.
I think I have narrowed this down to the use of jsdom
in tossr
: https://github.com/jsdom/jsdom/issues/2346.
Basically, as the DOM is getting torn down, after SSR is complete, an async error is unhandled and this causes node to quit. The async error is in my frontend code, of course, but it's very hard to debug when everything just exits and nothing prints out. The browser is a lot more forgiving of errors like these since it is generally running async code, but Node does not like them. Perhaps there is a way to force unhandled rejections to be just printed out: https://thecodebarbarian.com/unhandled-promise-rejections-in-node.js.html
This seems to be a useful clue: https://nodejs.org/api/process.html#process_event_rejectionhandled
The built version of the code above also shows part of the error.
Thanks for looking into this @mizzao. Would you be interested in submitting a PR for this?
Beat you to it: https://github.com/roxiness/tossr/pull/11/. Happy to take any feedback there.
I'm having a hard time tracking down errors in SSR code right now. For example, if there is a runtime error on the SSR function, both options don't work particularly well:
With
npm run dev:ssr
,tossr
appears to be fine, but has actually actually died and no longer responds to connections. The error does not appear anywhere.with
npm run serve
the error appears but thespassr
just dies, and does not print out the whole error. It is also annoying to debug because it requires a newnpm run build
to try again, which can take a while.Happy to help with improvements to
tossr
,spassr
or anything else to make this work.