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.

Added cache control headers

Steven Tey5fdf839d9ee4
diff --git a/README.md b/README.md
index 60416b81b7..320d1a2cc6 100644
--- a/README.md
+++ b/README.md
@@ -6,7 +6,7 @@
</p>
<p align="center">
- An open-source link shortener built with Vercel Edge Functions and Upstash.
+ An open-source link shortener built with Vercel Edge Functions and Upstash Redis.
</p>
<p align="center">
@@ -18,7 +18,7 @@
## Introduction
-Dub is an open-source link shortener built with Vercel Edge Functions and Upstash.
+Dub is an open-source link shortener built with Vercel Edge Functions and Upstash Redis.
## Deploy Your Own
@@ -27,7 +27,7 @@ Dub is an open-source link shortener built with Vercel Edge Functions and Upstas
## Contributing
- [Open an issue](https://github.com/steven-tey/dub/issues) if you believe you've encountered a bug.
-- Make a pull request to add new features/make quality-of-life improvements/fix bugs.
+- Make a [pull request](https://github.com/steven-tey/dub/pull) to add new features/make quality-of-life improvements/fix bugs.
## Author
diff --git a/middleware.ts b/middleware.ts
index 34d19d4d7a..b32bc512f4 100644
--- a/middleware.ts
+++ b/middleware.ts
@@ -1,21 +1,15 @@
-import { NextRequest, NextFetchEvent, NextResponse } from "next/server";
-import { redis } from "@/lib/redis";
+import { NextRequest, NextResponse } from "next/server";
export const config = {
matcher: "/([^/.]*)",
};
-export default async function middleware(req: NextRequest, ev: NextFetchEvent) {
+export default async function middleware(req: NextRequest) {
const url = req.nextUrl.pathname;
const key = url.split("/")[1];
if (key.length === 0) {
+ // for root (/), just return as is
return NextResponse.next();
- } // skip root
- const target = await redis.hget<string>("links", key);
- if (target) {
- ev.waitUntil(redis.hincrby("stats", key, 1)); // increment click count
- return NextResponse.redirect(target);
- } else {
- return NextResponse.redirect("/");
}
+ return NextResponse.rewrite(new URL(`/api/links/${key}`, req.url));
}
diff --git a/pages/_app.tsx b/pages/_app.tsx
index 5a3c962679..786a1ab966 100644
--- a/pages/_app.tsx
+++ b/pages/_app.tsx
@@ -9,7 +9,7 @@ function MyApp({ Component, pageProps }: AppProps) {
<title>Dub - Open-Source Link Shortener</title>
<meta
name="description"
- content="An open-source link shortener built with Vercel Edge Functions and Upstash."
+ content="An open-source link shortener built with Vercel Edge Functions and Upstash Redis."
/>
<link rel="icon" href="/favicon.ico" />
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
@@ -29,7 +29,7 @@ function MyApp({ Component, pageProps }: AppProps) {
<meta name="twitter:title" content="Dub - Open-Source Link Shortener" />
<meta
name="twitter:description"
- content="An open-source link shortener built with Vercel Edge Functions and Upstash."
+ content="An open-source link shortener built with Vercel Edge Functions and Upstash Redis."
/>
<meta name="twitter:image" content="https://dub.sh/thumbnail.png" />
</Head>
diff --git a/pages/api/links/[key]/clicks.ts b/pages/api/links/[key]/clicks.ts
index 3c97855a3b..527e8b623b 100644
--- a/pages/api/links/[key]/clicks.ts
+++ b/pages/api/links/[key]/clicks.ts
@@ -1,6 +1,5 @@
import type { NextRequest, NextResponse } from "next/server";
import { redis } from "@/lib/redis";
-import { customAlphabet } from "nanoid";
export const config = {
runtime: "experimental-edge",
diff --git a/pages/api/links/[key]/index.ts b/pages/api/links/[key]/index.ts
new file mode 100644
index 0000000000..a836014f97
--- /dev/null
+++ b/pages/api/links/[key]/index.ts
@@ -0,0 +1,31 @@
+import { NextFetchEvent, NextRequest, NextResponse } from "next/server";
+import { redis } from "@/lib/redis";
+
+export const config = {
+ runtime: "experimental-edge",
+};
+
+export default async function handler(
+ req: NextRequest,
+ ev: NextFetchEvent,
+ res: NextResponse
+) {
+ if (req.method === "GET") {
+ const url = req.nextUrl.pathname;
+ const key = url.split("/")[1]; // pathname is `/[key]` because we rewrote with middleware
+ const target = await redis.hget<string>("links", key); // get the target url from redis
+ if (target) {
+ ev.waitUntil(redis.hincrby("stats", key, 1)); // increment click count
+ return NextResponse.redirect(target, {
+ headers: {
+ "cache-control":
+ "public, s-maxage=31536000, stale-while-revalidate=31536000",
+ }, // cache for 1 year only if there is a valid target for key
+ });
+ } else {
+ return NextResponse.redirect("/");
+ }
+ } else {
+ return new Response(`Method ${req.method} Not Allowed`, { status: 405 });
+ }
+}
diff --git a/pages/index.tsx b/pages/index.tsx
index ad1c8d44de..ae86708d51 100644
--- a/pages/index.tsx
+++ b/pages/index.tsx
@@ -45,19 +45,17 @@ export default function Home() {
setUrl((e.target as HTMLInputElement).value);
}}
required
- className="shadow-sm focus:outline-none focus:ring-0 border focus:border-black block w-full p-2 text-sm border-gray-300 rounded-md pl-3 pr-12"
+ className="peer shadow-sm focus:outline-none focus:ring-0 border focus:border-black block w-full p-2 text-sm border-gray-200 rounded-md pl-3 pr-12"
/>
- <div className="absolute inset-y-0 right-0 flex py-1.5 pr-1.5">
- <button
- type="submit"
- disabled={saving}
- className={`${
- saving ? "text-gray-200 cursor-not-allowed" : ""
- } inline-flex items-center border border-gray-200 rounded px-2 text-sm font-sans font-medium text-gray-400 hover:text-gray-700`}
- >
- {saving ? <LoadingDots color="#e5e7eb" /> : <p>↵</p>}
- </button>
- </div>
+ <button
+ type="submit"
+ disabled={saving}
+ className={`${
+ saving ? "cursor-not-allowed" : ""
+ } absolute inset-y-0 right-0 flex my-1.5 mr-1.5 items-center border border-gray-200 hover:border-gray-700 peer-focus:border-gray-700 rounded px-2 text-sm font-sans font-medium text-gray-400 hover:text-gray-700 peer-focus:text-gray-700`}
+ >
+ {saving ? <LoadingDots color="#e5e7eb" /> : <p>↵</p>}
+ </button>
</div>
</form>