File tree 3 files changed +19
-3
lines changed 3 files changed +19
-3
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,12 @@ import process from 'node:process';
5
5
import sirv from 'sirv' ;
6
6
import { fileURLToPath } from 'node:url' ;
7
7
import { parse as polka_url_parser } from '@polka/url' ;
8
- import { getRequest , setResponse , createReadableStream } from '@sveltejs/kit/node' ;
8
+ import {
9
+ getRequest ,
10
+ setResponse ,
11
+ createReadableStream ,
12
+ relative_pathname
13
+ } from '@sveltejs/kit/node' ;
9
14
import { Server } from 'SERVER' ;
10
15
import { manifest , prerendered , base } from 'MANIFEST' ;
11
16
import { env } from 'ENV' ;
@@ -81,8 +86,11 @@ function serve_prerendered() {
81
86
}
82
87
83
88
// remove or add trailing slash as appropriate
84
- let location = pathname . at ( - 1 ) === '/' ? pathname . slice ( 0 , - 1 ) : pathname + '/' ;
85
- if ( prerendered . has ( location ) ) {
89
+ const inverted_trailing_slash =
90
+ pathname . at ( - 1 ) === '/' ? pathname . slice ( 0 , - 1 ) : pathname + '/' ;
91
+ if ( prerendered . has ( inverted_trailing_slash ) ) {
92
+ // ensure preservation of (possibly invisible) path prefixes
93
+ let location = relative_pathname ( pathname , inverted_trailing_slash ) ;
86
94
if ( query ) location += search ;
87
95
res . writeHead ( 308 , { location } ) . end ( ) ;
88
96
} else {
Original file line number Diff line number Diff line change @@ -222,3 +222,5 @@ export async function setResponse(res, response) {
222
222
export function createReadableStream ( file ) {
223
223
return /** @type {ReadableStream } */ ( Readable . toWeb ( createReadStream ( file ) ) ) ;
224
224
}
225
+
226
+ export { relative_pathname } from '../../utils/url.js' ;
Original file line number Diff line number Diff line change @@ -2145,6 +2145,12 @@ declare module '@sveltejs/kit/node' {
2145
2145
* @since 2.4.0
2146
2146
*/
2147
2147
export function createReadableStream ( file : string ) : ReadableStream ;
2148
+ /**
2149
+ * Calculates the relative path between two URL pathnames.
2150
+ * Note that `relative` from `node:path` works with file system paths which are subtly diffent.
2151
+ * For example: it ignores trailing slashes, which are significant in URLs.
2152
+ * */
2153
+ export function relative_pathname ( from : string , to : string ) : string ;
2148
2154
2149
2155
export { } ;
2150
2156
}
You can’t perform that action at this time.
0 commit comments