Skip to content

Commit 09196dc

Browse files
[workers] Update QR Code example (#17230)
* [workers] Update QR Code example Updates QR Code example to use a pure JS library for generating QR codes. Closes #14797 * Update src/content/docs/workers/tutorials/build-a-qr-code-generator/index.mdx Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> * Update with suggestions --------- Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com>
1 parent ee6fece commit 09196dc

File tree

1 file changed

+14
-34
lines changed
  • src/content/docs/workers/tutorials/build-a-qr-code-generator

1 file changed

+14
-34
lines changed

src/content/docs/workers/tutorials/build-a-qr-code-generator/index.mdx

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -122,43 +122,27 @@ export default {
122122

123123
## 3. Build a QR code generator
124124

125-
All projects deployed to Cloudflare Workers support npm packages. This support makes it easy to rapidly build out functionality in your Workers. The [`qr-image`](https://github.com/alexeyten/qr-image) package is a great way to take text and encode it into a QR code. The package supports generating the QR codes in a number of file formats (such as PNG, the default, and SVG) and configuring other aspects of the generated QR code. In the command line, install and save `qr-image` to your project’s `package.json`:
125+
All projects deployed to Cloudflare Workers support npm packages. This support makes it easy to rapidly build out functionality in your Workers. The ['qrcode-svg'](https://github.com/papnkukn/qrcode-svg) package is a great way to take text and encode it into a QR code. In the command line, install and save 'qrcode-svg' to your project’s 'package.json':
126126

127127
```sh title="Installing the qr-image package"
128-
npm install --save qr-image
128+
npm install --save qrcode-svg
129129
```
130130

131-
In `index.js`, import the `qr-image` package as the variable `qr`. In the `generateQRCode` function, parse the incoming request as JSON using `request.json`, and generate a QR code from `text` using `qr.imageSync`:
131+
In `index.js`, import the `qrcode-svg` package as the variable `QRCode`. In the `generateQRCode` function, parse the incoming request as JSON using `request.json`, and generate a new QR code using the `qrcode-svg` package. The QR code is generated as an SVG. Construct a new instance of `Response`, passing in the SVG data as the body, and a `Content-Type` header of `image/svg+xml`. This will allow browsers to properly parse the data coming back from your Worker as an image:
132132

133133
```js null {1,2,3,4,5,6}
134-
const qr = require("qr-image");
134+
import QRCode from "qrcode-svg";
135135

136136
async function generateQRCode(request) {
137137
const { text } = await request.json();
138-
const qr_png = qr.imageSync(text || "https://workers.dev");
138+
const qr = new QRCode({ content: text || "https://workers.dev" });
139+
return new Response(qr.svg(), { headers: { "Content-Type": "image/svg+xml" } });
139140
}
140141
```
141142

142-
By default, the QR code is generated as a PNG. Construct a new instance of `Response`, passing in the PNG data as the body, and a `Content-Type` header of `image/png`. This will allow browsers to properly parse the data coming back from your Worker as an image:
143-
144-
```js null {3,4,5}
145-
async function generateQRCode(request) {
146-
const { text } = await request.json();
147-
const headers = { "Content-Type": "image/png" };
148-
const qr_png = qr.imageSync(text || "https://workers.dev");
149-
return new Response(qr_png, { headers });
150-
}
151-
```
152-
153-
The `qr-image` package you installed depends on Node.js APIs. For this to work, you need to set the ["nodejs_compat_v2" compatibility flag](/workers/runtime-apis/nodejs/#enable-nodejs-with-workers) in your Wrangler configuration file:
154-
155-
```toml
156-
compatibility_flags = [ "nodejs_compat_v2"]
157-
```
158-
159143
## 4. Test in an application UI
160144

161-
The Worker will work if a user sends a `POST` request to a route, but it would be best practice to also be able to test the function with a proper interface. At this point in the tutorial, if any request is received by your function that is not a `POST`, a `405` response is returned. The new version of `fetch` should return a new `Response` with a static HTML document instead of the `405` error:
145+
The Worker will execute when a user sends a `POST` request to a route, but it is best practice to also provide a proper interface for testing the function. At this point in the tutorial, if any request is received by your function that is not a `POST`, a `405` response is returned. The new version of `fetch` should return a new `Response` with a static HTML document instead of the `405` error:
162146

163147
```js null {23-54}
164148
export default {
@@ -177,10 +161,8 @@ export default {
177161

178162
async function generateQRCode(request) {
179163
const { text } = await request.json();
180-
const headers = { "Content-Type": "image/png" };
181-
const qr_png = qr.imageSync(text || "https://workers.dev");
182-
183-
return new Response(qr_png, { headers });
164+
const qr = new QRCode({ content: text || "https://workers.dev" });
165+
return new Response(qr.svg(), { headers: { "Content-Type": "image/svg+xml" } });
184166
}
185167

186168
const landing = `
@@ -215,7 +197,7 @@ The `landing` variable, which is a static HTML string, sets up an `input` tag an
215197
With the above steps complete, your Worker is ready. The full version of the code looks like this:
216198

217199
```js
218-
const qr = require("qr-image");
200+
const QRCode = require("qrcode-svg");
219201

220202
export default {
221203
async fetch(request, env, ctx) {
@@ -233,10 +215,8 @@ export default {
233215

234216
async function generateQRCode(request) {
235217
const { text } = await request.json();
236-
const headers = { "Content-Type": "image/png" };
237-
const qr_png = qr.imageSync(text || "https://workers.dev");
238-
239-
return new Response(qr_png, { headers });
218+
const qr = new QRCode({ content: text || "https://workers.dev" });
219+
return new Response(qr.svg(), { headers: { "Content-Type": "image/svg+xml" } });
240220
}
241221

242222
const landing = `
@@ -278,6 +258,6 @@ npx wrangler deploy
278258

279259
## Related resources
280260

281-
In this tutorial, you built and deployed a Worker application for generating QR codes. If you would like to see the full source code for this application, you can find it [on GitHub](https://github.com/cloudflare/workers-sdk/tree/main/templates/examples/qr-code-generator).
261+
In this tutorial, you built and deployed a Worker application for generating QR codes. If you would like to see the full source code for this application, you can find it [on GitHub](https://github.com/kristianfreeman/workers-qr-code-generator).
282262

283-
If you want to get started building your own projects, review the existing list of [Quickstart templates](/workers/get-started/quickstarts/).
263+
If you want to get started building your own projects, review the existing list of [Quickstart templates](/workers/get-started/quickstarts/).

0 commit comments

Comments
 (0)