@@ -13,6 +13,7 @@ import {
1313 isRouteErrorResponse ,
1414 redirect ,
1515 data ,
16+ stripBasename ,
1617} from "../../router/utils" ;
1718import { createRequestInit } from "./data" ;
1819import type { AssetsManifest , EntryContext } from "./entry" ;
@@ -135,12 +136,13 @@ export function getSingleFetchDataStrategy(
135136 manifest : AssetsManifest ,
136137 routeModules : RouteModules ,
137138 ssr : boolean ,
139+ basename : string | undefined ,
138140 getRouter : ( ) => DataRouter
139141) : DataStrategyFunction {
140142 return async ( { request, matches, fetcherKey } ) => {
141143 // Actions are simple and behave the same for navigations and fetchers
142144 if ( request . method !== "GET" ) {
143- return singleFetchActionStrategy ( request , matches ) ;
145+ return singleFetchActionStrategy ( request , matches , basename ) ;
144146 }
145147
146148 if ( ! ssr ) {
@@ -186,7 +188,7 @@ export function getSingleFetchDataStrategy(
186188 // Skip single fetch and just call the loaders in parallel when this is
187189 // a SPA mode navigation
188190 let matchesToLoad = matches . filter ( ( m ) => m . shouldLoad ) ;
189- let url = stripIndexParam ( singleFetchUrl ( request . url ) ) ;
191+ let url = stripIndexParam ( singleFetchUrl ( request . url , basename ) ) ;
190192 let init = await createRequestInit ( request ) ;
191193 let results : Record < string , DataStrategyResult > = { } ;
192194 await Promise . all (
@@ -212,7 +214,7 @@ export function getSingleFetchDataStrategy(
212214
213215 // Fetcher loads are singular calls to one loader
214216 if ( fetcherKey ) {
215- return singleFetchLoaderFetcherStrategy ( request , matches ) ;
217+ return singleFetchLoaderFetcherStrategy ( request , matches , basename ) ;
216218 }
217219
218220 // Navigational loads are more complex...
@@ -222,7 +224,8 @@ export function getSingleFetchDataStrategy(
222224 ssr ,
223225 getRouter ( ) ,
224226 request ,
225- matches
227+ matches ,
228+ basename
226229 ) ;
227230 } ;
228231}
@@ -231,14 +234,15 @@ export function getSingleFetchDataStrategy(
231234// navigations and fetchers)
232235async function singleFetchActionStrategy (
233236 request : Request ,
234- matches : DataStrategyFunctionArgs [ "matches" ]
237+ matches : DataStrategyFunctionArgs [ "matches" ] ,
238+ basename : string | undefined
235239) {
236240 let actionMatch = matches . find ( ( m ) => m . shouldLoad ) ;
237241 invariant ( actionMatch , "No action match found" ) ;
238242 let actionStatus : number | undefined = undefined ;
239243 let result = await actionMatch . resolve ( async ( handler ) => {
240244 let result = await handler ( async ( ) => {
241- let url = singleFetchUrl ( request . url ) ;
245+ let url = singleFetchUrl ( request . url , basename ) ;
242246 let init = await createRequestInit ( request ) ;
243247 let { data, status } = await fetchAndDecode ( url , init ) ;
244248 actionStatus = status ;
@@ -272,7 +276,8 @@ async function singleFetchLoaderNavigationStrategy(
272276 ssr : boolean ,
273277 router : DataRouter ,
274278 request : Request ,
275- matches : DataStrategyFunctionArgs [ "matches" ]
279+ matches : DataStrategyFunctionArgs [ "matches" ] ,
280+ basename : string | undefined
276281) {
277282 // Track which routes need a server load - in case we need to tack on a
278283 // `_routes` param
@@ -293,7 +298,7 @@ async function singleFetchLoaderNavigationStrategy(
293298 let singleFetchDfd = createDeferred < SingleFetchResults > ( ) ;
294299
295300 // Base URL and RequestInit for calls to the server
296- let url = stripIndexParam ( singleFetchUrl ( request . url ) ) ;
301+ let url = stripIndexParam ( singleFetchUrl ( request . url , basename ) ) ;
297302 let init = await createRequestInit ( request ) ;
298303
299304 // We'll build up this results object as we loop through matches
@@ -418,12 +423,13 @@ async function singleFetchLoaderNavigationStrategy(
418423// Fetcher loader calls are much simpler than navigational loader calls
419424async function singleFetchLoaderFetcherStrategy (
420425 request : Request ,
421- matches : DataStrategyFunctionArgs [ "matches" ]
426+ matches : DataStrategyFunctionArgs [ "matches" ] ,
427+ basename : string | undefined
422428) {
423429 let fetcherMatch = matches . find ( ( m ) => m . shouldLoad ) ;
424430 invariant ( fetcherMatch , "No fetcher match found" ) ;
425431 let result = await fetcherMatch . resolve ( async ( handler ) => {
426- let url = stripIndexParam ( singleFetchUrl ( request . url ) ) ;
432+ let url = stripIndexParam ( singleFetchUrl ( request . url , basename ) ) ;
427433 let init = await createRequestInit ( request ) ;
428434 return fetchSingleLoader ( handler , url , init , fetcherMatch ! . route . id ) ;
429435 } ) ;
@@ -462,7 +468,10 @@ function stripIndexParam(url: URL) {
462468 return url ;
463469}
464470
465- export function singleFetchUrl ( reqUrl : URL | string ) {
471+ export function singleFetchUrl (
472+ reqUrl : URL | string ,
473+ basename : string | undefined
474+ ) {
466475 let url =
467476 typeof reqUrl === "string"
468477 ? new URL (
@@ -477,6 +486,8 @@ export function singleFetchUrl(reqUrl: URL | string) {
477486
478487 if ( url . pathname === "/" ) {
479488 url . pathname = "_root.data" ;
489+ } else if ( basename && stripBasename ( url . pathname , basename ) === "/" ) {
490+ url . pathname = `${ basename . replace ( / \/ $ / , "" ) } /_root.data` ;
480491 } else {
481492 url . pathname = `${ url . pathname . replace ( / \/ $ / , "" ) } .data` ;
482493 }
0 commit comments