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.

isr fix

Steven Tey3db82e2971fa
diff --git a/app/dub.sh/page.tsx b/app/dub.sh/page.tsx
index ab740173bd..16100e7593 100644
--- a/app/dub.sh/page.tsx
+++ b/app/dub.sh/page.tsx
@@ -1,8 +1,8 @@
+import Hero from "#/ui/home/hero";
import Demo from "#/ui/home/demo";
import Globe from "#/ui/home/globe";
import Stats, { StatsSection } from "#/ui/home/stats";
import Features from "#/ui/home/features";
-import Hero from "#/ui/home/hero";
import Logos from "#/ui/home/logos";
import OSS, { OSSSection } from "#/ui/home/oss";
import Testimonials from "#/ui/home/testimonials";
diff --git a/app/ui/home/demo/action.ts b/app/ui/home/demo-actions/action.ts
similarity index 100%
rename from app/ui/home/demo/action.ts
rename to app/ui/home/demo-actions/action.ts
diff --git a/app/ui/home/demo/index.tsx b/app/ui/home/demo-actions/index.tsx
similarity index 98%
rename from app/ui/home/demo/index.tsx
rename to app/ui/home/demo-actions/index.tsx
index 768abf9896..b1f59a43b6 100644
--- a/app/ui/home/demo/index.tsx
+++ b/app/ui/home/demo-actions/index.tsx
@@ -1,5 +1,7 @@
"use client";
+// we will revisit using Server Actions for our homepage demo when they play nice with ISR
+
import { useRef, useState } from "react";
import { experimental_useFormStatus as useFormStatus } from "react-dom";
import { motion } from "framer-motion";
diff --git a/app/ui/home/demo.tsx b/app/ui/home/demo.tsx
new file mode 100644
index 0000000000..1d564e5940
--- /dev/null
+++ b/app/ui/home/demo.tsx
@@ -0,0 +1,163 @@
+"use client";
+
+import { useRef, useState } from "react";
+import { experimental_useFormStatus as useFormStatus } from "react-dom";
+import { motion } from "framer-motion";
+import LinkCard from "#/ui/home/link-card";
+import PlaceholderCard from "#/ui/home/placeholder-card";
+import { LoadingSpinner } from "#/ui/icons";
+import { CornerDownLeft, Link2 } from "lucide-react";
+import Tooltip, { TooltipContent } from "#/ui/tooltip";
+import { APP_DOMAIN, FRAMER_MOTION_LIST_ITEM_VARIANTS } from "#/lib/constants";
+import useLocalStorage from "#/lib/hooks/use-local-storage";
+import { SimpleLinkProps } from "#/lib/types";
+import { toast } from "sonner";
+
+export default function Demo() {
+ const formRef = useRef<HTMLFormElement>(null);
+ const [hashes, setHashes] = useLocalStorage<SimpleLinkProps[]>("hashes", []);
+ const [submitting, setSubmitting] = useState(false);
+ const [showDefaultLink, setShowDefaultLink] = useState(true);
+
+ return (
+ <div className="mx-auto w-full max-w-md px-2.5 sm:px-0">
+ <form
+ ref={formRef}
+ onSubmit={(e) => {
+ e.preventDefault();
+ setSubmitting(true);
+ fetch("/api/edge/links", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ url: e.currentTarget.url.value,
+ }),
+ }).then(async (res) => {
+ setSubmitting(false);
+ if (res.ok) {
+ const data = await res.json();
+ setHashes([
+ ...hashes,
+ {
+ key: data.key,
+ url: data.url,
+ },
+ ]);
+ toast.success("Successfully shortened link!");
+ formRef.current?.reset();
+ } else {
+ const error = await res.text();
+ toast.error(error);
+ }
+ });
+ }}
+ >
+ {hashes.length >= 3 ? (
+ <Tooltip
+ content={
+ <TooltipContent
+ title="Maximum number of links reached. Swipe to delete existing links or
+ create a free account."
+ cta="Start For Free"
+ href={`${APP_DOMAIN}/register`}
+ />
+ }
+ >
+ <div className="relative flex w-full items-center">
+ <div className="block w-full rounded-md border border-gray-200 bg-white p-2 pl-3 pr-12 text-sm text-gray-400 shadow-lg focus:border-black">
+ Shorten your link
+ </div>
+ <div className="absolute inset-y-0 right-0 my-1.5 mr-1.5 flex w-10 cursor-not-allowed items-center justify-center rounded border border-gray-200 font-sans text-sm font-medium text-gray-400">
+ <CornerDownLeft className="h-4 w-4" />
+ </div>
+ </div>
+ </Tooltip>
+ ) : (
+ <div className="relative flex items-center">
+ <Link2 className="absolute inset-y-0 left-0 my-2 ml-3 w-5 text-gray-400" />
+ <input
+ name="url"
+ type="url"
+ placeholder="Shorten your link"
+ autoComplete="off"
+ required
+ className="peer block w-full rounded-md border border-gray-200 bg-white p-2 pl-10 pr-12 shadow-lg focus:border-black focus:outline-none focus:ring-0 sm:text-sm"
+ />
+ <button
+ type="submit"
+ disabled={submitting}
+ className={`${
+ submitting
+ ? "cursor-not-allowed bg-gray-100"
+ : "hover:border-gray-700 hover:text-gray-700 peer-focus:text-gray-700"
+ } absolute inset-y-0 right-0 my-1.5 mr-1.5 flex w-10 items-center justify-center rounded border border-gray-200 font-sans text-sm font-medium text-gray-400`}
+ >
+ {submitting ? (
+ <LoadingSpinner className="h-4 w-4" />
+ ) : (
+ <CornerDownLeft className="h-4 w-4" />
+ )}
+ </button>
+ </div>
+ )}
+ </form>
+
+ <motion.ul
+ key={hashes.length + 1} // workaround for https://github.com/framer/motion/issues/776, add 1 to account for the demo GH link
+ initial="hidden"
+ animate="show"
+ variants={{
+ hidden: {},
+ show: {
+ transition: {
+ staggerChildren: 0.1,
+ },
+ },
+ }}
+ className="mt-3 grid gap-2"
+ >
+ {showDefaultLink && (
+ <LinkCard
+ key="github"
+ _key="github"
+ url="https://github.com/steven-tey/dub"
+ hashes={hashes}
+ setHashes={setHashes}
+ setShowDefaultLink={setShowDefaultLink}
+ />
+ )}
+ {hashes.map(({ key, url }) => (
+ <LinkCard
+ key={key}
+ _key={key}
+ url={url}
+ hashes={hashes}
+ setHashes={setHashes}
+ />
+ ))}
+ {Array.from({
+ length: showDefaultLink ? 3 - hashes.length : 4 - hashes.length,
+ }).map((_, i) => (
+ <PlaceholderCard key={i} />
+ ))}
+ <motion.li
+ variants={FRAMER_MOTION_LIST_ITEM_VARIANTS}
+ className="rounded-md border border-gray-200 bg-white p-3 text-sm text-gray-500 shadow-lg"
+ >
+ Note: Public links will be automatically-deleted after 30 minutes.{" "}
+ <a
+ href={`${APP_DOMAIN}/register`}
+ target="_blank"
+ rel="noreferrer"
+ className="font-medium text-gray-700 underline transition-all hover:text-black"
+ >
+ Create a free account
+ </a>{" "}
+ to keep your links forever.
+ </motion.li>
+ </motion.ul>
+ </div>
+ );
+}
diff --git a/app/ui/stats/feedback/index.tsx b/app/ui/stats/feedback/index.tsx
index f210ff88f6..775e370e23 100644
--- a/app/ui/stats/feedback/index.tsx
+++ b/app/ui/stats/feedback/index.tsx
@@ -48,7 +48,7 @@ export default function Feedback() {
type="email"
placeholder="[email protected]"
autoComplete="email"
- className="block w-full rounded-md border-gray-300 pr-10 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
+ className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
/>
</div>
<div>
@@ -63,7 +63,7 @@ export default function Feedback() {
id="feedback"
required={true}
rows={6}
- className="block w-full rounded-md border-gray-300 pr-10 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
+ className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
placeholder="What other data would you like to see?"
aria-invalid="true"
/>
diff --git a/app/ui/stats/index.tsx b/app/ui/stats/index.tsx
index eff8280be4..19ddb26859 100644
--- a/app/ui/stats/index.tsx
+++ b/app/ui/stats/index.tsx
@@ -15,13 +15,14 @@ import {
useRouter,
useSearchParams,
} from "next/navigation";
+import { X } from "lucide-react";
+import Toggle from "./toggle";
import Clicks from "./clicks";
import Devices from "./devices";
-import Feedback from "./feedback";
import Locations from "./locations";
import Referer from "./referer";
-import Toggle from "./toggle";
-import { X } from "lucide-react";
+// using the regular feedback component until Server Actions play nice with ISR
+import Feedback from "@/components/stats/feedback";
export const StatsContext = createContext<{
basePath: string;
diff --git a/components/stats/feedback.tsx b/components/stats/feedback.tsx
index 88694c769f..5ae8ca13fb 100644
--- a/components/stats/feedback.tsx
+++ b/components/stats/feedback.tsx
@@ -89,7 +89,7 @@ export default function Feedback() {
autoComplete="email"
onFocus={prewarm}
onChange={(e) => setData({ ...data, email: e.target.value })}
- className="block w-full rounded-md border-gray-300 pr-10 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
+ className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
/>
</div>
<div>
@@ -105,7 +105,7 @@ export default function Feedback() {
required={true}
rows={6}
onKeyDown={handleKeyDown}
- className="block w-full rounded-md border-gray-300 pr-10 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
+ className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
placeholder="What other data would you like to see?"
value={data.feedback}
onFocus={prewarm}
diff --git a/pages/api/edge/links/index.ts b/pages/api/edge/links/index.ts
new file mode 100644
index 0000000000..ea440fd49f
--- /dev/null
+++ b/pages/api/edge/links/index.ts
@@ -0,0 +1,51 @@
+import { NextResponse, type NextRequest } from "next/server";
+import { ipAddress } from "@vercel/edge";
+import { LOCALHOST_IP } from "#/lib/constants";
+import { ratelimit, setRandomKey } from "#/lib/upstash";
+import { isValidUrl } from "#/lib/utils";
+import { isBlacklistedDomain } from "#/lib/edge-config";
+
+export const config = {
+ runtime: "edge",
+};
+
+export default async function handler(req: NextRequest) {
+ if (req.method === "POST") {
+ const { url } = (await req.json()) as { url?: string };
+ if (!url || !isValidUrl(url)) {
+ return new Response("Invalid URL", { status: 400 });
+ }
+
+ const ip = ipAddress(req) || LOCALHOST_IP;
+ const { success } = await ratelimit(5, "1 m").limit(ip);
+ if (!success) {
+ return new Response("Don't DDoS me pls 🥺", { status: 429 });
+ }
+
+ const domainBlacklisted = await isBlacklistedDomain(url);
+ if (domainBlacklisted) {
+ return new Response("Invalid URL", { status: 400 });
+ }
+
+ const { response, key } = await setRandomKey(url);
+ if (response === "OK") {
+ // if key was successfully added
+ return NextResponse.json(
+ {
+ key,
+ url,
+ },
+ { status: 200 },
+ );
+ } else {
+ return NextResponse.json(
+ {
+ error: "Failed to create link",
+ },
+ { status: 500 },
+ );
+ }
+ } else {
+ return new Response(`Method ${req.method} Not Allowed`, { status: 405 });
+ }
+}
diff --git a/pages/api/edge/metatags.ts b/pages/api/edge/metatags.ts
index 20557211f7..7863fc21a0 100644
--- a/pages/api/edge/metatags.ts
+++ b/pages/api/edge/metatags.ts
@@ -12,7 +12,7 @@ export const config = {
export default async function handler(req: NextRequest, ev: NextFetchEvent) {
if (req.method === "GET") {
- let url = req.nextUrl.searchParams.get("url");
+ const url = req.nextUrl.searchParams.get("url");
if (!url || !isValidUrl(url)) {
return new Response("Invalid URL", { status: 400 });
}