diff --git a/docs/faq.md b/docs/faq.md index c42a1c7..f249f23 100644 --- a/docs/faq.md +++ b/docs/faq.md @@ -49,7 +49,7 @@ encounter. ### SharedArrayBuffer -This is the first and most common error users may encounter with PyScript. +This is the first and most common error users may encounter with PyScript: !!! failure @@ -58,52 +58,35 @@ This is the first and most common error users may encounter with PyScript. you see this message: ``` - Unable to use SharedArrayBuffer due insecure environment. - Please read requirements in MDN: ... + Unable to use `window` or `document` -> https://docs.pyscript.net/latest/faq/#sharedarraybuffer ``` -The error contains -[a link to MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements) -but it's the amount of content provided on this topic is overwhelming. - #### When -This error happens when **the server delivering your PyScript application is -incorrectly configured**. It fails to provide the correct headers to handle -security concerns for web workers, or you're not using -[mini-coi](https://github.com/WebReflection/mini-coi#readme) as an alternative -solution. (These requirements are explored -[in the worker page](../user-guide/workers#http-headers).) - -**And** at least one of the following scenarios is true: - -* There is a `worker` attribute in the *py* or *mpy* script element and the - [sync_main_only](https://pyscript.github.io/polyscript/#extra-config-features) - flag is not present or not `true`. -* There is a ` - - - - +If you're unable to configure your server's headers, you have two options: + +1. Use the [mini-coi](https://github.com/WebReflection/mini-coi#readme) project + to enforce headers. +2. Use the `service-worker` attribute with the `script` element. + +### Option 1: mini-coi + +For performance reasons, this is the preferred option so Atomics works at +native speed. + +The simplest way to use mini-coi is to copy the +[mini-coi.js](https://raw.githubusercontent.com/WebReflection/mini-coi/main/mini-coi.js) +file content and save it in the root of your website (i.e. `/`), and reference +it as the first child tag in the `` of your HTML documents: + +```html + + + + + + + +``` + +### Option 2: `service-worker` attribute + +This allows you to slot in a custom +[service worker](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API) +to handle requirements for synchronous operations. + +Each ` + + +``` + +!!! warning + + Using sabayon as the fallback for synchronous operations via Atomics + should be **the last solution to consider**. It is inevitably + slower than using native Atomics. + + If you must use sabayon, always reduce the amount of synchronous + operations by caching references from the *main* thread. + + ```python + # ❌ THIS IS UNNECESSARILY SLOWER + from pyscript import document + + # add a data-test="not ideal" attribute + document.body.dataset.test = "not ideal" + # read a data-test attribute + print(document.body.dataset.test) + + # - - - - - - - - - - - - - - - - - - - - - + + # ✔️ THIS IS FINE + from pyscript import document + + # if needed elsewhere, reach it once + body = document.body + dataset = body.dataset + + # add a data-test="not ideal" attribute + dataset.test = "not ideal" + # read a data-test attribute + print(dataset.test) ``` +In latter example the number of operations has been reduced from six to just +four. The rule of thumb is: _if you ever need a DOM reference more than once, +cache it_. 👍 + + ## Start working To start your code in a worker, simply ensure the `