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.

add support for both

Steven Tey2383ebf8a7f1
diff --git a/apps/web/app/api/domains/[domain]/route.ts b/apps/web/app/api/domains/[domain]/route.ts
index 837f8f3a1a..42249b9808 100644
--- a/apps/web/app/api/domains/[domain]/route.ts
+++ b/apps/web/app/api/domains/[domain]/route.ts
@@ -15,7 +15,6 @@ import {
updateDomainBodySchema,
} from "@/lib/zod/schemas/domains";
import { prisma } from "@dub/prisma";
-import { Prisma } from "@dub/prisma/client";
import { combineWords, nanoid, R2_URL } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
@@ -44,7 +43,6 @@ export const PATCH = withWorkspace(
slug: domain,
registeredDomain,
logo: oldLogo,
- deepLink: existingDeepLink,
} = await getDomainOrThrow({
workspace,
domain: params.domain,
@@ -58,7 +56,8 @@ export const PATCH = withWorkspace(
notFoundUrl,
logo,
archived,
- deepLink,
+ assetLinks,
+ appleAppSiteAssociation,
} = updateDomainBodySchema.parse(await parseRequestBody(req));
if (workspace.plan === "free") {
@@ -122,11 +121,9 @@ export const PATCH = withWorkspace(
expiredUrl,
notFoundUrl,
logo: deleteLogo ? null : logoUploaded?.url || oldLogo,
- ...(deepLink && {
- deepLink: {
- ...(existingDeepLink as Prisma.JsonObject),
- ...deepLink,
- },
+ ...(assetLinks && { assetLinks: JSON.parse(assetLinks) }),
+ ...(appleAppSiteAssociation && {
+ appleAppSiteAssociation: JSON.parse(appleAppSiteAssociation),
}),
},
include: {
diff --git a/apps/web/app/api/domains/route.ts b/apps/web/app/api/domains/route.ts
index bcfa27f839..464bf7db66 100644
--- a/apps/web/app/api/domains/route.ts
+++ b/apps/web/app/api/domains/route.ts
@@ -78,8 +78,15 @@ export const GET = withWorkspace(
export const POST = withWorkspace(
async ({ req, workspace, session }) => {
const body = await parseRequestBody(req);
- const { slug, logo, expiredUrl, notFoundUrl, placeholder, deepLink } =
- createDomainBodySchema.parse(body);
+ const {
+ slug,
+ logo,
+ expiredUrl,
+ notFoundUrl,
+ placeholder,
+ assetLinks,
+ appleAppSiteAssociation,
+ } = createDomainBodySchema.parse(body);
const totalDomains = await prisma.domain.count({
where: {
@@ -150,7 +157,11 @@ export const POST = withWorkspace(
expiredUrl,
notFoundUrl,
...(logoUploaded && { logo: logoUploaded.url }),
- ...(deepLink && { deepLink }),
+ ...(assetLinks && { assetLinks: JSON.parse(assetLinks) }),
+ ...(appleAppSiteAssociation && {
+ appleAppSiteAssociation: JSON.parse(appleAppSiteAssociation),
+ }),
+ },
}),
createLink({
diff --git a/apps/web/app/wellknown/[domain]/[file]/route.ts b/apps/web/app/wellknown/[domain]/[file]/route.ts
index d0b6911595..cb2fc7b799 100644
--- a/apps/web/app/wellknown/[domain]/[file]/route.ts
+++ b/apps/web/app/wellknown/[domain]/[file]/route.ts
@@ -1,12 +1,8 @@
-import { prisma } from "@/lib/prisma";
-import { Prisma } from "@prisma/client";
+import { prismaEdge } from "@dub/prisma/edge";
import { NextRequest, NextResponse } from "next/server";
-const supportedFiles = [
- "apple-app-site-association",
- "assetlinks.json",
-] as const;
+import { SupportedFiles, supportedFiles, WellKnownConfig } from "./utils";
-type SupportedFiles = (typeof supportedFiles)[number];
+export const runtime = "edge";
export async function GET(
_req: NextRequest,
@@ -18,80 +14,32 @@ export async function GET(
return NextResponse.json({ error: "File not supported" }, { status: 400 });
}
- const response = await generateWellKnownFile({ domain, file });
-
- return NextResponse.json(response);
-}
-
-async function generateWellKnownFile({
- domain,
- file,
-}: {
- domain: string;
- file: SupportedFiles;
-}) {
- const domainRecord = await prisma.domain.findUniqueOrThrow({
- where: {
- slug: domain,
- // slug: "devvv.com",
- },
- select: {
- deepLink: true,
- },
- });
-
- // Apple App Site Association
- if (file === "apple-app-site-association") {
- const { iosAppId, iosTeamId } = domainRecord.deepLink as Prisma.JsonObject;
-
- if (!iosAppId || !iosTeamId) {
- return [];
- }
-
- const identifier = `${iosTeamId}.${iosAppId}`;
-
- return {
- applinks: {
- apps: [],
- details: [
- {
- appIDs: [identifier],
- components: [
- {
- "/": "*",
- comment:
- "Matches any URL whose path is * and instructs the system to open it as a Universal link",
- },
- {
- "/": "/",
- comment:
- "Matches any URL whose path is / and instructs the system to open it as a Universal link",
- },
- ],
- },
- ],
+ const { appleAppSiteAssociation, assetLinks } =
+ await prismaEdge.domain.findUniqueOrThrow({
+ where: {
+ slug: domain,
},
- };
- }
-
- // Android Asset Links
- if (file === "assetlinks.json") {
- const { androidPackageName, androidSha256CertFingerprints } =
- domainRecord.deepLink as Prisma.JsonObject;
-
- if (!androidPackageName || !androidSha256CertFingerprints) {
- return [];
- }
-
- return [
- {
- relation: ["delegate_permission/common.handle_all_urls"],
- target: {
- namespace: "android_app",
- package_name: androidPackageName,
- sha256_cert_fingerprints: [androidSha256CertFingerprints],
- },
+ select: {
+ appleAppSiteAssociation: true,
+ assetLinks: true,
},
- ];
+ });
+
+ let response: WellKnownConfig[SupportedFiles];
+ switch (file) {
+ case "apple-app-site-association":
+ response =
+ (appleAppSiteAssociation as WellKnownConfig["apple-app-site-association"]) || {
+ applinks: {
+ apps: [],
+ details: [],
+ },
+ };
+ break;
+ case "assetlinks.json":
+ response = (assetLinks as WellKnownConfig["assetlinks.json"]) || [];
+ break;
}
+
+ return NextResponse.json(response);
}
diff --git a/apps/web/app/wellknown/[domain]/[file]/utils.ts b/apps/web/app/wellknown/[domain]/[file]/utils.ts
new file mode 100644
index 0000000000..4bcef22b42
--- /dev/null
+++ b/apps/web/app/wellknown/[domain]/[file]/utils.ts
@@ -0,0 +1,16 @@
+export type WellKnownConfig = {
+ "apple-app-site-association": {
+ applinks: {
+ apps: any[];
+ details: any[];
+ };
+ };
+ "assetlinks.json": any[];
+};
+
+export const supportedFiles = [
+ "apple-app-site-association",
+ "assetlinks.json",
+] as const;
+
+export type SupportedFiles = (typeof supportedFiles)[number];
diff --git a/apps/web/lib/zod/schemas/domains.ts b/apps/web/lib/zod/schemas/domains.ts
index 7f8732ef55..b110c5bdd6 100644
--- a/apps/web/lib/zod/schemas/domains.ts
+++ b/apps/web/lib/zod/schemas/domains.ts
@@ -86,13 +86,6 @@ export const getDefaultDomainsQuerySchema = getDomainsQuerySchema.pick({
search: true,
});
-const deepLink = z.object({
- iosAppId: z.string().nullish().default(null),
- iosTeamId: z.string().nullish().default(null),
- androidPackageName: z.string().nullish().default(null),
- androidSha256CertFingerprints: z.string().nullish().default(null),
-});
-
export const createDomainBodySchema = z.object({
slug: z
.string({ required_error: "slug is required" })
@@ -136,7 +129,18 @@ export const createDomainBodySchema = z.object({
.nullish()
.transform((v) => v || null)
.describe("The logo of the domain."),
- deepLink: deepLink.nullish().describe("Deep link configuration."),
+ assetLinks: z
+ .string()
+ .nullish()
+ .describe(
+ "assetLinks.json configuration file (for deep link support on Android).",
+ ),
+ appleAppSiteAssociation: z
+ .string()
+ .nullish()
+ .describe(
+ "apple-app-site-association configuration file (for deep link support on iOS).",
+ ),
});
export const updateDomainBodySchema = createDomainBodySchema.partial();
diff --git a/packages/prisma/schema/domain.prisma b/packages/prisma/schema/domain.prisma
index dc325914d6..8f86c1fe07 100644
--- a/packages/prisma/schema/domain.prisma
+++ b/packages/prisma/schema/domain.prisma
@@ -1,5 +1,5 @@
model Domain {
- id String @id @default(cuid())
+ id String @id @default(cuid())
slug String @unique
verified Boolean @default(false)
placeholder String?
@@ -9,7 +9,8 @@ model Domain {
archived Boolean @default(false)
lastChecked DateTime @default(now())
logo String?
- deepLink Json?
+ appleAppSiteAssociation Json?
+ assetLinks Json?
links Link[]
project Project? @relation(fields: [projectId], references: [id], onDelete: Cascade)
projectId String?