@@ -21,7 +21,7 @@ import type {
21
21
Submission ,
22
22
SuccessResult ,
23
23
AgnosticRouteMatch ,
24
- SubmissionFormMethod ,
24
+ MutationFormMethod ,
25
25
} from "./utils" ;
26
26
import {
27
27
DeferredData ,
@@ -521,15 +521,20 @@ interface QueryRouteResponse {
521
521
response : Response ;
522
522
}
523
523
524
- const validActionMethodsArr : SubmissionFormMethod [ ] = [
524
+ const validMutationMethodsArr : MutationFormMethod [ ] = [
525
525
"post" ,
526
526
"put" ,
527
527
"patch" ,
528
528
"delete" ,
529
529
] ;
530
- const validActionMethods = new Set < SubmissionFormMethod > ( validActionMethodsArr ) ;
530
+ const validMutationMethods = new Set < MutationFormMethod > (
531
+ validMutationMethodsArr
532
+ ) ;
531
533
532
- const validRequestMethodsArr : FormMethod [ ] = [ "get" , ...validActionMethodsArr ] ;
534
+ const validRequestMethodsArr : FormMethod [ ] = [
535
+ "get" ,
536
+ ...validMutationMethodsArr ,
537
+ ] ;
533
538
const validRequestMethods = new Set < FormMethod > ( validRequestMethodsArr ) ;
534
539
535
540
const redirectStatusCodes = new Set ( [ 301 , 302 , 303 , 307 , 308 ] ) ;
@@ -811,7 +816,8 @@ export function createRouter(init: RouterInit): Router {
811
816
} ;
812
817
813
818
let historyAction =
814
- ( opts && opts . replace ) === true || submission != null
819
+ ( opts && opts . replace ) === true ||
820
+ ( submission != null && isMutationMethod ( submission . formMethod ) )
815
821
? HistoryAction . Replace
816
822
: HistoryAction . Push ;
817
823
let preventScrollReset =
@@ -935,7 +941,11 @@ export function createRouter(init: RouterInit): Router {
935
941
pendingError = {
936
942
[ findNearestBoundary ( matches ) . route . id ] : opts . pendingError ,
937
943
} ;
938
- } else if ( opts && opts . submission ) {
944
+ } else if (
945
+ opts &&
946
+ opts . submission &&
947
+ isMutationMethod ( opts . submission . formMethod )
948
+ ) {
939
949
// Call action if we received an action submission
940
950
let actionOutput = await handleAction (
941
951
request ,
@@ -1095,6 +1105,7 @@ export function createRouter(init: RouterInit): Router {
1095
1105
formAction : undefined ,
1096
1106
formEncType : undefined ,
1097
1107
formData : undefined ,
1108
+ ...submission ,
1098
1109
} ;
1099
1110
loadingNavigation = navigation ;
1100
1111
}
@@ -1259,15 +1270,15 @@ export function createRouter(init: RouterInit): Router {
1259
1270
let { path, submission } = normalizeNavigateOptions ( href , opts , true ) ;
1260
1271
let match = getTargetMatch ( matches , path ) ;
1261
1272
1262
- if ( submission ) {
1273
+ if ( submission && isMutationMethod ( submission . formMethod ) ) {
1263
1274
handleFetcherAction ( key , routeId , path , match , matches , submission ) ;
1264
1275
return ;
1265
1276
}
1266
1277
1267
1278
// Store off the match so we can call it's shouldRevalidate on subsequent
1268
1279
// revalidations
1269
1280
fetchLoadMatches . set ( key , [ path , match , matches ] ) ;
1270
- handleFetcherLoader ( key , routeId , path , match , matches ) ;
1281
+ handleFetcherLoader ( key , routeId , path , match , matches , submission ) ;
1271
1282
}
1272
1283
1273
1284
// Call the action for the matched fetcher.submit(), and then handle redirects,
@@ -1494,7 +1505,8 @@ export function createRouter(init: RouterInit): Router {
1494
1505
routeId : string ,
1495
1506
path : string ,
1496
1507
match : AgnosticDataRouteMatch ,
1497
- matches : AgnosticDataRouteMatch [ ]
1508
+ matches : AgnosticDataRouteMatch [ ] ,
1509
+ submission ?: Submission
1498
1510
) {
1499
1511
let existingFetcher = state . fetchers . get ( key ) ;
1500
1512
// Put this fetcher into it's loading state
@@ -1504,6 +1516,7 @@ export function createRouter(init: RouterInit): Router {
1504
1516
formAction : undefined ,
1505
1517
formEncType : undefined ,
1506
1518
formData : undefined ,
1519
+ ...submission ,
1507
1520
data : existingFetcher && existingFetcher . data ,
1508
1521
} ;
1509
1522
state . fetchers . set ( key , loadingFetcher ) ;
@@ -1635,12 +1648,12 @@ export function createRouter(init: RouterInit): Router {
1635
1648
let { formMethod, formAction, formEncType, formData } = state . navigation ;
1636
1649
1637
1650
// If this was a 307/308 submission we want to preserve the HTTP method and
1638
- // re-submit the POST/PUT/PATCH/DELETE as a submission navigation to the
1651
+ // re-submit the GET/ POST/PUT/PATCH/DELETE as a submission navigation to the
1639
1652
// redirected location
1640
1653
if (
1641
1654
redirectPreserveMethodStatusCodes . has ( redirect . status ) &&
1642
1655
formMethod &&
1643
- isSubmissionMethod ( formMethod ) &&
1656
+ isMutationMethod ( formMethod ) &&
1644
1657
formEncType &&
1645
1658
formData
1646
1659
) {
@@ -2096,7 +2109,7 @@ export function unstable_createStaticHandler(
2096
2109
) ;
2097
2110
2098
2111
try {
2099
- if ( isSubmissionMethod ( request . method . toLowerCase ( ) ) ) {
2112
+ if ( isMutationMethod ( request . method . toLowerCase ( ) ) ) {
2100
2113
let result = await submit (
2101
2114
request ,
2102
2115
matches ,
@@ -2409,17 +2422,19 @@ function normalizeNavigateOptions(
2409
2422
}
2410
2423
2411
2424
// Create a Submission on non-GET navigations
2412
- if ( opts . formMethod && isSubmissionMethod ( opts . formMethod ) ) {
2413
- return {
2414
- path,
2415
- submission : {
2416
- formMethod : opts . formMethod ,
2417
- formAction : stripHashFromPath ( path ) ,
2418
- formEncType :
2419
- ( opts && opts . formEncType ) || "application/x-www-form-urlencoded" ,
2420
- formData : opts . formData ,
2421
- } ,
2425
+ let submission : Submission | undefined ;
2426
+ if ( opts . formData ) {
2427
+ submission = {
2428
+ formMethod : opts . formMethod || "get" ,
2429
+ formAction : stripHashFromPath ( path ) ,
2430
+ formEncType :
2431
+ ( opts && opts . formEncType ) || "application/x-www-form-urlencoded" ,
2432
+ formData : opts . formData ,
2422
2433
} ;
2434
+
2435
+ if ( isMutationMethod ( submission . formMethod ) ) {
2436
+ return { path, submission } ;
2437
+ }
2423
2438
}
2424
2439
2425
2440
// Flatten submission onto URLSearchParams for GET submissions
@@ -2444,7 +2459,7 @@ function normalizeNavigateOptions(
2444
2459
} ;
2445
2460
}
2446
2461
2447
- return { path : createPath ( parsedPath ) } ;
2462
+ return { path : createPath ( parsedPath ) , submission } ;
2448
2463
}
2449
2464
2450
2465
// Filter out all routes below any caught error as they aren't going to
@@ -2767,7 +2782,7 @@ function createClientSideRequest(
2767
2782
let url = createClientSideURL ( stripHashFromPath ( location ) ) . toString ( ) ;
2768
2783
let init : RequestInit = { signal } ;
2769
2784
2770
- if ( submission ) {
2785
+ if ( submission && isMutationMethod ( submission . formMethod ) ) {
2771
2786
let { formMethod, formEncType, formData } = submission ;
2772
2787
init . method = formMethod . toUpperCase ( ) ;
2773
2788
init . body =
@@ -3115,8 +3130,8 @@ function isValidMethod(method: string): method is FormMethod {
3115
3130
return validRequestMethods . has ( method as FormMethod ) ;
3116
3131
}
3117
3132
3118
- function isSubmissionMethod ( method : string ) : method is SubmissionFormMethod {
3119
- return validActionMethods . has ( method as SubmissionFormMethod ) ;
3133
+ function isMutationMethod ( method ? : string ) : method is MutationFormMethod {
3134
+ return validMutationMethods . has ( method as MutationFormMethod ) ;
3120
3135
}
3121
3136
3122
3137
async function resolveDeferredResults (
0 commit comments