Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

svg bg when set

Pedro Ladeirae38a9cff9f1a
diff --git a/apps/web/app/api/qr/route.tsx b/apps/web/app/api/qr/route.tsx
index 77b47e5199..2e9bc088a6 100644
--- a/apps/web/app/api/qr/route.tsx
+++ b/apps/web/app/api/qr/route.tsx
@@ -3,6 +3,7 @@ import { ratelimitOrThrow } from "@/lib/api/utils";
import { getShortLinkViaEdge, getWorkspaceViaEdge } from "@/lib/planetscale";
import { getDomainViaEdge } from "@/lib/planetscale/get-domain-via-edge";
import { getQRAsSVG } from "@/lib/qr/api";
+import { DEFAULT_BGCOLOR } from "@/lib/qr/constants";
import { QRCodeSVG } from "@/lib/qr/utils";
import { getQRCodeQuerySchema } from "@/lib/zod/schemas/qr";
import { DUB_QR_LOGO, getSearchParams, isDubDomain } from "@dub/utils";
@@ -41,7 +42,6 @@ export async function GET(req: NextRequest) {
size,
level,
fgColor,
- bgColor,
margin,
...(qrCodeLogo
? {
@@ -56,18 +56,28 @@ export async function GET(req: NextRequest) {
};
if (format === "svg") {
- const svgString = await getQRAsSVG(qrProps);
+ const svgString = await getQRAsSVG({
+ ...qrProps,
+ ...(bgColor ? { bgColor } : {}),
+ });
const headers = new Headers(CORS_HEADERS);
headers.set("Content-Type", "image/svg+xml");
headers.set("Content-Disposition", "inline; filename=qr.svg");
return new Response(svgString, { headers });
}
- return new ImageResponse(QRCodeSVG({ ...qrProps, isOGContext: true }), {
- width: size,
- height: size,
- headers: CORS_HEADERS,
- });
+ return new ImageResponse(
+ QRCodeSVG({
+ ...qrProps,
+ bgColor: bgColor ?? DEFAULT_BGCOLOR,
+ isOGContext: true,
+ }),
+ {
+ width: size,
+ height: size,
+ headers: CORS_HEADERS,
+ },
+ );
} catch (error) {
return handleAndReturnErrorResponse(error, CORS_HEADERS);
}
diff --git a/apps/web/lib/qr/api.tsx b/apps/web/lib/qr/api.tsx
index 9e3c81cf18..e43468f36b 100644
--- a/apps/web/lib/qr/api.tsx
+++ b/apps/web/lib/qr/api.tsx
@@ -14,6 +14,7 @@ export async function getQRAsSVG(props: QRPropsSVG): Promise<string> {
value,
size = DEFAULT_SIZE,
level = DEFAULT_LEVEL,
+ bgColor,
fgColor = DEFAULT_FGCOLOR,
margin = DEFAULT_MARGIN,
imageSettings,
@@ -67,12 +68,16 @@ export async function getQRAsSVG(props: QRPropsSVG): Promise<string> {
}
// Drawing strategy: instead of a rect per module, we're going to create a
- // single path for the dark modules. Background is omitted so SVGs are
- // transparent (bgColor applies to PNG only).
+ // single path for the dark modules. Background is only painted when bgColor
+ // is set — otherwise the SVG stays transparent.
const fgPath = generatePath(cells, margin);
+ const bgPath = bgColor
+ ? `<path fill="${bgColor}" d="M0,0 h${numCells}v${numCells}H0z" shape-rendering="crispEdges"></path>`
+ : "";
return [
`<svg xmlns="http://www.w3.org/2000/svg" height="${size}" width="${size}" viewBox="0 0 ${numCells} ${numCells}">`,
+ bgPath,
`<path fill="${fgColor}" d="${fgPath}" shape-rendering="crispEdges"></path>`,
image,
"</svg>",
diff --git a/apps/web/lib/zod/schemas/qr.ts b/apps/web/lib/zod/schemas/qr.ts
index 04426845e5..7f21827cd1 100644
--- a/apps/web/lib/zod/schemas/qr.ts
+++ b/apps/web/lib/zod/schemas/qr.ts
@@ -1,9 +1,4 @@
-import {
- DEFAULT_BGCOLOR,
- DEFAULT_FGCOLOR,
- DEFAULT_MARGIN,
- QR_LEVELS,
-} from "@/lib/qr/constants";
+import { DEFAULT_FGCOLOR, DEFAULT_MARGIN, QR_LEVELS } from "@/lib/qr/constants";
import * as z from "zod/v4";
import { booleanQuerySchema } from "./misc";
import { parseUrlSchema } from "./utils";
@@ -40,9 +35,8 @@ export const getQRCodeQuerySchema = z.object({
bgColor: z
.string()
.optional()
- .default(DEFAULT_BGCOLOR)
.describe(
- "The background color of the QR code in hex format. Defaults to `#ffffff` if not provided. Only applies to `png` format; `svg` responses have a transparent background.",
+ "The background color of the QR code in hex format. Defaults to `#ffffff` for `png`. When omitted for `svg`, the background is transparent.",
),
hideLogo: booleanQuerySchema
.optional()