this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

add middleware, update links

alice c16496cb 0a29fcce

+55 -6
+2 -6
app/page.tsx
··· 4 4 return ( 5 5 <> 6 6 <div className="main"> 7 - <Link href="https://broken-next-link-repro.vercel.app/page1"> 8 - Page 1 9 - </Link> 7 + <Link href="https://page1.paperclip.monster">Page 1</Link> 10 8 <br /> 11 - <Link href="https://broken-next-link-repro.vercel.app/page2"> 12 - Page 2 13 - </Link> 9 + <Link href="https://page2.paperclip.monster">Page 2</Link> 14 10 <br /> 15 11 </div> 16 12 </>
+53
middleware.ts
··· 1 + import { NextResponse } from "next/server"; 2 + import type { NextRequest } from "next/server"; 3 + 4 + export const getValidSubdomain = (host?: string | null) => { 5 + let subdomain: string | null = null; 6 + if (!host && typeof window !== "undefined") { 7 + // On client side, get the host from window 8 + host = window.location.host; 9 + } 10 + 11 + if (host && host.includes(".")) { 12 + const candidate = host.split(".")[0]; 13 + if ( 14 + candidate && 15 + !candidate.includes("localhost") && 16 + !candidate.includes("bsky") 17 + ) { 18 + // Valid candidate 19 + subdomain = candidate; 20 + } 21 + } 22 + return subdomain; 23 + }; 24 + 25 + // RegExp for public files 26 + const PUBLIC_FILE = /\.(.*)$/; // Files 27 + const DID_PATH_URI = "/.well-known/atproto-did"; 28 + 29 + export async function middleware(req: NextRequest) { 30 + // Clone the URL 31 + const url = req.nextUrl.clone(); 32 + 33 + // Skip public files 34 + if ( 35 + (PUBLIC_FILE.test(url.pathname) || url.pathname.includes("_next")) && 36 + !url.pathname.includes(DID_PATH_URI) 37 + ) { 38 + console.log(`>>> Skipping: ${url.pathname}`); 39 + return; 40 + } 41 + 42 + const host = req.headers.get("host"); 43 + const subdomain = getValidSubdomain(host); 44 + if (subdomain) { 45 + // Subdomain available, rewriting 46 + console.log( 47 + `>>> Rewriting: ${url.pathname} to /${subdomain}${url.pathname}` 48 + ); 49 + url.pathname = `/${subdomain}${url.pathname}`; 50 + } 51 + 52 + return NextResponse.rewrite(url); 53 + }