How to Build a Simple PWA Barcode Reader

Progressive web apps (PWA), simply to say, are web apps that running like native apps on the desktop and mobile platforms. The advantages of PWA include installable, discoverable, linkable and so on. In this article, I’m trying to give a snapshot of a simple PWA barcode reader project that built with Dynamsoft JavaScript barcode SDK.


Since from the day that mobile became the mainstream, users are spending more of time in native apps rather than on the web.

However, there are some drawbacks of using native apps:

  • It takes time to install native apps from the app store.
  • Users tend to install many apps but only use a few of them, which is a waste to system space.

Progressive web apps feature native app capabilities, such as push notification, offline work, background sync, sensor access and so on.

How doe a Progressive Web App Work

The main component of PWA is a service worker that runs in the background for caching web resources and providing native capabilities.

PWA diagram

PWA Browser Compatibility

Browser compatibility is important for web app development. You can find the PWA features supported by Chrome, Safari, Edge, Firefox and Opera here:

Building and Publishing PWA Barcode Reader

Download Dynamsoft JavaScript Barcode SDK.

Get a FREE 30-day trial license.

Get the source code of web barcode reader app from

Copy dbr- and dbr- to the project root directory.

A standard progressive web app has to meet the following criteria:

  • Hosted over HTTPS.
  • Include a web manifest file.
  • Register a service worker.

Create a manifest file manifest.json:

    "name": "BarcodeReader",
    "short_name": "BarcodeReader",
    "description": "A progressive web app for reading barcodes.",
    "icons": [
            "src": "icons/icon-192.png",
            "sizes": "192x192",
            "type": "image/png"
    "start_url": "/javascript-barcode/examples/pwa/index.html",
    "display": "standalone",
    "theme_color": "#2F3BA2",
    "background_color": "#3E4EB8"

With the manifest file, your app can be installed to the Android home screen and be launched as a native app.

Create a service-worker.js file:

var cacheName = 'barcode-reader-1';
var filesToCache = [

self.addEventListener('install', function(e) {
  console.log('[ServiceWorker] Install');
  e.waitUntil( {
      console.log('[ServiceWorker] Caching app shell');
      return cache.addAll(filesToCache);

self.addEventListener('activate', function(e) {
  console.log('[ServiceWorker] Activate');
    caches.keys().then(function(keyList) {
      return Promise.all( {
        if (key !== cacheName) {
          console.log('[ServiceWorker] Removing old cache', key);
          return caches.delete(key);
  return self.clients.claim();

self.addEventListener('fetch', function(e) {
    caches.match(e.request).then(function(r) {
      console.log('[Service Worker] Fetching resource: '+e.request.url);
      return r || fetch(e.request).then(function(response) {
        return {
          console.log('[Service Worker] Caching new resource: '+e.request.url);
          cache.put(e.request, response.clone());
          return response;

The JavaScript code above caches all resources required by the web app to make it work offline.

Register the service worker in app.js:

if ('serviceWorker' in navigator) {
        .then(function () {
            console.log('Service Worker Registered');

You can now test the PWA project with Web Server for Chrome or other web servers. Two things you have to know:

  1. The project relies on a wasm file. Web server for Chrome can work well with wasm file. For other web servers, you have to add MIME type ‘application/wasm’ to the web configuration file.
  2. Since Web Server for Chrome does not support HTTPS, you will fail to add PWA barcode reader to Android home screen.

The suggested way is to push your code to GitHub and activate the GitHub page in repository settings. Then visit your project page via ****.

Try my PWA barcode reader.

PWA barcode reader


PWA barcode reader desktop


PWA add to home screen


Source Code

About Dynamsoft Barcode Reader JavaScript Edition

Dynamsoft Barcode Reader JavaScript Edition is a JavaScript barcode scanning library based on the WebAssembly technology. It supports Code 39Code 93Code 128CodabarEAN-8EAN-13UPC-AUPC-EInterleaved 2 of 5 (ITF)Industrial 2 of 5 (Code 2 of 5 Industry, Standard 2 of 5, Code 2 of 5)ITF-14 QR codeDatamatrixPDF417 and Aztec code. The library is capable of scanning multiple barcodes from static images and camera video stream.

Online Demo

dynamsoft javascript barcode reader

Search Blog Posts