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.

fix destination url

Steven Teyc3d1ebb06c6b
diff --git a/apps/web/lib/api/links/process-link.ts b/apps/web/lib/api/links/process-link.ts
index f0b9d6eca4..9e4410d315 100644
--- a/apps/web/lib/api/links/process-link.ts
+++ b/apps/web/lib/api/links/process-link.ts
@@ -66,15 +66,7 @@ export async function processLink<T extends Record<string, any>>({
let expiresAt: string | Date | null | undefined = payload.expiresAt;
const tagIds = combineTagIds(payload);
- // url checks
- if (!url && key !== "_root") {
- return {
- link: payload,
- error: "Missing destination url.",
- code: "bad_request",
- };
- }
-
+ // if URL is defined, perform URL checks
if (url) {
url = getUrlFromString(url);
if (!isValidUrl(url)) {
@@ -84,6 +76,13 @@ export async function processLink<T extends Record<string, any>>({
code: "unprocessable_entity",
};
}
+ // only root domain links can have empty desintation URL
+ } else if (key !== "_root") {
+ return {
+ link: payload,
+ error: "Missing destination url.",
+ code: "bad_request",
+ };
}
// free plan restrictions (after Jan 19, 2024)
diff --git a/apps/web/lib/zod/schemas/domains.ts b/apps/web/lib/zod/schemas/domains.ts
index 56f3b25723..09f8d36cba 100644
--- a/apps/web/lib/zod/schemas/domains.ts
+++ b/apps/web/lib/zod/schemas/domains.ts
@@ -1,5 +1,4 @@
import z from "@/lib/zod";
-import { getUrlFromString } from "@dub/utils";
import { parseUrlSchema } from "./utils";
export const DomainSchema = z.object({
@@ -22,7 +21,6 @@ export const DomainSchema = z.object({
.default(false),
placeholder: z
.string()
- .transform((v) => getUrlFromString(v))
.describe(
"Provide context to your teammates in the link creation modal by showing them an example of a link to be shortened.",
)
@@ -30,8 +28,7 @@ export const DomainSchema = z.object({
.openapi({ example: "https://dub.co/help/article/what-is-dub" }),
expiredUrl: z
.string()
- .transform((v) => getUrlFromString(v))
- .nullish()
+ .nullable()
.describe(
"The URL to redirect to when a link under this domain has expired.",
)
diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.ts
index b48c4788fc..353c02888d 100644
--- a/apps/web/lib/zod/schemas/links.ts
+++ b/apps/web/lib/zod/schemas/links.ts
@@ -2,7 +2,7 @@ import z from "@/lib/zod";
import { COUNTRY_CODES, validDomainRegex } from "@dub/utils";
import { booleanQuerySchema } from "./misc";
import { TagSchema } from "./tags";
-import { parseUrlSchema } from "./utils";
+import { parseUrlSchema, parseUrlSchemaAllowEmpty } from "./utils";
export const getUrlQuerySchema = z.object({
url: parseUrlSchema,
@@ -108,7 +108,7 @@ export const domainKeySchema = z.object({
});
export const createLinkBodySchema = z.object({
- url: parseUrlSchema
+ url: parseUrlSchemaAllowEmpty
.describe("The destination URL of the short link.")
.openapi({
example: "https://google/com",
diff --git a/apps/web/lib/zod/schemas/utils.ts b/apps/web/lib/zod/schemas/utils.ts
index 8f6f2d24e7..ff82f56751 100644
--- a/apps/web/lib/zod/schemas/utils.ts
+++ b/apps/web/lib/zod/schemas/utils.ts
@@ -4,7 +4,11 @@ import { getUrlFromString, isValidUrl, parseDateTime } from "@dub/utils";
export const parseUrlSchema = z
.string()
.transform((v) => getUrlFromString(v))
- .refine((v) => isValidUrl(v) || v === "", { message: "Invalid URL" });
+ .refine((v) => isValidUrl(v), { message: "Invalid URL" });
+
+export const parseUrlSchemaAllowEmpty = z
+ .string()
+ .transform((v) => getUrlFromString(v));
export const parseDateSchema = z
.string()
diff --git a/apps/web/ui/modals/add-edit-link-modal/cloaking-section.tsx b/apps/web/ui/modals/add-edit-link-modal/cloaking-section.tsx
index e7487423c5..251e0986b3 100644
--- a/apps/web/ui/modals/add-edit-link-modal/cloaking-section.tsx
+++ b/apps/web/ui/modals/add-edit-link-modal/cloaking-section.tsx
@@ -10,6 +10,7 @@ import {
import { motion } from "framer-motion";
import { Dispatch, SetStateAction, useEffect, useState } from "react";
import useSWR from "swr";
+import { useDebounce } from "use-debounce";
export default function CloakingSection({
data,
@@ -71,8 +72,9 @@ export default function CloakingSection({
}
function IframeIndicator({ url, domain }: { url: string; domain: string }) {
+ const [debouncedUrl] = useDebounce(url, 500);
const { data, isLoading } = useSWR<{ iframeable: boolean }>(
- `/api/links/iframeable?url=${url}&domain=${domain}`,
+ `/api/links/iframeable?url=${debouncedUrl}&domain=${domain}`,
fetcher,
);
diff --git a/apps/web/ui/modals/add-edit-link-modal/index.tsx b/apps/web/ui/modals/add-edit-link-modal/index.tsx
index b29d3fdb96..e2d9e22855 100644
--- a/apps/web/ui/modals/add-edit-link-modal/index.tsx
+++ b/apps/web/ui/modals/add-edit-link-modal/index.tsx
@@ -4,11 +4,11 @@ import useDomains from "@/lib/swr/use-domains";
import useWorkspace from "@/lib/swr/use-workspace";
import { LinkWithTagsProps } from "@/lib/types";
import { AlertCircleFill, Lock, Random, X } from "@/ui/shared/icons";
+import { ProBadgeTooltip } from "@/ui/shared/pro-badge-tooltip";
import { UpgradeRequiredToast } from "@/ui/shared/upgrade-required-toast";
import {
Button,
ButtonTooltip,
- InfoTooltip,
LinkLogo,
LinkedIn,
LoadingCircle,
@@ -21,6 +21,7 @@ import {
useMediaQuery,
useRouterStuff,
} from "@dub/ui";
+import { InfoTooltip } from "@dub/ui/src/tooltip";
import {
DEFAULT_LINK_PROPS,
cn,
@@ -440,7 +441,7 @@ function AddEditLinkModal({
}
const message = error.message.toLowerCase();
- if (message.includes("key") || message.includes("domain")) {
+ if (message.includes("key")) {
setKeyError(error.message);
} else if (message.includes("url")) {
setUrlError(error.message);
@@ -462,16 +463,26 @@ function AddEditLinkModal({
>
Destination URL
</label>
- {key === "_root" && (
- <InfoTooltip
+ {key === "_root" ? (
+ <ProBadgeTooltip
content={
<SimpleTooltipContent
- title="The page your users will get redirected to when they visit your domain."
+ title="The URL your users will get redirected to when they visit your root domain link."
cta="Learn more."
href="https://dub.co/help/article/how-to-redirect-root-domain"
/>
}
/>
+ ) : (
+ <InfoTooltip
+ content={
+ <SimpleTooltipContent
+ title="The URL your users will get redirected to when they visit your short link."
+ cta="Learn more."
+ href="https://dub.co/help/article/how-to-create-link"
+ />
+ }
+ />
)}
</div>
{urlError ? (
@@ -484,51 +495,39 @@ function AddEditLinkModal({
</div>
) : null}
</div>
- {plan === "free" && key === "_root" ? (
- <Tooltip
- content={
- <TooltipContent title="You can't configure a custom landing page on a free plan." />
+ <div className="relative mt-2 flex rounded-md shadow-sm">
+ <input
+ name="url"
+ id={`url-${randomIdx}`}
+ required={key !== "_root"}
+ placeholder={
+ domains?.find(({ slug }) => slug === domain)
+ ?.placeholder ||
+ "https://dub.co/help/article/what-is-dub"
}
- >
- <div className="mt-2 w-full cursor-not-allowed rounded-md border border-gray-300 px-3 py-2 text-left text-sm text-gray-300 sm:max-w-md">
- https://yourdomain.com
+ value={url}
+ autoFocus={!key && !isMobile}
+ autoComplete="off"
+ onChange={(e) => {
+ setUrlError(null);
+ setData({ ...data, url: e.target.value });
+ }}
+ className={`${
+ urlError
+ ? "border-red-300 pr-10 text-red-900 placeholder-red-300 focus:border-red-500 focus:ring-red-500"
+ : "border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:ring-gray-500"
+ } block w-full rounded-md focus:outline-none sm:text-sm`}
+ aria-invalid="true"
+ />
+ {urlError && (
+ <div className="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-3">
+ <AlertCircleFill
+ className="h-5 w-5 text-red-500"
+ aria-hidden="true"
+ />
</div>
- </Tooltip>
- ) : (
- <div className="relative mt-1 flex rounded-md shadow-sm">
- <input
- name="url"
- id={`url-${randomIdx}`}
- required
- placeholder={
- domains?.find(({ slug }) => slug === domain)
- ?.placeholder ||
- "https://dub.co/help/article/what-is-dub"
- }
- value={url}
- autoFocus={!key && !isMobile}
- autoComplete="off"
- onChange={(e) => {
- setUrlError(null);
- setData({ ...data, url: e.target.value });
- }}
- className={`${
- urlError
- ? "border-red-300 pr-10 text-red-900 placeholder-red-300 focus:border-red-500 focus:ring-red-500"
- : "border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:ring-gray-500"
- } block w-full rounded-md focus:outline-none sm:text-sm`}
- aria-invalid="true"
- />
- {urlError && (
- <div className="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-3">
- <AlertCircleFill
- className="h-5 w-5 text-red-500"
- aria-hidden="true"
- />
- </div>
- )}
- </div>
- )}
+ )}
+ </div>
</div>
{key !== "_root" && (
diff --git a/apps/web/ui/modals/provider.tsx b/apps/web/ui/modals/provider.tsx
index e2f1bbef0d..a3bc5fb1c5 100644
--- a/apps/web/ui/modals/provider.tsx
+++ b/apps/web/ui/modals/provider.tsx
@@ -77,7 +77,7 @@ export default function ModalProvider({ children }: { children: ReactNode }) {
? {
duplicateProps: {
...DEFAULT_LINK_PROPS,
- domain: newLinkValues.domain,
+ ...(newLinkValues.domain && { domain: newLinkValues.domain }),
url: newLinkValues.url,
},
}
diff --git a/packages/utils/src/functions/urls.ts b/packages/utils/src/functions/urls.ts
index 88fbbd7d3d..b89ed2c207 100644
--- a/packages/utils/src/functions/urls.ts
+++ b/packages/utils/src/functions/urls.ts
@@ -14,7 +14,7 @@ export const getUrlFromString = (str: string) => {
return new URL(`https://${str}`).toString();
}
} catch (_) {}
- return "";
+ return str;
};
export const getSearchParams = (url: string) => {