TL;DR
- Keep page specific code out of app level custom code.
- Whenever possible, use plain HTML/CSS instead of Javascript.
- Use
<script type="module">to avoid global name conflicts. - Instead of “DOMContentLoaded” in page level level custom code, wait for elements that you need using
window.SOFTR_PAGE.waitFor. - Use
window.SOFTR_PAGE.beforeUnloadto cleanup elements and timers before navigating to a new page. This function can be called multiple times to register several cleanup functions.
Keep page specific code out of app level custom code
Scripts in the App header/App footer custom code execute only once—on the first page load. They won’t re-execute when navigating between pages, unlike in non-SPA mode. To ensure your app looks consistent whether you’re navigating from another page or visiting directly via URL, keep page-specific code in custom code blocks on the page or in page-level custom code.Cleanup any dynamically inserted DOM nodes on page navigation
When navigating to a new page, HTML elements dynamically injected by JavaScript outside the custom code’s own scope won’t be automatically cleaned up. Use thewindow.SOFTR_PAGE.beforeUnload API to remove any dynamically rendered DOM nodes. This ensures that navigating from page A to page B doesn’t leave behind remnants from page A.
Here’s an example
Cleanup timers and resize observers on page unload
If your page custom code sets up timers or resize observers, they need to be cleaned up before the page unloads. This ensures that the timers don’t keep running after navigating away from the page. For example, here’s a custom code block which shows the number of seconds this page has been open forDon’t use DOMContentLoaded in page level custom code
DOMContentLoaded will only fire once, on app load. Navigating between the pages will not re-run the event listener. Instead,window.SOFTR_PAGE.waitFor provides a simple way to wait for an element to be on the page before continuing.