@@ -298,6 +298,7 @@ export interface RouterInit {
298
298
* State returned from a server-side query() call
299
299
*/
300
300
export interface StaticHandlerContext {
301
+ basename : Router [ "basename" ] ;
301
302
location : RouterState [ "location" ] ;
302
303
matches : RouterState [ "matches" ] ;
303
304
loaderData : RouterState [ "loaderData" ] ;
@@ -1858,14 +1859,18 @@ const validActionMethods = new Set(["POST", "PUT", "PATCH", "DELETE"]);
1858
1859
const validRequestMethods = new Set ( [ "GET" , "HEAD" , ...validActionMethods ] ) ;
1859
1860
1860
1861
export function unstable_createStaticHandler (
1861
- routes : AgnosticRouteObject [ ]
1862
+ routes : AgnosticRouteObject [ ] ,
1863
+ opts ?: {
1864
+ basename ?: string ;
1865
+ }
1862
1866
) : StaticHandler {
1863
1867
invariant (
1864
1868
routes . length > 0 ,
1865
1869
"You must provide a non-empty routes array to unstable_createStaticHandler"
1866
1870
) ;
1867
1871
1868
1872
let dataRoutes = convertRoutesToDataRoutes ( routes ) ;
1873
+ let basename = ( opts ? opts . basename : null ) || "/" ;
1869
1874
1870
1875
/**
1871
1876
* The query() method is intended for document requests, in which we want to
@@ -1891,13 +1896,14 @@ export function unstable_createStaticHandler(
1891
1896
) : Promise < StaticHandlerContext | Response > {
1892
1897
let url = new URL ( request . url ) ;
1893
1898
let location = createLocation ( "" , createPath ( url ) , null , "default" ) ;
1894
- let matches = matchRoutes ( dataRoutes , location ) ;
1899
+ let matches = matchRoutes ( dataRoutes , location , basename ) ;
1895
1900
1896
1901
if ( ! validRequestMethods . has ( request . method ) ) {
1897
1902
let error = getInternalRouterError ( 405 , { method : request . method } ) ;
1898
1903
let { matches : methodNotAllowedMatches , route } =
1899
1904
getShortCircuitMatches ( dataRoutes ) ;
1900
1905
return {
1906
+ basename,
1901
1907
location,
1902
1908
matches : methodNotAllowedMatches ,
1903
1909
loaderData : { } ,
@@ -1914,6 +1920,7 @@ export function unstable_createStaticHandler(
1914
1920
let { matches : notFoundMatches , route } =
1915
1921
getShortCircuitMatches ( dataRoutes ) ;
1916
1922
return {
1923
+ basename,
1917
1924
location,
1918
1925
matches : notFoundMatches ,
1919
1926
loaderData : { } ,
@@ -1935,7 +1942,7 @@ export function unstable_createStaticHandler(
1935
1942
// When returning StaticHandlerContext, we patch back in the location here
1936
1943
// since we need it for React Context. But this helps keep our submit and
1937
1944
// loadRouteData operating on a Request instead of a Location
1938
- return { location, ...result } ;
1945
+ return { location, basename , ...result } ;
1939
1946
}
1940
1947
1941
1948
/**
@@ -1961,7 +1968,7 @@ export function unstable_createStaticHandler(
1961
1968
async function queryRoute ( request : Request , routeId ?: string ) : Promise < any > {
1962
1969
let url = new URL ( request . url ) ;
1963
1970
let location = createLocation ( "" , createPath ( url ) , null , "default" ) ;
1964
- let matches = matchRoutes ( dataRoutes , location ) ;
1971
+ let matches = matchRoutes ( dataRoutes , location , basename ) ;
1965
1972
1966
1973
if ( ! validRequestMethods . has ( request . method ) ) {
1967
1974
throw getInternalRouterError ( 405 , { method : request . method } ) ;
@@ -2007,7 +2014,7 @@ export function unstable_createStaticHandler(
2007
2014
location : Location ,
2008
2015
matches : AgnosticDataRouteMatch [ ] ,
2009
2016
routeMatch ?: AgnosticDataRouteMatch
2010
- ) : Promise < Omit < StaticHandlerContext , "location" > | Response > {
2017
+ ) : Promise < Omit < StaticHandlerContext , "location" | "basename" > | Response > {
2011
2018
invariant (
2012
2019
request . signal ,
2013
2020
"query()/queryRoute() requests must contain an AbortController signal"
@@ -2056,7 +2063,7 @@ export function unstable_createStaticHandler(
2056
2063
matches : AgnosticDataRouteMatch [ ] ,
2057
2064
actionMatch : AgnosticDataRouteMatch ,
2058
2065
isRouteRequest : boolean
2059
- ) : Promise < Omit < StaticHandlerContext , "location" > | Response > {
2066
+ ) : Promise < Omit < StaticHandlerContext , "location" | "basename" > | Response > {
2060
2067
let result : DataResult ;
2061
2068
2062
2069
if ( ! actionMatch . route . action ) {
@@ -2078,7 +2085,7 @@ export function unstable_createStaticHandler(
2078
2085
request ,
2079
2086
actionMatch ,
2080
2087
matches ,
2081
- undefined , // Basename not currently supported in static handlers
2088
+ basename ,
2082
2089
true ,
2083
2090
isRouteRequest
2084
2091
) ;
@@ -2168,7 +2175,10 @@ export function unstable_createStaticHandler(
2168
2175
routeMatch ?: AgnosticDataRouteMatch ,
2169
2176
pendingActionError ?: RouteData
2170
2177
) : Promise <
2171
- | Omit < StaticHandlerContext , "location" | "actionData" | "actionHeaders" >
2178
+ | Omit <
2179
+ StaticHandlerContext ,
2180
+ "location" | "basename" | "actionData" | "actionHeaders"
2181
+ >
2172
2182
| Response
2173
2183
> {
2174
2184
let isRouteRequest = routeMatch != null ;
@@ -2208,7 +2218,7 @@ export function unstable_createStaticHandler(
2208
2218
request ,
2209
2219
match ,
2210
2220
matches ,
2211
- undefined , // Basename not currently supported in static handlers
2221
+ basename ,
2212
2222
true ,
2213
2223
isRouteRequest
2214
2224
)
@@ -2519,7 +2529,7 @@ async function callLoaderOrAction(
2519
2529
request : Request ,
2520
2530
match : AgnosticDataRouteMatch ,
2521
2531
matches : AgnosticDataRouteMatch [ ] ,
2522
- basename : string | undefined ,
2532
+ basename = "/" ,
2523
2533
isStaticRequest : boolean = false ,
2524
2534
isRouteRequest : boolean = false
2525
2535
) : Promise < DataResult > {
0 commit comments