@@ -5,7 +5,12 @@ import { render_page } from './page/index.js';
55import { render_response } from './page/render.js' ;
66import { respond_with_error } from './page/respond_with_error.js' ;
77import { is_form_content_type } from '../../utils/http.js' ;
8- import { handle_fatal_error , method_not_allowed , redirect_response } from './utils.js' ;
8+ import {
9+ handle_fatal_error ,
10+ has_prerendered_path ,
11+ method_not_allowed ,
12+ redirect_response
13+ } from './utils.js' ;
914import { decode_pathname , decode_params , disable_search , normalize_path } from '../../utils/url.js' ;
1015import { exec } from '../../utils/routing.js' ;
1116import { redirect_json_response , render_data } from './data/index.js' ;
@@ -21,6 +26,8 @@ import { get_public_env } from './env_module.js';
2126import { resolve_route } from './page/server_routing.js' ;
2227import { validateHeaders } from './validate-headers.js' ;
2328import {
29+ add_data_suffix ,
30+ add_resolution_suffix ,
2431 has_data_suffix ,
2532 has_resolution_suffix ,
2633 strip_data_suffix ,
@@ -191,6 +198,34 @@ export async function respond(request, options, manifest, state) {
191198 return text ( 'Malformed URI' , { status : 400 } ) ;
192199 }
193200
201+ if (
202+ resolved_path !== url . pathname &&
203+ ! state . prerendering ?. fallback &&
204+ has_prerendered_path ( manifest , resolved_path )
205+ ) {
206+ const url = new URL ( request . url ) ;
207+ url . pathname = is_data_request
208+ ? add_data_suffix ( resolved_path )
209+ : is_route_resolution_request
210+ ? add_resolution_suffix ( resolved_path )
211+ : resolved_path ;
212+
213+ // `fetch` automatically decodes the body, so we need to delete the related headers to not break the response
214+ // Also see https://github.com/sveltejs/kit/issues/12197 for more info (we should fix this more generally at some point)
215+ const response = await fetch ( url , request ) ;
216+ const headers = new Headers ( response . headers ) ;
217+ if ( headers . has ( 'content-encoding' ) ) {
218+ headers . delete ( 'content-encoding' ) ;
219+ headers . delete ( 'content-length' ) ;
220+ }
221+
222+ return new Response ( response . body , {
223+ headers,
224+ status : response . status ,
225+ statusText : response . statusText
226+ } ) ;
227+ }
228+
194229 /** @type {import('types').SSRRoute | null } */
195230 let route = null ;
196231
0 commit comments