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.

improved UX

Steven Tey3ab099a960d4
diff --git a/lib/utils.ts b/lib/utils.ts
index 16182fe531..fe24c4a6e6 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -175,16 +175,6 @@ export const getApexDomain = (url: string) => {
return domain;
};
-export const getDomainWithoutWWW = (url: string) => {
- let hostname;
- try {
- hostname = new URL(url).hostname;
- } catch (e) {
- return "";
- }
- return hostname.replace(/^www\./, "");
-};
-
export const getParamsFromURL = (url: string) => {
if (!url) return {};
try {
@@ -248,6 +238,30 @@ export const isValidUrl = (url: string) => {
}
};
+export const getUrlFromString = (str: string) => {
+ if (isValidUrl(str)) return str;
+ try {
+ if (str.includes(".") && !str.includes(" ")) {
+ return new URL(`https://${str}`).toString();
+ }
+ } catch (e) {
+ return null;
+ }
+};
+
+export const getDomainWithoutWWW = (url: string) => {
+ if (isValidUrl(url)) {
+ return new URL(url).hostname.replace(/^www\./, "");
+ }
+ try {
+ if (url.includes(".") && !url.includes(" ")) {
+ return new URL(`https://${url}`).hostname.replace(/^www\./, "");
+ }
+ } catch (e) {
+ return null;
+ }
+};
+
export const getQueryString = (router: NextRouter) => {
const { slug: omit, ...queryWithoutSlug } = router.query as {
slug: string;
diff --git a/pages/api/edge/metatags.ts b/pages/api/edge/metatags.ts
index 542dc8ff4e..0b9f05efcb 100644
--- a/pages/api/edge/metatags.ts
+++ b/pages/api/edge/metatags.ts
@@ -100,10 +100,8 @@ export const getMetaTags = async (url: string, ev: NextFetchEvent) => {
export default async function handler(req: NextRequest, ev: NextFetchEvent) {
if (req.method === "GET") {
- const url = req.nextUrl.searchParams.get("url");
- try {
- new URL(url).hostname;
- } catch (e) {
+ let url = req.nextUrl.searchParams.get("url");
+ if (!isValidUrl(url)) {
return new Response("Invalid URL", { status: 400 });
}
const metatags = await getMetaTags(url, ev);
diff --git a/pages/metatags.tsx b/pages/metatags.tsx
index 5984d2071a..b0542e68a9 100644
--- a/pages/metatags.tsx
+++ b/pages/metatags.tsx
@@ -13,13 +13,12 @@ import { useMemo, useState } from "react";
import Background from "@/components/shared/background";
import Meta from "@/components/layout/meta";
import { useDebounce } from "use-debounce";
-import { getDomainWithoutWWW } from "@/lib/utils";
+import { fetcher, getDomainWithoutWWW, getUrlFromString } from "@/lib/utils";
import useSWR from "swr";
-import { fetcher, isValidUrl } from "@/lib/utils";
export default function Metatags({ tweets }: { tweets: any }) {
const [url, setUrl] = useState("https://github.com/steven-tey/dub");
- const [debouncedUrl] = useDebounce(url, 500);
+ const [debouncedUrl] = useDebounce(getUrlFromString(url), 500);
const hostname = useMemo(() => {
return getDomainWithoutWWW(debouncedUrl);
}, [debouncedUrl]);
@@ -28,13 +27,9 @@ export default function Metatags({ tweets }: { tweets: any }) {
title: string | null;
description: string | null;
image: string | null;
- }>(
- isValidUrl(debouncedUrl) && `/api/edge/metatags?url=${debouncedUrl}`,
- fetcher,
- {
- revalidateOnFocus: false,
- },
- );
+ }>(debouncedUrl && `/api/edge/metatags?url=${debouncedUrl}`, fetcher, {
+ revalidateOnFocus: false,
+ });
const { title, description, image } = data || {};
@@ -79,7 +74,6 @@ export default function Metatags({ tweets }: { tweets: any }) {
name="url"
id="url"
type="url"
- required
className="block w-full rounded-md border-gray-300 text-sm text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500"
placeholder="Enter your URL"
value={url}
@@ -149,7 +143,7 @@ export default function Metatags({ tweets }: { tweets: any }) {
<div className="w-11/12 overflow-scroll scrollbar-hide">
<p className="whitespace-nowrap text-sm font-medium text-gray-600">
https://api.dub.sh/metatags?url=
- <span className="text-amber-600">{url}</span>
+ <span className="text-amber-600">{getUrlFromString(url)}</span>
</p>
</div>
<span className="absolute inset-y-0 top-1 right-1 flex h-8 w-8 items-center justify-center rounded-full bg-black/[0.07] transition-all group-hover:bg-black/10">