One of the ways in which writing reliable software for SharePoint Online differs from previous versions of SharePoint is all the extra redundancy that has to be built in. It's a little like writing software for a space mission, deployed in an environment where it can be subjected to many types of failure and must adapt. Every action needs at least two ways of accomplishing it in case the first one fails. It helps that one of our programmers did write software for NASA.
The biggest need for redundancy comes from Modern pages. Classic pages and modern pages do not have the same API. Any code that you put on classic pages cannot run on modern pages, and vice-versa. Except when it does. There are some cases where it is possible to execute code from a previous classic page when you are on a modern page. We have gotten very adept at executing JavaScript on pages where that JavaScript is not present.
This is because SharePoint Online does not always fully load a new page, but rather it often simulates transitions from page to page without fully wiping out the current page DOM and page data in the browser. These transitions, as well as the use of caching including caching of API calls, mean that some events that are normally triggered when loading a fresh page are not necessarily triggered when navigating to that page from another page.