11import type { NextRequest } from 'next/server'
2- import { constructSitemap } from '@/app/sitemap'
32import { ALLOW_SEO_INDEXING } from '@/configs/flags'
4- import { ROUTE_REWRITE_CONFIG } from '@/configs/rewrites'
53import { BASE_URL } from '@/configs/urls'
64import { l , serializeErrorForLog } from '@/core/shared/clients/logger/logger'
75import {
86 getRewriteForPath ,
97 rewriteContentPagesHtml ,
108} from '@/lib/utils/rewrites'
119
12- export const dynamic = 'force-static'
13- export const revalidate = 900
10+ export const dynamic = 'force-dynamic'
1411
1512const REVALIDATE_TIME = 900 // 15 minutes ttl
13+ const CDN_CACHE_CONTROL = `public, s-maxage=${ REVALIDATE_TIME } , stale-while-revalidate=${ REVALIDATE_TIME } `
14+
15+ function getRewriteRequestHeaders ( ) : Headers {
16+ return new Headers ( {
17+ Accept : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' ,
18+ } )
19+ }
20+
21+ function setRewriteCacheHeaders ( headers : Headers ) : void {
22+ headers . delete ( 'set-cookie' )
23+ headers . delete ( 'set-cookie2' )
24+ headers . set ( 'Cache-Control' , 'public, max-age=0, must-revalidate' )
25+ headers . set ( 'CDN-Cache-Control' , CDN_CACHE_CONTROL )
26+ headers . set ( 'Vercel-CDN-Cache-Control' , CDN_CACHE_CONTROL )
27+ }
1628
1729export async function GET ( request : NextRequest ) : Promise < Response > {
1830 const url = new URL ( request . url )
@@ -31,8 +43,6 @@ export async function GET(request: NextRequest): Promise<Response> {
3143 url . pathname = '/'
3244 }
3345
34- const requestHostname = url . hostname
35-
3646 const updateUrlHostname = ( newHostname : string ) => {
3747 url . hostname = newHostname
3848 url . port = ''
@@ -49,15 +59,14 @@ export async function GET(request: NextRequest): Promise<Response> {
4959 }
5060
5161 try {
52- const notFound = url . hostname === requestHostname
53-
54- // if hostname did not change, we want to make sure it does not cache the route based on the build times hostname (127.0.0.1:3000)
62+ const notFound = ! config
5563 const fetchUrl = notFound ? `${ BASE_URL } /not-found` : url . toString ( )
5664
5765 const res = await fetch ( fetchUrl , {
58- headers : new Headers ( request . headers ) ,
66+ headers : notFound
67+ ? new Headers ( request . headers )
68+ : getRewriteRequestHeaders ( ) ,
5969 redirect : 'follow' ,
60- // if the hostname is the same, we don't want to cache the response, since it will not be available in build time
6170 ...( notFound
6271 ? { cache : 'no-store' }
6372 : {
@@ -70,11 +79,16 @@ export async function GET(request: NextRequest): Promise<Response> {
7079 const contentType = res . headers . get ( 'Content-Type' )
7180 const newHeaders = new Headers ( res . headers )
7281
82+ if ( ! notFound ) {
83+ setRewriteCacheHeaders ( newHeaders )
84+ }
85+
7386 if ( contentType ?. startsWith ( 'text/html' ) ) {
7487 let html = await res . text ( )
7588
7689 // remove content-encoding header to ensure proper rendering
7790 newHeaders . delete ( 'content-encoding' )
91+ newHeaders . delete ( 'content-length' )
7892
7993 // rewrite absolute URLs pointing to the rewritten domain to relative paths and with correct SEO tags
8094 if ( config ) {
@@ -98,7 +112,11 @@ export async function GET(request: NextRequest): Promise<Response> {
98112 return modifiedResponse
99113 }
100114
101- return res
115+ return new Response ( res . body , {
116+ status : res . status ,
117+ statusText : res . statusText ,
118+ headers : newHeaders ,
119+ } )
102120 } catch ( error ) {
103121 l . error ( {
104122 key : 'url_rewrite:unexpected_error' ,
@@ -114,44 +132,3 @@ export async function GET(request: NextRequest): Promise<Response> {
114132 )
115133 }
116134}
117-
118- export async function generateStaticParams ( ) {
119- const sitemapEntries = await constructSitemap ( )
120-
121- const slugs = sitemapEntries
122- . filter ( ( entry ) => {
123- const url = new URL ( entry . url )
124- const pathname = url . pathname
125-
126- // check if this path matches any rule in ROUTE_REWRITE_CONFIG
127- for ( const domainConfig of ROUTE_REWRITE_CONFIG ) {
128- const isIndex = pathname === '/' || pathname === ''
129- const matchingRule = domainConfig . rules . find ( ( rule ) => {
130- if ( isIndex && rule . path === '/' ) {
131- return true
132- }
133- if ( pathname === rule . path || pathname . startsWith ( `${ rule . path } /` ) ) {
134- return true
135- }
136- return false
137- } )
138-
139- if ( matchingRule ) {
140- return true
141- }
142- }
143- return false
144- } )
145- . map ( ( entry ) => {
146- // map the filtered entries to slug format
147- const url = new URL ( entry . url )
148- const pathname = url . pathname
149- const pathSegments = pathname
150- . split ( '/' )
151- . filter ( ( segment ) => segment !== '' )
152-
153- return { slug : pathSegments }
154- } )
155-
156- return slugs
157- }
0 commit comments