Generating QR codes on the client means shipping a library to do it. The node-qrcode package, for instance, weighs about 180 KB once all its dependencies are counted — a lot of payload for a page that only occasionally needs a code. A serverless function moves that work off the browser: the code stays on a host and is reached through a URL whenever it's needed.

DigitalOcean Functions is one option, with a CLI that scaffolds and deploys projects and support for PHP and Python alongside our choice, Node. The walkthrough below builds a small generator in four steps: scaffold, code, configure, deploy.

Scaffolding the project

From whatever directory you want to work in, the CLI creates the project with the language declared explicitly:

doctl serverless init --language js qr-generator

The result is a qr-generator project containing a /packages folder, which is where every function in the project lives. It ships with a sample function; ignore that and create a qr directory beside it:

That directory holds both the qrcode package and the function file. Change into packages/sample/qr and install the dependency:

npm install --save qrcode

The function itself

Create qr.js with the generator:

const qrcode = require('qrcode')

exports.main = (args) => {
  return qrcode.toDataURL(args.text).then(res => ({
    headers:  { 'content-type': 'text/html; charset=UTF-8' },
    body: args.img == undefined ? res : `<img src="${res}">`
  }))
}

if (process.env.TEST) exports.main({text:"hello"}).then(console.log)

The module requires the qrcode package and exports a function that returns an <img> tag whose source is a base64-encoded PNG. It can be exercised straight from the terminal before any deployment:

doctl serverless functions invoke sample/qr -p "text:css-tricks.com"

Pointing the config at the new function

The scaffold also produced a project.yml file that tells the host how the function is wired up. Its default contents:

targetNamespace: ''
parameters: {}
packages:
  - name: sample
    environment: {}
    parameters: {}
    annotations: {}
    actions:
      - name: hello
        binary: false
        main: ''
        runtime: 'nodejs:default'
        web: true
        parameters: {}
        environment: {}
        annotations: {}
        limits: {}

Two properties matter. The packages: name value is the folder under packages that contains the function — sample here. The actions/ name value is the function's name, which matches its file name. That value defaults to hello, so it must be changed to qr to match qr.js.

Deploying and calling it

First, connect the CLI to the DigitalOcean sandbox so there's a live URL to test against:

## You will need an DO API key handy
doctl sandbox connect

Then deploy:

doctl sandbox deploy qr-generator

After deployment, the function is reachable at a URL. To get it, ask the CLI:

doctl sbx fn get sample/qr --url
https://faas-nyc1-2ef2e6cc.doserverless.co/api/v1/web/fn-10a937cb-1f12-427b-aadd-f43d0b08d64a/sample/qr

With the URL in hand, the 180 KB package never has to be bundled with the rest of the site's scripts. A fetch call against the endpoint returns the generated QR code, and that is the whole of the client side.