From 03647ca34edb809c77b1e0ce1f43da9333041c85 Mon Sep 17 00:00:00 2001 From: Jieiku Date: Wed, 7 Aug 2024 16:07:46 -0700 Subject: allow wasm on pagefind/tinysearch cloudflare demo sites --- package_abridge.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/package_abridge.js b/package_abridge.js index 9ddfdce..ba2deba 100644 --- a/package_abridge.js +++ b/package_abridge.js @@ -162,6 +162,7 @@ async function abridge() { } if (bpath === '') {// abridge used directly + _headersWASM(); // These are truely static js files, so they should only need to be updated by the abridge maintainer or contributors. minify(['static/js/theme.js']); minify(['static/js/theme_light.js']); @@ -199,6 +200,22 @@ async function abridge() { await execWrapper('zola build'+args); } +async function _headersWASM() { + // running WASM in the browser requires wasm-unsafe-eval if using Content-Security-Policy: + // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#unsafe_webassembly_execution + // This function adds wasm-unsafe-eval to the pagefind and tinysearch demos without adding it to the elasticlunr demo. + // I keep hoping they will add something like: wasm-src 'self' but as far as I know nothing like that exists. + + const { replaceInFileSync } = await import('replace-in-file'); + if (search_library === 'pagefind') { + replaceInFileSync({files: 'static/_headers', from: /script-src 'self'/g, to: "script-src 'wasm-unsafe-eval' 'self'"}); + } else if (search_library === 'tinysearch') { + replaceInFileSync({files: 'static/_headers', from: /script-src 'self'/g, to: "script-src 'wasm-unsafe-eval' 'self'"}); + } else { + replaceInFileSync({files: 'static/_headers', from: /script-src 'wasm-unsafe-eval' 'self'/g, to: "script-src 'self'"}); + } +} + function bundle(bpath,js_prestyle,js_switcher,js_email_encode,js_copycode,search_library,index_format,uglyurls,pwa) { minify_files = []; -- cgit v1.3.1