barcode-detector: Barcode detection service unavailable
i used vue-qrcode-reader at https website
<qrcode-stream @detect="onDetect"></qrcode-stream>
it shows "Barcode detection service unavailable. Use 'setZXingModuleOverrides' in offline or strict CSP environments.", "NotSupportedError"
the host already set Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
but the static js file not set
i see the demo website https://vue-qrcode-reader.netlify.app. it’s doc and static js both set Strict-Transport-Security
how can i solve the detect error?
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 47 (24 by maintainers)
Oooh, I’m stupid 😅 I forgot I had disabled
console & debuggeron build…This is the actual error:
And the line it occurs:
Nope, nothing else is printed on console
I thought so too, but the thing is… The Network Request doesn’t show when it’s working successfully with no error.
Yeah, I thought so, probably some kind of header issue. I’ll have to check with dev-ops tomorrow…
Thanks for all the help 🙏
Sadly, he AWS belongs to a client, so I can’t really provide a direct link to the server right now. I’ll check if I can figure a way to provide you with a link to a problematic instance.
Sure, here you go, it’s quite basic really: https://github.com/crazyrabbit0/TestVueQrcode
Mind the fact, that when I build it locally it works fine, even with separate asset files, but when I push it to the AWS server, I get the error mentioned above.
https://github.com/louie0086/test_Barcode/blob/f1834f83f809259f7038b52557df0a9f3df58d93/barcode-detector.pure_4.js#L564-L566
Ok you decide not to fetch the
.wasmfile from jsDelivr. There’re some gotchas, so I’d like to explain more:First, a question: I wonder if you want to inline the
.wasmfile inside your script output files or you want to host it as a binary file on your site, and fetch it from your site at runtime?Inline
Inlining means encoding the
.wasmfile as a Base64 data url and hardcode this data url inside your script files. If you use a syntax like this:then you’re inlining it.
?urlis a syntax provided by build tools, like Vite, to transform assets into data urls. I’m not sure ifvue-clisupports it. If it is supported, then thewasmFilewill be a huge base64 encoded data url.Afterwards, you use
setZXingModuleOverrides+locateFileto relocate the fetching path to this data url:A data url occupies a larger space than its binary form, and parsing a data url into binary data also consumes more CPU clocks. So, ideally, I suggest you to not use the inline form.
Binary
If you want to host it as a binary file on your site, then you need not to import it. You can just copy the
.wasmfile to your website’s assets folder, e.g.publicorassets, and usesetZXingModuleOverrides+locateFileto relocate the fetching path like this:Either way, CSP still applies. You have to make sure
content-srcandscript-srcare correctly configured to allow you fetching and executing it.Second question: where is the
.wasmfile?Normally, when you add
vue-qrcode-readeras a dependency in your project, it will also download its dependencies insidenode_modulesfolder, which includesbarcode-detector. Andbarcode-detectorin turn will download its dependency@sec-ant/zxing-wasm. This process is handled by the package manager automatically so you don’t need to worry.The
.wasmfile is published as an asset included in the package@sec-ant/zxing-wasm, therefore, the.wasmfile can be found at this path:BUT, I made a mistake not pinning the version. So, the
.wasmfile you found in<project root>/node_modules/@sec-ant/zxing-wasmmay not be whatvue-qrcode-readerexpects (Sorry for this mistake, I’m going to fix this in the next version). Therefore, for now, I recommend you manually download the.wasmfile with the correct version, from the CDN.If you’re using
vue-qrcode-reader@v5.3.4, the matching version is@sec-ant/zxing-wasm@2.1.3, so you can download thezxing_reader.wasmfile from any of the following CDN paths:Once you’ve downloaded the correct version of the
.wasmfile, you can inline or host it as you wish in your project.Again, I’m really sorry for the inconvenience. Once the version problem is fixed and released in
vue-qrcode-reader, you won’t have to worry about downloading it with the matching version. You can just import or copy it from<project root>/node_modules/@sec-ant/zxing-wasm/dist/reader/zxing_reader.wasm.Hey, louie
I don’t think
Strict-Transport-Securityis relevant here. “CSP” is short for “Content Security Policy”.This package uses WebAssembly to provide the core function of detecting barcodes. And the browser must instantiate and execute the WebAssembly module to use it. However, per the spec, websites can control whether to block the WebAssembly execution by setting the
script-srcdirective in CSP, specifically thewasm-unsafe-evalorunsafe-evalsource value. If it is blocked, the errorBarcode detection service unavailablewill get thrown.The directive
connect-srccan also affect this package. To prevent bloating the size, this package will load the.wasmfile from thecdn.jsdelivr.netCDN at runtime (usingfetch).connect-srcordefault-srccan restrict the URLs which can be loaded withfetch. So if this connection is somewhat get blocked, theservice unavailableerror will also be thrown.There’re corresponding ways in which you can solve this problem:
wasm-unsafe-eval. and make sureconnect-srcordefault-srcincludeshttps://cdn.jsdelivr.net. A detailed explanation can be found at https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy.wasmfile from a CDN. You can usesetZXingModuleOverridesexported byvue-qrcode-readerto change the location where the.wasmfile is hosted. For example, you can host the file on your website as an asset, etc.If neither solves your problem, it would be great if you can provide the malfunctioning website so that I can dig into it and check whether there’re some other bugs to fix.