File tree Expand file tree Collapse file tree 4 files changed +33
-3
lines changed Expand file tree Collapse file tree 4 files changed +33
-3
lines changed Original file line number Diff line number Diff line change 16
16
- akamfoad
17
17
- alany411
18
18
- alberto
19
+ - Aleuck
19
20
- alexandernanberg
20
21
- alexanderson1993
21
22
- alexlbr
Original file line number Diff line number Diff line change
1
+ import { singleFetchUrl } from "../../../lib/dom/ssr/single-fetch" ;
2
+
3
+ describe ( "singleFetchUrl" , ( ) => {
4
+ it ( "should resolve to _root.data when path is /" , ( ) => {
5
+ window . __reactRouterContext = { } as any ;
6
+ const actual = singleFetchUrl ( `${ window . location . origin } /` ) ;
7
+ expect ( actual . toString ( ) ) . toBe ( `${ window . location . origin } /_root.data` ) ;
8
+ } ) ;
9
+
10
+ it ( "should resolve to _root.data when path is equal to basename" , ( ) => {
11
+ window . __reactRouterContext = { } as any ;
12
+ window . __reactRouterContext ! . basename = "/basename/" ;
13
+ const actual = singleFetchUrl ( `${ window . location . origin } /basename/` ) ;
14
+ expect ( actual . toString ( ) ) . toBe (
15
+ `${ window . location . origin } /basename/_root.data`
16
+ ) ;
17
+ } ) ;
18
+
19
+ it ( "should resolve to path.data when path is not to base" , ( ) => {
20
+ window . __reactRouterContext = { } as any ;
21
+ const actual = singleFetchUrl ( `${ window . location . origin } /path/` ) ;
22
+ expect ( actual . toString ( ) ) . toBe ( `${ window . location . origin } /path.data` ) ;
23
+ } ) ;
24
+ } ) ;
Original file line number Diff line number Diff line change @@ -444,6 +444,10 @@ function stripIndexParam(url: URL) {
444
444
}
445
445
446
446
export function singleFetchUrl ( reqUrl : URL | string ) {
447
+ let basename =
448
+ ( typeof window !== "undefined" && window . __reactRouterContext ?. basename ) ||
449
+ "" ;
450
+ basename = `/${ basename } /` . replace ( / \/ + / g, "/" ) ;
447
451
let url =
448
452
typeof reqUrl === "string"
449
453
? new URL (
@@ -456,8 +460,8 @@ export function singleFetchUrl(reqUrl: URL | string) {
456
460
)
457
461
: reqUrl ;
458
462
459
- if ( url . pathname === "/" ) {
460
- url . pathname = " _root.data" ;
463
+ if ( url . pathname === basename ) {
464
+ url . pathname = ` ${ basename } _root.data` ;
461
465
} else {
462
466
url . pathname = `${ url . pathname . replace ( / \/ $ / , "" ) } .data` ;
463
467
}
Original file line number Diff line number Diff line change @@ -107,9 +107,10 @@ export const createRequestHandler: CreateRequestHandlerFunction = (
107
107
}
108
108
109
109
let url = new URL ( request . url ) ;
110
+ let basebane = `/${ _build . basename || '' } /` . replace ( / \/ + / g, '/' )
110
111
let normalizedPath = url . pathname
111
112
. replace ( / \. d a t a $ / , "" )
112
- . replace ( / ^ \/ _ r o o t $ / , "/" ) ;
113
+ . replace ( new RegExp ( `^ ${ basebane } _root$` ) , basebane ) ;
113
114
if ( normalizedPath !== "/" && normalizedPath . endsWith ( "/" ) ) {
114
115
normalizedPath = normalizedPath . slice ( 0 , - 1 ) ;
115
116
}
You can’t perform that action at this time.
0 commit comments