A Server Component
// app/components/qr-code.tsx
import Image from "next/image"
export function QrCode({ value, size = 200 }: { value: string; size?: number }) {
const src = `https://qrcodesforever.com/qr.svg?${new URLSearchParams({ url: value })}`
return <Image src={src} width={size} height={size} alt={`QR code for ${value}`} unoptimized />
}
There's no "use client", so it renders on the server. URLSearchParams handles the encoding: a value of https://example.com/menu?table=4 arrives intact instead of losing everything after the ?.
Why unoptimized
The next/image docs say an SVG src is blocked unless unoptimized or dangerouslyAllowSVG is set, and that unoptimized is applied automatically only when src ends in .svg. Ours ends in ?url=…, so set it yourself. With it, Next.js serves the URL as given, and you need no remotePatterns entry. Optimizing an SVG wouldn't shrink it anyway.
If you'd rather use the PNG and let Next.js optimize it, allow the host in next.config.ts:
import type { NextConfig } from "next"
const config: NextConfig = {
images: {
remotePatterns: [
// No `search` key, so any query string is allowed on this one path.
{ protocol: "https", hostname: "qrcodesforever.com", pathname: "/qr.png" },
],
},
}
export default config
Use the object form. The new URL(...) shorthand copies the URL's empty query string into the pattern, which blocks every query string, and every QR code URL has one.
A code for each page
// app/events/[slug]/page.tsx
import { QrCode } from "@/app/components/qr-code"
export default async function Page({ params }: { params: Promise<{ slug: string }> }) {
const { slug } = await params
return <QrCode value={`https://yoursite.com/events/${slug}`} size={160} />
}
params is a Promise in current App Router pages, so await it. This suits printable event pages and product pages, where someone reading on a laptop wants the page on their phone.
For other kinds of code, change the object you pass to URLSearchParams. { type: "event", title: "Launch party", start: "2027-03-14T18:00", tz: "Europe/Berlin" } produces a calendar event with DTSTART:20270314T170000Z inside, converted to UTC. Every type and its fields are in the embed reference.
Static export and caching
With output: "export", the default image optimizer isn't available, so use a plain <img> with the same src, as in QR codes in HTML. It behaves identically for an SVG. Each response carries a one-year Cache-Control header and never changes for the same URL, so ISR or a cache of your own adds nothing. For a live preview that updates as someone types, see the React guide.