mirror of
https://github.com/hanzoai/next.js.git
synced 2026-10-30 14:46:38 +00:00
Based on: - https://github.com/vercel/next.js/pull/88834 - https://github.com/vercel/next.js/pull/88989 - https://github.com/vercel/next.js/pull/88998 --- Adds optimistic routing, enabling the client to predict route structure for URLs that haven't been prefetched yet. When navigating to a URL like /blog/post-2, if we've previously learned the pattern from /blog/post-1, we can predict the route structure without waiting for a server response. The client learns route patterns from server responses and builds a trie indexed by URL parts. When a cache miss occurs, we check if the URL matches a known pattern. If so, we create a synthetic cache entry from the template, allowing immediate rendering of loading boundaries. Static siblings (like /blog/featured alongside /blog/[slug]) are tracked to avoid incorrect predictions — we only predict dynamic routes when we're confident no static sibling matches.
36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import type { NextRequest } from 'next/server'
|
|
|
|
export function proxy(request: NextRequest) {
|
|
const pathname = request.nextUrl.pathname
|
|
|
|
// Rewrite /rewritten/[slug] to /actual/[slug]
|
|
// This creates a mismatch between URL and route structure that should
|
|
// mark the route as having a dynamic rewrite.
|
|
if (pathname.startsWith('/rewritten/')) {
|
|
const slug = pathname.replace('/rewritten/', '')
|
|
const url = request.nextUrl.clone()
|
|
url.pathname = `/actual/${slug}`
|
|
return NextResponse.rewrite(url)
|
|
}
|
|
|
|
// Rewrite /search-rewrite?v=alpha to /rewrite-alpha
|
|
// Rewrite /search-rewrite?v=beta to /rewrite-beta
|
|
// This tests that search param rewrites are correctly detected.
|
|
// The destination pages are fully static, so if we incorrectly use a cached
|
|
// pattern, we'd show the wrong content.
|
|
if (pathname === '/search-rewrite') {
|
|
const v = request.nextUrl.searchParams.get('v')
|
|
const url = request.nextUrl.clone()
|
|
url.pathname = v === 'beta' ? '/rewrite-beta' : '/rewrite-alpha'
|
|
url.searchParams.delete('v')
|
|
return NextResponse.rewrite(url)
|
|
}
|
|
|
|
return NextResponse.next()
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ['/rewritten/:path*', '/search-rewrite'],
|
|
}
|