revops + onboarding

This commit is contained in:
Timo Knuth
2026-04-22 20:00:44 +02:00
parent ce724662d4
commit 7d2724b65d
37 changed files with 5073 additions and 1286 deletions

View File

@@ -1,22 +1,62 @@
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import {
ATTRIBUTION_COOKIE_NAME,
buildAttributionSnapshot,
serializeAttributionCookie,
} from '@/lib/revops';
const isProduction = process.env.NODE_ENV === 'production';
function attachAttributionCookie(req: NextRequest, response: NextResponse) {
if (req.cookies.get(ATTRIBUTION_COOKIE_NAME)?.value) {
return response;
}
const path = req.nextUrl.pathname;
if (path.startsWith('/api/') || path.startsWith('/_next') || path.startsWith('/r/') || path.includes('.')) {
return response;
}
const snapshot = buildAttributionSnapshot({
utmSource: req.nextUrl.searchParams.get('utm_source'),
utmMedium: req.nextUrl.searchParams.get('utm_medium'),
utmCampaign: req.nextUrl.searchParams.get('utm_campaign'),
utmContent: req.nextUrl.searchParams.get('utm_content'),
utmTerm: req.nextUrl.searchParams.get('utm_term'),
referrer: req.headers.get('referer'),
landingPath: path,
firstSeenAt: new Date(),
});
response.cookies.set(ATTRIBUTION_COOKIE_NAME, serializeAttributionCookie(snapshot), {
httpOnly: false,
secure: isProduction,
sameSite: 'lax',
path: '/',
maxAge: 60 * 60 * 24 * 90,
});
return response;
}
export function middleware(req: NextRequest) {
const path = req.nextUrl.pathname;
// 301 Redirects for /guide -> /learn to avoid duplicate content and consolidate authority
if (path === '/guide/tracking-analytics') {
return NextResponse.redirect(new URL('/learn/tracking', req.url), 301);
}
if (path === '/guide/bulk-qr-code-generation') {
return NextResponse.redirect(new URL('/learn/developer', req.url), 301);
}
if (path === '/guide/qr-code-best-practices') {
return NextResponse.redirect(new URL('/learn/basics', req.url), 301);
}
if (path === '/create-qr') {
return NextResponse.redirect(new URL('/dynamic-qr-code-generator', req.url), 301);
}
// 301 Redirects for /guide -> /learn to avoid duplicate content and consolidate authority
if (path === '/guide/tracking-analytics') {
return attachAttributionCookie(req, NextResponse.redirect(new URL('/learn/tracking', req.url), 301));
}
if (path === '/guide/bulk-qr-code-generation') {
return attachAttributionCookie(req, NextResponse.redirect(new URL('/learn/developer', req.url), 301));
}
if (path === '/guide/qr-code-best-practices') {
return attachAttributionCookie(req, NextResponse.redirect(new URL('/learn/basics', req.url), 301));
}
if (path === '/create-qr') {
return attachAttributionCookie(req, NextResponse.redirect(new URL('/dynamic-qr-code-generator', req.url), 301));
}
// Public routes that don't require authentication
const publicPaths = [
@@ -68,38 +108,40 @@ export function middleware(req: NextRequest) {
// Check if path is public
const isPublicPath = publicPaths.some(p => path === p || path.startsWith(p + '/'));
// Allow API routes
if (path.startsWith('/api/')) {
return NextResponse.next();
}
// Allow API routes
if (path.startsWith('/api/')) {
return attachAttributionCookie(req, NextResponse.next());
}
// Allow redirect routes (QR code redirects)
if (path.startsWith('/r/')) {
return NextResponse.next();
}
if (path.startsWith('/r/')) {
return attachAttributionCookie(req, NextResponse.next());
}
// Allow static files
if (path.includes('.') || path.startsWith('/_next')) {
return NextResponse.next();
}
if (path.includes('.') || path.startsWith('/_next')) {
return attachAttributionCookie(req, NextResponse.next());
}
// Allow public paths
if (isPublicPath) {
return NextResponse.next();
}
if (isPublicPath) {
return attachAttributionCookie(req, NextResponse.next());
}
// For protected routes, check for userId cookie
const userId = req.cookies.get('userId')?.value;
if (!userId) {
// Not authenticated - redirect to signup
const signupUrl = new URL('/signup', req.url);
return NextResponse.redirect(signupUrl);
}
// Authenticated - allow access
return NextResponse.next();
}
if (!userId) {
// Not authenticated - redirect to signup
const signupUrl = new URL('/signup', req.url);
const redirectTarget = `${path}${req.nextUrl.search}`;
signupUrl.searchParams.set('redirect', redirectTarget);
return attachAttributionCookie(req, NextResponse.redirect(signupUrl));
}
// Authenticated - allow access
return attachAttributionCookie(req, NextResponse.next());
}
export const config = {
matcher: [