From 24f98a5d683b49cd34e7d993fd73160012d8562e Mon Sep 17 00:00:00 2001 From: webreflection Date: Fri, 26 Jul 2024 12:56:34 +0200 Subject: [PATCH 1/3] Update SharedArrayBuffer + add service-worker attribute details --- docs/faq.md | 70 +++++++------------- docs/user-guide/workers.md | 132 +++++++++++++++++++++++++++++-------- 2 files changed, 127 insertions(+), 75 deletions(-) diff --git a/docs/faq.md b/docs/faq.md index c42a1c7..4608269 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,28 @@ 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 need those features and you are not able to configure your server's headers, +there are at least 2 options: use the +[mini-coi](https://github.com/WebReflection/mini-coi#readme) project +to enforce server side headers on each request or +use the `service-worker` attribute. + +### Option 1: mini-coi + +This is still a preferred option, mostly for performance reasons, so that +*Atomics*' synchronous operations can work 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 + +Recently introduced, each ` + + +``` + +!!! note + + Using the *sabayon* fallback to enable *Atomics* synchronous operations + should be the least solution to consider because it is inevitably + slower than having native operations enabled by other means. + + When that is still needed or desired, it is always better to 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 amount of operations have been reduced +from *6* to just *4* and 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 `