@@ -20,7 +20,6 @@ import { renderEndpoint } from '../runtime/server/endpoint.js';
20
20
import { renderPage } from '../runtime/server/index.js' ;
21
21
import {
22
22
ASTRO_VERSION ,
23
- DEFAULT_404_COMPONENT ,
24
23
REROUTE_DIRECTIVE_HEADER ,
25
24
ROUTE_TYPE_HEADER ,
26
25
clientAddressSymbol ,
@@ -46,7 +45,7 @@ export class RenderContext {
46
45
readonly pipeline : Pipeline ,
47
46
public locals : App . Locals ,
48
47
readonly middleware : MiddlewareHandler ,
49
- readonly pathname : string ,
48
+ public pathname : string ,
50
49
public request : Request ,
51
50
public routeData : RouteData ,
52
51
public status : number ,
@@ -103,16 +102,17 @@ export class RenderContext {
103
102
componentInstance : ComponentInstance | undefined ,
104
103
slots : Record < string , any > = { }
105
104
) : Promise < Response > {
106
- const { cookies, middleware, pathname, pipeline } = this ;
107
- const { logger, routeCache, serverLike, streaming } = pipeline ;
105
+ const { cookies, middleware, pipeline } = this ;
106
+ const { logger, serverLike, streaming } = pipeline ;
107
+
108
108
const props =
109
109
Object . keys ( this . props ) . length > 0
110
110
? this . props
111
111
: await getProps ( {
112
112
mod : componentInstance ,
113
113
routeData : this . routeData ,
114
- routeCache,
115
- pathname,
114
+ routeCache : this . pipeline . routeCache ,
115
+ pathname : this . pathname ,
116
116
logger,
117
117
serverLike,
118
118
} ) ;
@@ -222,7 +222,7 @@ export class RenderContext {
222
222
223
223
const rewrite = async ( reroutePayload : RewritePayload ) => {
224
224
pipeline . logger . debug ( 'router' , 'Called rewriting to:' , reroutePayload ) ;
225
- const [ routeData , component ] = await pipeline . tryRewrite (
225
+ const [ routeData , component , newURL ] = await pipeline . tryRewrite (
226
226
reroutePayload ,
227
227
this . request ,
228
228
this . originalRoute
@@ -231,15 +231,13 @@ export class RenderContext {
231
231
if ( reroutePayload instanceof Request ) {
232
232
this . request = reroutePayload ;
233
233
} else {
234
- this . request = this . #copyRequest(
235
- new URL ( routeData . pathname ?? routeData . route , this . url . origin ) ,
236
- this . request
237
- ) ;
234
+ this . request = this . #copyRequest( newURL , this . request ) ;
238
235
}
239
- this . url = new URL ( this . request . url ) ;
236
+ this . url = newURL ;
240
237
this . cookies = new AstroCookies ( this . request ) ;
241
- this . params = getParams ( routeData , url . toString ( ) ) ;
238
+ this . params = getParams ( routeData , this . url . pathname ) ;
242
239
this . isRewriting = true ;
240
+ this . pathname = this . url . pathname ;
243
241
return await this . render ( component ) ;
244
242
} ;
245
243
@@ -359,11 +357,17 @@ export class RenderContext {
359
357
props : Record < string , any > ,
360
358
slotValues : Record < string , any > | null
361
359
) : AstroGlobal {
360
+ let astroPagePartial ;
361
+ // During rewriting, we must recompute the Astro global, because we need to purge the previous params/props/etc.
362
+ if ( this . isRewriting ) {
363
+ astroPagePartial = this . #astroPagePartial = this . createAstroPagePartial ( result , astroStaticPartial ) ;
364
+ } else {
362
365
// Create page partial with static partial so they can be cached together.
363
- const astroPagePartial = ( this . #astroPagePartial ??= this . createAstroPagePartial (
364
- result ,
365
- astroStaticPartial
366
- ) ) ;
366
+ astroPagePartial = this . #astroPagePartial ??= this . createAstroPagePartial (
367
+ result ,
368
+ astroStaticPartial
369
+ ) ;
370
+ }
367
371
// Create component-level partials. `Astro.self` is added by the compiler.
368
372
const astroComponentPartial = { props, self : null } ;
369
373
@@ -410,7 +414,7 @@ export class RenderContext {
410
414
411
415
const rewrite = async ( reroutePayload : RewritePayload ) => {
412
416
pipeline . logger . debug ( 'router' , 'Calling rewrite: ' , reroutePayload ) ;
413
- const [ routeData , component ] = await pipeline . tryRewrite (
417
+ const [ routeData , component , newURL ] = await pipeline . tryRewrite (
414
418
reroutePayload ,
415
419
this . request ,
416
420
this . originalRoute
@@ -419,14 +423,12 @@ export class RenderContext {
419
423
if ( reroutePayload instanceof Request ) {
420
424
this . request = reroutePayload ;
421
425
} else {
422
- this . request = this . #copyRequest(
423
- new URL ( routeData . pathname ?? routeData . route , this . url . origin ) ,
424
- this . request
425
- ) ;
426
+ this . request = this . #copyRequest( newURL , this . request ) ;
426
427
}
427
428
this . url = new URL ( this . request . url ) ;
428
429
this . cookies = new AstroCookies ( this . request ) ;
429
- this . params = getParams ( routeData , url . toString ( ) ) ;
430
+ this . params = getParams ( routeData , this . url . pathname ) ;
431
+ this . pathname = this . url . pathname ;
430
432
this . isRewriting = true ;
431
433
return await this . render ( component ) ;
432
434
} ;
0 commit comments