使用 Next.js 16、Supabase 和 Edge Functions 构建高性能链接短缩器
发布: (2026年1月4日 GMT+8 21:13)
2 min read
原文: Dev.to
Source: Dev.to
架构概览
该应用被拆分为两部分:
- 仪表盘(Dashboard): 一个标准的 Next.js App Router 应用(服务器组件),用于管理链接、查看分析数据以及处理身份验证。
- 重定向引擎(Redirect Engine): 一个超轻量的 Edge Route,负责高并发的重定向逻辑。
1. 数据库模式(Supabase)
保持简洁。我们使用 Supabase 提供的 PostgreSQL。核心包含两张表:links 和 clicks。
-- The core links table
create table links (
id uuid default gen_random_uuid() primary key,
code text unique not null, -- The slug (e.g. 'hostgator')
destination_url text not null,
is_active boolean default true,
group_id uuid references groups(id), -- For organizing campaigns
created_at timestamp with time zone default timezone('utc'::text, now())
);
-- The analytics table
create table clicks (
id uuid default gen_random_uuid() primary key,
link_id uuid references links(id),
country text, -- e.g. "US"
city text,
device_type text, -- "mobile", "desktop"
os_name text,
browser_name text,
created_at timestamp with time zone default timezone('utc'::text, now())
-- NOTICE: No IP address column!
);
2. Edge 重定向(有趣的部分)
我们几乎不在 Node.js 中处理重定向,而是将代码运行在 Edge Runtime 上,该运行时在 Vercel 或 Netlify 的全球边缘网络上执行,离用户更近。
// src/app/[code]/route.ts
import { NextRequest, NextResponse } from 'next/server'
import { createClient } from '@/lib/supabase/server'
export const runtime = 'edge' //