Repository navigation
Implement WebAssembly support detection and fallbacks #74827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,57 @@ | ||
| # Technical Documentation | ||
|
|
||
| ## Browser Compatibility | ||
|
|
||
| Client-side media processing requires the following browser capabilities: | ||
|
|
||
| | Feature | Required | Purpose | | ||
| |---------|----------|---------| | ||
| | WebAssembly | Yes | Runs the wasm-vips image processing library | | ||
| | SharedArrayBuffer | Yes | Enables multi-threaded WASM execution | | ||
| | Cross-origin isolation | Yes | Required for SharedArrayBuffer in modern browsers | | ||
| | CSP `blob:` workers | Yes | The WASM worker is loaded via a blob URL | | ||
|
|
||
| ### Browser Support Matrix | ||
|
|
||
| | Browser | Minimum Version | Notes | | ||
| |---------|-----------------|-------| | ||
| | Chrome | 92+ | Full support | | ||
| | Firefox | 79+ | Full support except embed previews | | ||
| | Safari | 15.2+ | Requires `require-corp` instead of `credentialless` | | ||
| | Edge | 92+ | Full support | | ||
|
|
||
| ### Automatic Fallback | ||
|
|
||
| When client-side media processing is unavailable, the system automatically falls back to server-side processing. This fallback is transparent to users and requires no action. A message is logged to the browser console indicating the reason for the fallback. | ||
|
|
||
| The fallback occurs when any of the following conditions are detected: | ||
| - WebAssembly is not supported in the browser | ||
| - SharedArrayBuffer is not available | ||
| - Cross-origin isolation is not enabled (missing required headers) | ||
| - The site's Content Security Policy (CSP) blocks blob URL workers | ||
|
|
||
| ## Cross-origin isolation / `SharedArrayBuffer` | ||
|
|
||
| WASM-based image optimization requires `SharedArrayBuffer` support, which in turn requires [cross-origin isolation](https://web.dev/articles/cross-origin-isolation-guide). To implement this, the editor uses the following headers: | ||
| - `Cross-Origin-Opener-Policy: same-origin` | ||
| - `Cross-Origin-Embedder-Policy: credentialless` (or `require-corp` for safari) | ||
| WASM-based image optimization requires `SharedArrayBuffer` support, which in turn requires [cross-origin isolation](https://web.dev/articles/cross-origin-isolation-guide). | ||
|
|
||
| Once the page is served with these headers, `SharedArrayBuffer` will be available in the browser, and WASM-based image optimization will work as expected. However, all embedded resources (e.g., images, iframes, scripts) must also be served with appropriate CORS headers (or iframe with `iframe-credentialless` for supporting browsers) to ensure cross-origin isolation is maintained. For third party embeds (for example a YouTube video), the plugin uses [iframe `credentialless` attribute](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/IFrame_credentialless) to help with this. For browsers that do not support this attribute, embeds will show an information pane instead of a live preview. | ||
|
|
||
| ### Troubleshooting | ||
|
|
||
| If client-side media processing is not working, check the browser console for messages. Common issues include: | ||
|
|
||
| 1. **"SharedArrayBuffer is not available"**: The server is not sending the required cross-origin isolation headers. | ||
| 2. **"Cross-origin isolation is not enabled"**: The headers are present but cross-origin isolation is not active. This can happen if: | ||
| - Third-party resources are blocking isolation | ||
| - Headers are being stripped by a proxy or CDN | ||
| - The page is being served over HTTP instead of HTTPS | ||
|
|
||
| 3. **"WebAssembly is not supported"**: The browser does not support WebAssembly. This is rare in modern browsers but can occur in older versions or restricted environments. | ||
|
|
||
| Once the page is served with these headers, `SharedArrayBuffer` will be available in the browser, and WASM-based image optimization will work as expected. However, all embedded resources (e.g., images, iframes, scripts) must also be served with appropriate CORS headers to ensure cross-origin isolation is maintained. For third party embeds (for example a YouTube video), the plugin uses [iframe `credentialless` attribute](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/IFrame_credentialless) to help with this. For browsers that do not support this attribute, embeds will show an information pane instead of a live preview. | ||
| 4. **"Content Security Policy (CSP) does not allow blob: workers"**: A security plugin or server configuration is setting a `worker-src` CSP directive that does not include `blob:`. The WASM image processing worker is loaded via a blob URL, which requires CSP to permit it. To resolve this: | ||
| - Add `blob:` to the `worker-src` directive in the site's CSP header (e.g., `worker-src 'self' blob:`) | ||
| - If using a security plugin (e.g., WP Cerber, Wordfence, or similar), check its CSP settings and add `blob:` to the allowed worker sources | ||
| - If the CSP header is set at the server level (e.g., in `.htaccess`, Nginx config, or a CDN), update it there | ||
|
|
||
| Check out [this tracking issue](https://github.com/swissspidy/media-experiments/issues/294) for more details and further resources. | ||
| Check out [this tracking issue](https://github.com/WordPress/gutenberg/issues/74464) for more details and further resources. | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| /** | ||
| * Result of client-side media processing support detection. | ||
| */ | ||
| export interface FeatureDetectionResult { | ||
| /** | ||
| * Whether client-side media processing is supported. | ||
| */ | ||
| supported: boolean; | ||
| /** | ||
| * Reason why client-side media processing is not supported (if applicable). | ||
| */ | ||
| reason?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Cached result of feature detection. | ||
| */ | ||
| let cachedResult: FeatureDetectionResult | null = null; | ||
|
|
||
| /** | ||
| * Detects whether the browser supports client-side media processing. | ||
| * | ||
| * This checks for: | ||
| * 1. WebAssembly support (required for wasm-vips) | ||
| * 2. SharedArrayBuffer support (required for WASM threading) | ||
| * 3. CSP compatibility for blob URL workers (required for inline worker creation) | ||
| * | ||
| * @return Feature detection result with supported status and reason if not supported. | ||
| */ | ||
| export function detectClientSideMediaSupport(): FeatureDetectionResult { | ||
| // Return cached result if available. | ||
| if ( cachedResult !== null ) { | ||
| return cachedResult; | ||
| } | ||
|
|
||
| // Check WebAssembly support. | ||
| if ( typeof WebAssembly === 'undefined' ) { | ||
| cachedResult = { | ||
| supported: false, | ||
| reason: 'WebAssembly is not supported in this browser', | ||
| }; | ||
| return cachedResult; | ||
| } | ||
|
Comment on lines
+36
to
+43
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it can't hurt, but I don't think there is any modern browser that doesn't support it. It's been around for 9 years.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trying to be thorough but yeah, we can probably remove this. |
||
|
|
||
| // Check SharedArrayBuffer support (required for WASM threading). | ||
| if ( typeof SharedArrayBuffer === 'undefined' ) { | ||
| cachedResult = { | ||
| supported: false, | ||
| reason: 'SharedArrayBuffer is not available. This may be due to missing cross-origin isolation headers.', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| }; | ||
| return cachedResult; | ||
| } | ||
|
|
||
| // Check that blob URL workers are allowed by CSP. | ||
| // Security plugins often set a strict worker-src directive that blocks blob: URLs, | ||
| // which would prevent creating the WASM processing worker at runtime. | ||
| if ( typeof window !== 'undefined' && typeof Worker !== 'undefined' ) { | ||
| try { | ||
| const testBlob = new Blob( [ '' ], { | ||
| type: 'application/javascript', | ||
| } ); | ||
| const testUrl = URL.createObjectURL( testBlob ); | ||
| try { | ||
| const testWorker = new Worker( testUrl ); | ||
| testWorker.terminate(); | ||
| } finally { | ||
| URL.revokeObjectURL( testUrl ); | ||
| } | ||
| } catch { | ||
| cachedResult = { | ||
| supported: false, | ||
| reason: "The site's Content Security Policy (CSP) does not allow blob: workers. The worker-src directive must include blob: to enable client-side media processing.", | ||
| }; | ||
| return cachedResult; | ||
| } | ||
| } | ||
|
|
||
| cachedResult = { supported: true }; | ||
| return cachedResult; | ||
| } | ||
|
|
||
| /** | ||
| * Returns whether client-side media processing is supported. | ||
| * | ||
| * This is a convenience function that returns just the boolean result. | ||
| * | ||
| * @return Whether client-side media processing is supported. | ||
| */ | ||
| export function isClientSideMediaSupported(): boolean { | ||
| return detectClientSideMediaSupport().supported; | ||
| } | ||
|
|
||
| /** | ||
| * Clears the cached feature detection result. | ||
| * | ||
| * This is primarily useful for testing purposes. | ||
| */ | ||
| export function clearFeatureDetectionCache(): void { | ||
| cachedResult = null; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,5 +7,11 @@ export { uploadStore as store }; | |
|
|
||
| export { default as MediaUploadProvider } from './components/provider'; | ||
| export { UploadError } from './upload-error'; | ||
| export { | ||
| detectClientSideMediaSupport, | ||
| isClientSideMediaSupported, | ||
| clearFeatureDetectionCache, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tiny nit: do we need to export That said, if consumers need to clear the cache, it makes sense to include it, so not a blocker to me. |
||
| } from './feature-detection'; | ||
|
adamsilverstein marked this conversation as resolved.
Resolved
|
||
|
|
||
| export type { ImageFormat } from './store/types'; | ||
| export type { FeatureDetectionResult } from './feature-detection'; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just had a thought: should all this live in the
upload-mediapackage, or will we need anything that only exists in theblock-editorpackage?The reason I ask, is that we've also got conditional logic in the
editorpackage for post locking, so I imagine we'll want to use this check over there, too, in place of the experiment check once we stabilise client-side media processing (i.e. what you're doing in #75112).This might kind of be feedback for that PR, but the overall question is: should we have one place that incorporates all the different kinds of checks (e.g.
window.__clientSideMediaProcessing+ device capability, etc, etc) and use that function check elsewhere?So far, it seem this function is such a utility for this. And if so, then maybe
upload-mediais a good place for it.What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense to me, I'll give that a try here or in a follow up pr.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All good. Feel free to land this PR first if it winds up being a bit rabbit-holey.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to merge this, then loop back to the final feedback in a follow up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #75605