Best practices

Things you should consider when writing an application.

To create the best possible apps in terms of performance, reliability, and user experience, you should take the following practices into account.

Avoid polluting the global scope

This can potentially cause conflicts, so never assign anything to window directly. All apps are ran under WRT, which is the runtime used to run JS applications under Windows 96. WRT ensures the application has its own scope, but it cannot prevent anything from being assigned to window directly.

Consider const for variables that will not change state

This allows for better optimization of your JavaScript code and also ensures a level of clarity in your sources. Here is an example where using const is applicable.

const currentVersion = 1.0;

function updateAvailable() {
    const remoteVersion = getRemoteVersion();
    return currentVersion < remoteVersion;
}

Always revoke Blob object URLs

Ensure that you revoke any Blob object URLs to avoid unneeded memory usage in your application. Below is an example of how to do it.

const coolFileURL = URL.createObjectURL(await w96.FS.toBlob("c:/my cool file.txt"));
window.open(coolFileURL); // Do something with this URL
URL.revokeObjectURL(coolFileURL);

FS paths are absolute

The Windows 96 FS API does not work with relative paths. This will likely be improved in a future version, but use w96.FSUtil.resolvePath(base: String, path: String) to resolve any relative paths before handling the file.

Avoid using iFrames whenever possible

Creating a separate browser context through an iFrame should only be necessary for applications that need a complete DOM or applications where resources cannot easily be disposed of in a predictable manner. Too many iFrames will cause a page to slow down and use more memory.

Use unique class names

Do not use common class names to style your Windows 96 apps, since they can be unintentionally overridden. Also use CSS selectors with reasonable specificity to ensure that the correct elements are being targeted when styling them in a stylesheet.

The JavaScript console is your best friend

If your application is not working as expected, you can take a look inside the JavaScript console to find out what went wrong. More often than not, the error message will exactly specify the error in question.

To open the console, press F12 (or a similar key combination) to open your browser's developer tools.

Do not use undocumented APIs

There are a number of internal APIs that may not be documented. This is not intentional, our goal is to document everything, but we prefer to document APIs developers will actually use first.

That said, if you know what you're doing, please be aware that internal APIs can be changed or removed at any time, so use them at your own risk.

Last updated