router.js 193 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038
  1. /**
  2. * @remix-run/router v1.23.0
  3. *
  4. * Copyright (c) Remix Software Inc.
  5. *
  6. * This source code is licensed under the MIT license found in the
  7. * LICENSE.md file in the root directory of this source tree.
  8. *
  9. * @license MIT
  10. */
  11. function _extends() {
  12. _extends = Object.assign ? Object.assign.bind() : function (target) {
  13. for (var i = 1; i < arguments.length; i++) {
  14. var source = arguments[i];
  15. for (var key in source) {
  16. if (Object.prototype.hasOwnProperty.call(source, key)) {
  17. target[key] = source[key];
  18. }
  19. }
  20. }
  21. return target;
  22. };
  23. return _extends.apply(this, arguments);
  24. }
  25. ////////////////////////////////////////////////////////////////////////////////
  26. //#region Types and Constants
  27. ////////////////////////////////////////////////////////////////////////////////
  28. /**
  29. * Actions represent the type of change to a location value.
  30. */
  31. var Action;
  32. (function (Action) {
  33. /**
  34. * A POP indicates a change to an arbitrary index in the history stack, such
  35. * as a back or forward navigation. It does not describe the direction of the
  36. * navigation, only that the current index changed.
  37. *
  38. * Note: This is the default action for newly created history objects.
  39. */
  40. Action["Pop"] = "POP";
  41. /**
  42. * A PUSH indicates a new entry being added to the history stack, such as when
  43. * a link is clicked and a new page loads. When this happens, all subsequent
  44. * entries in the stack are lost.
  45. */
  46. Action["Push"] = "PUSH";
  47. /**
  48. * A REPLACE indicates the entry at the current index in the history stack
  49. * being replaced by a new one.
  50. */
  51. Action["Replace"] = "REPLACE";
  52. })(Action || (Action = {}));
  53. const PopStateEventType = "popstate";
  54. /**
  55. * Memory history stores the current location in memory. It is designed for use
  56. * in stateful non-browser environments like tests and React Native.
  57. */
  58. function createMemoryHistory(options) {
  59. if (options === void 0) {
  60. options = {};
  61. }
  62. let {
  63. initialEntries = ["/"],
  64. initialIndex,
  65. v5Compat = false
  66. } = options;
  67. let entries; // Declare so we can access from createMemoryLocation
  68. entries = initialEntries.map((entry, index) => createMemoryLocation(entry, typeof entry === "string" ? null : entry.state, index === 0 ? "default" : undefined));
  69. let index = clampIndex(initialIndex == null ? entries.length - 1 : initialIndex);
  70. let action = Action.Pop;
  71. let listener = null;
  72. function clampIndex(n) {
  73. return Math.min(Math.max(n, 0), entries.length - 1);
  74. }
  75. function getCurrentLocation() {
  76. return entries[index];
  77. }
  78. function createMemoryLocation(to, state, key) {
  79. if (state === void 0) {
  80. state = null;
  81. }
  82. let location = createLocation(entries ? getCurrentLocation().pathname : "/", to, state, key);
  83. warning(location.pathname.charAt(0) === "/", "relative pathnames are not supported in memory history: " + JSON.stringify(to));
  84. return location;
  85. }
  86. function createHref(to) {
  87. return typeof to === "string" ? to : createPath(to);
  88. }
  89. let history = {
  90. get index() {
  91. return index;
  92. },
  93. get action() {
  94. return action;
  95. },
  96. get location() {
  97. return getCurrentLocation();
  98. },
  99. createHref,
  100. createURL(to) {
  101. return new URL(createHref(to), "http://localhost");
  102. },
  103. encodeLocation(to) {
  104. let path = typeof to === "string" ? parsePath(to) : to;
  105. return {
  106. pathname: path.pathname || "",
  107. search: path.search || "",
  108. hash: path.hash || ""
  109. };
  110. },
  111. push(to, state) {
  112. action = Action.Push;
  113. let nextLocation = createMemoryLocation(to, state);
  114. index += 1;
  115. entries.splice(index, entries.length, nextLocation);
  116. if (v5Compat && listener) {
  117. listener({
  118. action,
  119. location: nextLocation,
  120. delta: 1
  121. });
  122. }
  123. },
  124. replace(to, state) {
  125. action = Action.Replace;
  126. let nextLocation = createMemoryLocation(to, state);
  127. entries[index] = nextLocation;
  128. if (v5Compat && listener) {
  129. listener({
  130. action,
  131. location: nextLocation,
  132. delta: 0
  133. });
  134. }
  135. },
  136. go(delta) {
  137. action = Action.Pop;
  138. let nextIndex = clampIndex(index + delta);
  139. let nextLocation = entries[nextIndex];
  140. index = nextIndex;
  141. if (listener) {
  142. listener({
  143. action,
  144. location: nextLocation,
  145. delta
  146. });
  147. }
  148. },
  149. listen(fn) {
  150. listener = fn;
  151. return () => {
  152. listener = null;
  153. };
  154. }
  155. };
  156. return history;
  157. }
  158. /**
  159. * Browser history stores the location in regular URLs. This is the standard for
  160. * most web apps, but it requires some configuration on the server to ensure you
  161. * serve the same app at multiple URLs.
  162. *
  163. * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#createbrowserhistory
  164. */
  165. function createBrowserHistory(options) {
  166. if (options === void 0) {
  167. options = {};
  168. }
  169. function createBrowserLocation(window, globalHistory) {
  170. let {
  171. pathname,
  172. search,
  173. hash
  174. } = window.location;
  175. return createLocation("", {
  176. pathname,
  177. search,
  178. hash
  179. },
  180. // state defaults to `null` because `window.history.state` does
  181. globalHistory.state && globalHistory.state.usr || null, globalHistory.state && globalHistory.state.key || "default");
  182. }
  183. function createBrowserHref(window, to) {
  184. return typeof to === "string" ? to : createPath(to);
  185. }
  186. return getUrlBasedHistory(createBrowserLocation, createBrowserHref, null, options);
  187. }
  188. /**
  189. * Hash history stores the location in window.location.hash. This makes it ideal
  190. * for situations where you don't want to send the location to the server for
  191. * some reason, either because you do cannot configure it or the URL space is
  192. * reserved for something else.
  193. *
  194. * @see https://github.com/remix-run/history/tree/main/docs/api-reference.md#createhashhistory
  195. */
  196. function createHashHistory(options) {
  197. if (options === void 0) {
  198. options = {};
  199. }
  200. function createHashLocation(window, globalHistory) {
  201. let {
  202. pathname = "/",
  203. search = "",
  204. hash = ""
  205. } = parsePath(window.location.hash.substr(1));
  206. // Hash URL should always have a leading / just like window.location.pathname
  207. // does, so if an app ends up at a route like /#something then we add a
  208. // leading slash so all of our path-matching behaves the same as if it would
  209. // in a browser router. This is particularly important when there exists a
  210. // root splat route (<Route path="*">) since that matches internally against
  211. // "/*" and we'd expect /#something to 404 in a hash router app.
  212. if (!pathname.startsWith("/") && !pathname.startsWith(".")) {
  213. pathname = "/" + pathname;
  214. }
  215. return createLocation("", {
  216. pathname,
  217. search,
  218. hash
  219. },
  220. // state defaults to `null` because `window.history.state` does
  221. globalHistory.state && globalHistory.state.usr || null, globalHistory.state && globalHistory.state.key || "default");
  222. }
  223. function createHashHref(window, to) {
  224. let base = window.document.querySelector("base");
  225. let href = "";
  226. if (base && base.getAttribute("href")) {
  227. let url = window.location.href;
  228. let hashIndex = url.indexOf("#");
  229. href = hashIndex === -1 ? url : url.slice(0, hashIndex);
  230. }
  231. return href + "#" + (typeof to === "string" ? to : createPath(to));
  232. }
  233. function validateHashLocation(location, to) {
  234. warning(location.pathname.charAt(0) === "/", "relative pathnames are not supported in hash history.push(" + JSON.stringify(to) + ")");
  235. }
  236. return getUrlBasedHistory(createHashLocation, createHashHref, validateHashLocation, options);
  237. }
  238. function invariant(value, message) {
  239. if (value === false || value === null || typeof value === "undefined") {
  240. throw new Error(message);
  241. }
  242. }
  243. function warning(cond, message) {
  244. if (!cond) {
  245. // eslint-disable-next-line no-console
  246. if (typeof console !== "undefined") console.warn(message);
  247. try {
  248. // Welcome to debugging history!
  249. //
  250. // This error is thrown as a convenience, so you can more easily
  251. // find the source for a warning that appears in the console by
  252. // enabling "pause on exceptions" in your JavaScript debugger.
  253. throw new Error(message);
  254. // eslint-disable-next-line no-empty
  255. } catch (e) {}
  256. }
  257. }
  258. function createKey() {
  259. return Math.random().toString(36).substr(2, 8);
  260. }
  261. /**
  262. * For browser-based histories, we combine the state and key into an object
  263. */
  264. function getHistoryState(location, index) {
  265. return {
  266. usr: location.state,
  267. key: location.key,
  268. idx: index
  269. };
  270. }
  271. /**
  272. * Creates a Location object with a unique key from the given Path
  273. */
  274. function createLocation(current, to, state, key) {
  275. if (state === void 0) {
  276. state = null;
  277. }
  278. let location = _extends({
  279. pathname: typeof current === "string" ? current : current.pathname,
  280. search: "",
  281. hash: ""
  282. }, typeof to === "string" ? parsePath(to) : to, {
  283. state,
  284. // TODO: This could be cleaned up. push/replace should probably just take
  285. // full Locations now and avoid the need to run through this flow at all
  286. // But that's a pretty big refactor to the current test suite so going to
  287. // keep as is for the time being and just let any incoming keys take precedence
  288. key: to && to.key || key || createKey()
  289. });
  290. return location;
  291. }
  292. /**
  293. * Creates a string URL path from the given pathname, search, and hash components.
  294. */
  295. function createPath(_ref) {
  296. let {
  297. pathname = "/",
  298. search = "",
  299. hash = ""
  300. } = _ref;
  301. if (search && search !== "?") pathname += search.charAt(0) === "?" ? search : "?" + search;
  302. if (hash && hash !== "#") pathname += hash.charAt(0) === "#" ? hash : "#" + hash;
  303. return pathname;
  304. }
  305. /**
  306. * Parses a string URL path into its separate pathname, search, and hash components.
  307. */
  308. function parsePath(path) {
  309. let parsedPath = {};
  310. if (path) {
  311. let hashIndex = path.indexOf("#");
  312. if (hashIndex >= 0) {
  313. parsedPath.hash = path.substr(hashIndex);
  314. path = path.substr(0, hashIndex);
  315. }
  316. let searchIndex = path.indexOf("?");
  317. if (searchIndex >= 0) {
  318. parsedPath.search = path.substr(searchIndex);
  319. path = path.substr(0, searchIndex);
  320. }
  321. if (path) {
  322. parsedPath.pathname = path;
  323. }
  324. }
  325. return parsedPath;
  326. }
  327. function getUrlBasedHistory(getLocation, createHref, validateLocation, options) {
  328. if (options === void 0) {
  329. options = {};
  330. }
  331. let {
  332. window = document.defaultView,
  333. v5Compat = false
  334. } = options;
  335. let globalHistory = window.history;
  336. let action = Action.Pop;
  337. let listener = null;
  338. let index = getIndex();
  339. // Index should only be null when we initialize. If not, it's because the
  340. // user called history.pushState or history.replaceState directly, in which
  341. // case we should log a warning as it will result in bugs.
  342. if (index == null) {
  343. index = 0;
  344. globalHistory.replaceState(_extends({}, globalHistory.state, {
  345. idx: index
  346. }), "");
  347. }
  348. function getIndex() {
  349. let state = globalHistory.state || {
  350. idx: null
  351. };
  352. return state.idx;
  353. }
  354. function handlePop() {
  355. action = Action.Pop;
  356. let nextIndex = getIndex();
  357. let delta = nextIndex == null ? null : nextIndex - index;
  358. index = nextIndex;
  359. if (listener) {
  360. listener({
  361. action,
  362. location: history.location,
  363. delta
  364. });
  365. }
  366. }
  367. function push(to, state) {
  368. action = Action.Push;
  369. let location = createLocation(history.location, to, state);
  370. if (validateLocation) validateLocation(location, to);
  371. index = getIndex() + 1;
  372. let historyState = getHistoryState(location, index);
  373. let url = history.createHref(location);
  374. // try...catch because iOS limits us to 100 pushState calls :/
  375. try {
  376. globalHistory.pushState(historyState, "", url);
  377. } catch (error) {
  378. // If the exception is because `state` can't be serialized, let that throw
  379. // outwards just like a replace call would so the dev knows the cause
  380. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#shared-history-push/replace-state-steps
  381. // https://html.spec.whatwg.org/multipage/structured-data.html#structuredserializeinternal
  382. if (error instanceof DOMException && error.name === "DataCloneError") {
  383. throw error;
  384. }
  385. // They are going to lose state here, but there is no real
  386. // way to warn them about it since the page will refresh...
  387. window.location.assign(url);
  388. }
  389. if (v5Compat && listener) {
  390. listener({
  391. action,
  392. location: history.location,
  393. delta: 1
  394. });
  395. }
  396. }
  397. function replace(to, state) {
  398. action = Action.Replace;
  399. let location = createLocation(history.location, to, state);
  400. if (validateLocation) validateLocation(location, to);
  401. index = getIndex();
  402. let historyState = getHistoryState(location, index);
  403. let url = history.createHref(location);
  404. globalHistory.replaceState(historyState, "", url);
  405. if (v5Compat && listener) {
  406. listener({
  407. action,
  408. location: history.location,
  409. delta: 0
  410. });
  411. }
  412. }
  413. function createURL(to) {
  414. // window.location.origin is "null" (the literal string value) in Firefox
  415. // under certain conditions, notably when serving from a local HTML file
  416. // See https://bugzilla.mozilla.org/show_bug.cgi?id=878297
  417. let base = window.location.origin !== "null" ? window.location.origin : window.location.href;
  418. let href = typeof to === "string" ? to : createPath(to);
  419. // Treating this as a full URL will strip any trailing spaces so we need to
  420. // pre-encode them since they might be part of a matching splat param from
  421. // an ancestor route
  422. href = href.replace(/ $/, "%20");
  423. invariant(base, "No window.location.(origin|href) available to create URL for href: " + href);
  424. return new URL(href, base);
  425. }
  426. let history = {
  427. get action() {
  428. return action;
  429. },
  430. get location() {
  431. return getLocation(window, globalHistory);
  432. },
  433. listen(fn) {
  434. if (listener) {
  435. throw new Error("A history only accepts one active listener");
  436. }
  437. window.addEventListener(PopStateEventType, handlePop);
  438. listener = fn;
  439. return () => {
  440. window.removeEventListener(PopStateEventType, handlePop);
  441. listener = null;
  442. };
  443. },
  444. createHref(to) {
  445. return createHref(window, to);
  446. },
  447. createURL,
  448. encodeLocation(to) {
  449. // Encode a Location the same way window.location would
  450. let url = createURL(to);
  451. return {
  452. pathname: url.pathname,
  453. search: url.search,
  454. hash: url.hash
  455. };
  456. },
  457. push,
  458. replace,
  459. go(n) {
  460. return globalHistory.go(n);
  461. }
  462. };
  463. return history;
  464. }
  465. //#endregion
  466. var ResultType;
  467. (function (ResultType) {
  468. ResultType["data"] = "data";
  469. ResultType["deferred"] = "deferred";
  470. ResultType["redirect"] = "redirect";
  471. ResultType["error"] = "error";
  472. })(ResultType || (ResultType = {}));
  473. const immutableRouteKeys = new Set(["lazy", "caseSensitive", "path", "id", "index", "children"]);
  474. function isIndexRoute(route) {
  475. return route.index === true;
  476. }
  477. // Walk the route tree generating unique IDs where necessary, so we are working
  478. // solely with AgnosticDataRouteObject's within the Router
  479. function convertRoutesToDataRoutes(routes, mapRouteProperties, parentPath, manifest) {
  480. if (parentPath === void 0) {
  481. parentPath = [];
  482. }
  483. if (manifest === void 0) {
  484. manifest = {};
  485. }
  486. return routes.map((route, index) => {
  487. let treePath = [...parentPath, String(index)];
  488. let id = typeof route.id === "string" ? route.id : treePath.join("-");
  489. invariant(route.index !== true || !route.children, "Cannot specify children on an index route");
  490. invariant(!manifest[id], "Found a route id collision on id \"" + id + "\". Route " + "id's must be globally unique within Data Router usages");
  491. if (isIndexRoute(route)) {
  492. let indexRoute = _extends({}, route, mapRouteProperties(route), {
  493. id
  494. });
  495. manifest[id] = indexRoute;
  496. return indexRoute;
  497. } else {
  498. let pathOrLayoutRoute = _extends({}, route, mapRouteProperties(route), {
  499. id,
  500. children: undefined
  501. });
  502. manifest[id] = pathOrLayoutRoute;
  503. if (route.children) {
  504. pathOrLayoutRoute.children = convertRoutesToDataRoutes(route.children, mapRouteProperties, treePath, manifest);
  505. }
  506. return pathOrLayoutRoute;
  507. }
  508. });
  509. }
  510. /**
  511. * Matches the given routes to a location and returns the match data.
  512. *
  513. * @see https://reactrouter.com/v6/utils/match-routes
  514. */
  515. function matchRoutes(routes, locationArg, basename) {
  516. if (basename === void 0) {
  517. basename = "/";
  518. }
  519. return matchRoutesImpl(routes, locationArg, basename, false);
  520. }
  521. function matchRoutesImpl(routes, locationArg, basename, allowPartial) {
  522. let location = typeof locationArg === "string" ? parsePath(locationArg) : locationArg;
  523. let pathname = stripBasename(location.pathname || "/", basename);
  524. if (pathname == null) {
  525. return null;
  526. }
  527. let branches = flattenRoutes(routes);
  528. rankRouteBranches(branches);
  529. let matches = null;
  530. for (let i = 0; matches == null && i < branches.length; ++i) {
  531. // Incoming pathnames are generally encoded from either window.location
  532. // or from router.navigate, but we want to match against the unencoded
  533. // paths in the route definitions. Memory router locations won't be
  534. // encoded here but there also shouldn't be anything to decode so this
  535. // should be a safe operation. This avoids needing matchRoutes to be
  536. // history-aware.
  537. let decoded = decodePath(pathname);
  538. matches = matchRouteBranch(branches[i], decoded, allowPartial);
  539. }
  540. return matches;
  541. }
  542. function convertRouteMatchToUiMatch(match, loaderData) {
  543. let {
  544. route,
  545. pathname,
  546. params
  547. } = match;
  548. return {
  549. id: route.id,
  550. pathname,
  551. params,
  552. data: loaderData[route.id],
  553. handle: route.handle
  554. };
  555. }
  556. function flattenRoutes(routes, branches, parentsMeta, parentPath) {
  557. if (branches === void 0) {
  558. branches = [];
  559. }
  560. if (parentsMeta === void 0) {
  561. parentsMeta = [];
  562. }
  563. if (parentPath === void 0) {
  564. parentPath = "";
  565. }
  566. let flattenRoute = (route, index, relativePath) => {
  567. let meta = {
  568. relativePath: relativePath === undefined ? route.path || "" : relativePath,
  569. caseSensitive: route.caseSensitive === true,
  570. childrenIndex: index,
  571. route
  572. };
  573. if (meta.relativePath.startsWith("/")) {
  574. invariant(meta.relativePath.startsWith(parentPath), "Absolute route path \"" + meta.relativePath + "\" nested under path " + ("\"" + parentPath + "\" is not valid. An absolute child route path ") + "must start with the combined path of all its parent routes.");
  575. meta.relativePath = meta.relativePath.slice(parentPath.length);
  576. }
  577. let path = joinPaths([parentPath, meta.relativePath]);
  578. let routesMeta = parentsMeta.concat(meta);
  579. // Add the children before adding this route to the array, so we traverse the
  580. // route tree depth-first and child routes appear before their parents in
  581. // the "flattened" version.
  582. if (route.children && route.children.length > 0) {
  583. invariant(
  584. // Our types know better, but runtime JS may not!
  585. // @ts-expect-error
  586. route.index !== true, "Index routes must not have child routes. Please remove " + ("all child routes from route path \"" + path + "\"."));
  587. flattenRoutes(route.children, branches, routesMeta, path);
  588. }
  589. // Routes without a path shouldn't ever match by themselves unless they are
  590. // index routes, so don't add them to the list of possible branches.
  591. if (route.path == null && !route.index) {
  592. return;
  593. }
  594. branches.push({
  595. path,
  596. score: computeScore(path, route.index),
  597. routesMeta
  598. });
  599. };
  600. routes.forEach((route, index) => {
  601. var _route$path;
  602. // coarse-grain check for optional params
  603. if (route.path === "" || !((_route$path = route.path) != null && _route$path.includes("?"))) {
  604. flattenRoute(route, index);
  605. } else {
  606. for (let exploded of explodeOptionalSegments(route.path)) {
  607. flattenRoute(route, index, exploded);
  608. }
  609. }
  610. });
  611. return branches;
  612. }
  613. /**
  614. * Computes all combinations of optional path segments for a given path,
  615. * excluding combinations that are ambiguous and of lower priority.
  616. *
  617. * For example, `/one/:two?/three/:four?/:five?` explodes to:
  618. * - `/one/three`
  619. * - `/one/:two/three`
  620. * - `/one/three/:four`
  621. * - `/one/three/:five`
  622. * - `/one/:two/three/:four`
  623. * - `/one/:two/three/:five`
  624. * - `/one/three/:four/:five`
  625. * - `/one/:two/three/:four/:five`
  626. */
  627. function explodeOptionalSegments(path) {
  628. let segments = path.split("/");
  629. if (segments.length === 0) return [];
  630. let [first, ...rest] = segments;
  631. // Optional path segments are denoted by a trailing `?`
  632. let isOptional = first.endsWith("?");
  633. // Compute the corresponding required segment: `foo?` -> `foo`
  634. let required = first.replace(/\?$/, "");
  635. if (rest.length === 0) {
  636. // Intepret empty string as omitting an optional segment
  637. // `["one", "", "three"]` corresponds to omitting `:two` from `/one/:two?/three` -> `/one/three`
  638. return isOptional ? [required, ""] : [required];
  639. }
  640. let restExploded = explodeOptionalSegments(rest.join("/"));
  641. let result = [];
  642. // All child paths with the prefix. Do this for all children before the
  643. // optional version for all children, so we get consistent ordering where the
  644. // parent optional aspect is preferred as required. Otherwise, we can get
  645. // child sections interspersed where deeper optional segments are higher than
  646. // parent optional segments, where for example, /:two would explode _earlier_
  647. // then /:one. By always including the parent as required _for all children_
  648. // first, we avoid this issue
  649. result.push(...restExploded.map(subpath => subpath === "" ? required : [required, subpath].join("/")));
  650. // Then, if this is an optional value, add all child versions without
  651. if (isOptional) {
  652. result.push(...restExploded);
  653. }
  654. // for absolute paths, ensure `/` instead of empty segment
  655. return result.map(exploded => path.startsWith("/") && exploded === "" ? "/" : exploded);
  656. }
  657. function rankRouteBranches(branches) {
  658. branches.sort((a, b) => a.score !== b.score ? b.score - a.score // Higher score first
  659. : compareIndexes(a.routesMeta.map(meta => meta.childrenIndex), b.routesMeta.map(meta => meta.childrenIndex)));
  660. }
  661. const paramRe = /^:[\w-]+$/;
  662. const dynamicSegmentValue = 3;
  663. const indexRouteValue = 2;
  664. const emptySegmentValue = 1;
  665. const staticSegmentValue = 10;
  666. const splatPenalty = -2;
  667. const isSplat = s => s === "*";
  668. function computeScore(path, index) {
  669. let segments = path.split("/");
  670. let initialScore = segments.length;
  671. if (segments.some(isSplat)) {
  672. initialScore += splatPenalty;
  673. }
  674. if (index) {
  675. initialScore += indexRouteValue;
  676. }
  677. return segments.filter(s => !isSplat(s)).reduce((score, segment) => score + (paramRe.test(segment) ? dynamicSegmentValue : segment === "" ? emptySegmentValue : staticSegmentValue), initialScore);
  678. }
  679. function compareIndexes(a, b) {
  680. let siblings = a.length === b.length && a.slice(0, -1).every((n, i) => n === b[i]);
  681. return siblings ?
  682. // If two routes are siblings, we should try to match the earlier sibling
  683. // first. This allows people to have fine-grained control over the matching
  684. // behavior by simply putting routes with identical paths in the order they
  685. // want them tried.
  686. a[a.length - 1] - b[b.length - 1] :
  687. // Otherwise, it doesn't really make sense to rank non-siblings by index,
  688. // so they sort equally.
  689. 0;
  690. }
  691. function matchRouteBranch(branch, pathname, allowPartial) {
  692. if (allowPartial === void 0) {
  693. allowPartial = false;
  694. }
  695. let {
  696. routesMeta
  697. } = branch;
  698. let matchedParams = {};
  699. let matchedPathname = "/";
  700. let matches = [];
  701. for (let i = 0; i < routesMeta.length; ++i) {
  702. let meta = routesMeta[i];
  703. let end = i === routesMeta.length - 1;
  704. let remainingPathname = matchedPathname === "/" ? pathname : pathname.slice(matchedPathname.length) || "/";
  705. let match = matchPath({
  706. path: meta.relativePath,
  707. caseSensitive: meta.caseSensitive,
  708. end
  709. }, remainingPathname);
  710. let route = meta.route;
  711. if (!match && end && allowPartial && !routesMeta[routesMeta.length - 1].route.index) {
  712. match = matchPath({
  713. path: meta.relativePath,
  714. caseSensitive: meta.caseSensitive,
  715. end: false
  716. }, remainingPathname);
  717. }
  718. if (!match) {
  719. return null;
  720. }
  721. Object.assign(matchedParams, match.params);
  722. matches.push({
  723. // TODO: Can this as be avoided?
  724. params: matchedParams,
  725. pathname: joinPaths([matchedPathname, match.pathname]),
  726. pathnameBase: normalizePathname(joinPaths([matchedPathname, match.pathnameBase])),
  727. route
  728. });
  729. if (match.pathnameBase !== "/") {
  730. matchedPathname = joinPaths([matchedPathname, match.pathnameBase]);
  731. }
  732. }
  733. return matches;
  734. }
  735. /**
  736. * Returns a path with params interpolated.
  737. *
  738. * @see https://reactrouter.com/v6/utils/generate-path
  739. */
  740. function generatePath(originalPath, params) {
  741. if (params === void 0) {
  742. params = {};
  743. }
  744. let path = originalPath;
  745. if (path.endsWith("*") && path !== "*" && !path.endsWith("/*")) {
  746. warning(false, "Route path \"" + path + "\" will be treated as if it were " + ("\"" + path.replace(/\*$/, "/*") + "\" because the `*` character must ") + "always follow a `/` in the pattern. To get rid of this warning, " + ("please change the route path to \"" + path.replace(/\*$/, "/*") + "\"."));
  747. path = path.replace(/\*$/, "/*");
  748. }
  749. // ensure `/` is added at the beginning if the path is absolute
  750. const prefix = path.startsWith("/") ? "/" : "";
  751. const stringify = p => p == null ? "" : typeof p === "string" ? p : String(p);
  752. const segments = path.split(/\/+/).map((segment, index, array) => {
  753. const isLastSegment = index === array.length - 1;
  754. // only apply the splat if it's the last segment
  755. if (isLastSegment && segment === "*") {
  756. const star = "*";
  757. // Apply the splat
  758. return stringify(params[star]);
  759. }
  760. const keyMatch = segment.match(/^:([\w-]+)(\??)$/);
  761. if (keyMatch) {
  762. const [, key, optional] = keyMatch;
  763. let param = params[key];
  764. invariant(optional === "?" || param != null, "Missing \":" + key + "\" param");
  765. return stringify(param);
  766. }
  767. // Remove any optional markers from optional static segments
  768. return segment.replace(/\?$/g, "");
  769. })
  770. // Remove empty segments
  771. .filter(segment => !!segment);
  772. return prefix + segments.join("/");
  773. }
  774. /**
  775. * Performs pattern matching on a URL pathname and returns information about
  776. * the match.
  777. *
  778. * @see https://reactrouter.com/v6/utils/match-path
  779. */
  780. function matchPath(pattern, pathname) {
  781. if (typeof pattern === "string") {
  782. pattern = {
  783. path: pattern,
  784. caseSensitive: false,
  785. end: true
  786. };
  787. }
  788. let [matcher, compiledParams] = compilePath(pattern.path, pattern.caseSensitive, pattern.end);
  789. let match = pathname.match(matcher);
  790. if (!match) return null;
  791. let matchedPathname = match[0];
  792. let pathnameBase = matchedPathname.replace(/(.)\/+$/, "$1");
  793. let captureGroups = match.slice(1);
  794. let params = compiledParams.reduce((memo, _ref, index) => {
  795. let {
  796. paramName,
  797. isOptional
  798. } = _ref;
  799. // We need to compute the pathnameBase here using the raw splat value
  800. // instead of using params["*"] later because it will be decoded then
  801. if (paramName === "*") {
  802. let splatValue = captureGroups[index] || "";
  803. pathnameBase = matchedPathname.slice(0, matchedPathname.length - splatValue.length).replace(/(.)\/+$/, "$1");
  804. }
  805. const value = captureGroups[index];
  806. if (isOptional && !value) {
  807. memo[paramName] = undefined;
  808. } else {
  809. memo[paramName] = (value || "").replace(/%2F/g, "/");
  810. }
  811. return memo;
  812. }, {});
  813. return {
  814. params,
  815. pathname: matchedPathname,
  816. pathnameBase,
  817. pattern
  818. };
  819. }
  820. function compilePath(path, caseSensitive, end) {
  821. if (caseSensitive === void 0) {
  822. caseSensitive = false;
  823. }
  824. if (end === void 0) {
  825. end = true;
  826. }
  827. warning(path === "*" || !path.endsWith("*") || path.endsWith("/*"), "Route path \"" + path + "\" will be treated as if it were " + ("\"" + path.replace(/\*$/, "/*") + "\" because the `*` character must ") + "always follow a `/` in the pattern. To get rid of this warning, " + ("please change the route path to \"" + path.replace(/\*$/, "/*") + "\"."));
  828. let params = [];
  829. let regexpSource = "^" + path.replace(/\/*\*?$/, "") // Ignore trailing / and /*, we'll handle it below
  830. .replace(/^\/*/, "/") // Make sure it has a leading /
  831. .replace(/[\\.*+^${}|()[\]]/g, "\\$&") // Escape special regex chars
  832. .replace(/\/:([\w-]+)(\?)?/g, (_, paramName, isOptional) => {
  833. params.push({
  834. paramName,
  835. isOptional: isOptional != null
  836. });
  837. return isOptional ? "/?([^\\/]+)?" : "/([^\\/]+)";
  838. });
  839. if (path.endsWith("*")) {
  840. params.push({
  841. paramName: "*"
  842. });
  843. regexpSource += path === "*" || path === "/*" ? "(.*)$" // Already matched the initial /, just match the rest
  844. : "(?:\\/(.+)|\\/*)$"; // Don't include the / in params["*"]
  845. } else if (end) {
  846. // When matching to the end, ignore trailing slashes
  847. regexpSource += "\\/*$";
  848. } else if (path !== "" && path !== "/") {
  849. // If our path is non-empty and contains anything beyond an initial slash,
  850. // then we have _some_ form of path in our regex, so we should expect to
  851. // match only if we find the end of this path segment. Look for an optional
  852. // non-captured trailing slash (to match a portion of the URL) or the end
  853. // of the path (if we've matched to the end). We used to do this with a
  854. // word boundary but that gives false positives on routes like
  855. // /user-preferences since `-` counts as a word boundary.
  856. regexpSource += "(?:(?=\\/|$))";
  857. } else ;
  858. let matcher = new RegExp(regexpSource, caseSensitive ? undefined : "i");
  859. return [matcher, params];
  860. }
  861. function decodePath(value) {
  862. try {
  863. return value.split("/").map(v => decodeURIComponent(v).replace(/\//g, "%2F")).join("/");
  864. } catch (error) {
  865. warning(false, "The URL path \"" + value + "\" could not be decoded because it is is a " + "malformed URL segment. This is probably due to a bad percent " + ("encoding (" + error + ")."));
  866. return value;
  867. }
  868. }
  869. /**
  870. * @private
  871. */
  872. function stripBasename(pathname, basename) {
  873. if (basename === "/") return pathname;
  874. if (!pathname.toLowerCase().startsWith(basename.toLowerCase())) {
  875. return null;
  876. }
  877. // We want to leave trailing slash behavior in the user's control, so if they
  878. // specify a basename with a trailing slash, we should support it
  879. let startIndex = basename.endsWith("/") ? basename.length - 1 : basename.length;
  880. let nextChar = pathname.charAt(startIndex);
  881. if (nextChar && nextChar !== "/") {
  882. // pathname does not start with basename/
  883. return null;
  884. }
  885. return pathname.slice(startIndex) || "/";
  886. }
  887. /**
  888. * Returns a resolved path object relative to the given pathname.
  889. *
  890. * @see https://reactrouter.com/v6/utils/resolve-path
  891. */
  892. function resolvePath(to, fromPathname) {
  893. if (fromPathname === void 0) {
  894. fromPathname = "/";
  895. }
  896. let {
  897. pathname: toPathname,
  898. search = "",
  899. hash = ""
  900. } = typeof to === "string" ? parsePath(to) : to;
  901. let pathname = toPathname ? toPathname.startsWith("/") ? toPathname : resolvePathname(toPathname, fromPathname) : fromPathname;
  902. return {
  903. pathname,
  904. search: normalizeSearch(search),
  905. hash: normalizeHash(hash)
  906. };
  907. }
  908. function resolvePathname(relativePath, fromPathname) {
  909. let segments = fromPathname.replace(/\/+$/, "").split("/");
  910. let relativeSegments = relativePath.split("/");
  911. relativeSegments.forEach(segment => {
  912. if (segment === "..") {
  913. // Keep the root "" segment so the pathname starts at /
  914. if (segments.length > 1) segments.pop();
  915. } else if (segment !== ".") {
  916. segments.push(segment);
  917. }
  918. });
  919. return segments.length > 1 ? segments.join("/") : "/";
  920. }
  921. function getInvalidPathError(char, field, dest, path) {
  922. return "Cannot include a '" + char + "' character in a manually specified " + ("`to." + field + "` field [" + JSON.stringify(path) + "]. Please separate it out to the ") + ("`to." + dest + "` field. Alternatively you may provide the full path as ") + "a string in <Link to=\"...\"> and the router will parse it for you.";
  923. }
  924. /**
  925. * @private
  926. *
  927. * When processing relative navigation we want to ignore ancestor routes that
  928. * do not contribute to the path, such that index/pathless layout routes don't
  929. * interfere.
  930. *
  931. * For example, when moving a route element into an index route and/or a
  932. * pathless layout route, relative link behavior contained within should stay
  933. * the same. Both of the following examples should link back to the root:
  934. *
  935. * <Route path="/">
  936. * <Route path="accounts" element={<Link to=".."}>
  937. * </Route>
  938. *
  939. * <Route path="/">
  940. * <Route path="accounts">
  941. * <Route element={<AccountsLayout />}> // <-- Does not contribute
  942. * <Route index element={<Link to=".."} /> // <-- Does not contribute
  943. * </Route
  944. * </Route>
  945. * </Route>
  946. */
  947. function getPathContributingMatches(matches) {
  948. return matches.filter((match, index) => index === 0 || match.route.path && match.route.path.length > 0);
  949. }
  950. // Return the array of pathnames for the current route matches - used to
  951. // generate the routePathnames input for resolveTo()
  952. function getResolveToMatches(matches, v7_relativeSplatPath) {
  953. let pathMatches = getPathContributingMatches(matches);
  954. // When v7_relativeSplatPath is enabled, use the full pathname for the leaf
  955. // match so we include splat values for "." links. See:
  956. // https://github.com/remix-run/react-router/issues/11052#issuecomment-1836589329
  957. if (v7_relativeSplatPath) {
  958. return pathMatches.map((match, idx) => idx === pathMatches.length - 1 ? match.pathname : match.pathnameBase);
  959. }
  960. return pathMatches.map(match => match.pathnameBase);
  961. }
  962. /**
  963. * @private
  964. */
  965. function resolveTo(toArg, routePathnames, locationPathname, isPathRelative) {
  966. if (isPathRelative === void 0) {
  967. isPathRelative = false;
  968. }
  969. let to;
  970. if (typeof toArg === "string") {
  971. to = parsePath(toArg);
  972. } else {
  973. to = _extends({}, toArg);
  974. invariant(!to.pathname || !to.pathname.includes("?"), getInvalidPathError("?", "pathname", "search", to));
  975. invariant(!to.pathname || !to.pathname.includes("#"), getInvalidPathError("#", "pathname", "hash", to));
  976. invariant(!to.search || !to.search.includes("#"), getInvalidPathError("#", "search", "hash", to));
  977. }
  978. let isEmptyPath = toArg === "" || to.pathname === "";
  979. let toPathname = isEmptyPath ? "/" : to.pathname;
  980. let from;
  981. // Routing is relative to the current pathname if explicitly requested.
  982. //
  983. // If a pathname is explicitly provided in `to`, it should be relative to the
  984. // route context. This is explained in `Note on `<Link to>` values` in our
  985. // migration guide from v5 as a means of disambiguation between `to` values
  986. // that begin with `/` and those that do not. However, this is problematic for
  987. // `to` values that do not provide a pathname. `to` can simply be a search or
  988. // hash string, in which case we should assume that the navigation is relative
  989. // to the current location's pathname and *not* the route pathname.
  990. if (toPathname == null) {
  991. from = locationPathname;
  992. } else {
  993. let routePathnameIndex = routePathnames.length - 1;
  994. // With relative="route" (the default), each leading .. segment means
  995. // "go up one route" instead of "go up one URL segment". This is a key
  996. // difference from how <a href> works and a major reason we call this a
  997. // "to" value instead of a "href".
  998. if (!isPathRelative && toPathname.startsWith("..")) {
  999. let toSegments = toPathname.split("/");
  1000. while (toSegments[0] === "..") {
  1001. toSegments.shift();
  1002. routePathnameIndex -= 1;
  1003. }
  1004. to.pathname = toSegments.join("/");
  1005. }
  1006. from = routePathnameIndex >= 0 ? routePathnames[routePathnameIndex] : "/";
  1007. }
  1008. let path = resolvePath(to, from);
  1009. // Ensure the pathname has a trailing slash if the original "to" had one
  1010. let hasExplicitTrailingSlash = toPathname && toPathname !== "/" && toPathname.endsWith("/");
  1011. // Or if this was a link to the current path which has a trailing slash
  1012. let hasCurrentTrailingSlash = (isEmptyPath || toPathname === ".") && locationPathname.endsWith("/");
  1013. if (!path.pathname.endsWith("/") && (hasExplicitTrailingSlash || hasCurrentTrailingSlash)) {
  1014. path.pathname += "/";
  1015. }
  1016. return path;
  1017. }
  1018. /**
  1019. * @private
  1020. */
  1021. function getToPathname(to) {
  1022. // Empty strings should be treated the same as / paths
  1023. return to === "" || to.pathname === "" ? "/" : typeof to === "string" ? parsePath(to).pathname : to.pathname;
  1024. }
  1025. /**
  1026. * @private
  1027. */
  1028. const joinPaths = paths => paths.join("/").replace(/\/\/+/g, "/");
  1029. /**
  1030. * @private
  1031. */
  1032. const normalizePathname = pathname => pathname.replace(/\/+$/, "").replace(/^\/*/, "/");
  1033. /**
  1034. * @private
  1035. */
  1036. const normalizeSearch = search => !search || search === "?" ? "" : search.startsWith("?") ? search : "?" + search;
  1037. /**
  1038. * @private
  1039. */
  1040. const normalizeHash = hash => !hash || hash === "#" ? "" : hash.startsWith("#") ? hash : "#" + hash;
  1041. /**
  1042. * This is a shortcut for creating `application/json` responses. Converts `data`
  1043. * to JSON and sets the `Content-Type` header.
  1044. *
  1045. * @deprecated The `json` method is deprecated in favor of returning raw objects.
  1046. * This method will be removed in v7.
  1047. */
  1048. const json = function json(data, init) {
  1049. if (init === void 0) {
  1050. init = {};
  1051. }
  1052. let responseInit = typeof init === "number" ? {
  1053. status: init
  1054. } : init;
  1055. let headers = new Headers(responseInit.headers);
  1056. if (!headers.has("Content-Type")) {
  1057. headers.set("Content-Type", "application/json; charset=utf-8");
  1058. }
  1059. return new Response(JSON.stringify(data), _extends({}, responseInit, {
  1060. headers
  1061. }));
  1062. };
  1063. class DataWithResponseInit {
  1064. constructor(data, init) {
  1065. this.type = "DataWithResponseInit";
  1066. this.data = data;
  1067. this.init = init || null;
  1068. }
  1069. }
  1070. /**
  1071. * Create "responses" that contain `status`/`headers` without forcing
  1072. * serialization into an actual `Response` - used by Remix single fetch
  1073. */
  1074. function data(data, init) {
  1075. return new DataWithResponseInit(data, typeof init === "number" ? {
  1076. status: init
  1077. } : init);
  1078. }
  1079. class AbortedDeferredError extends Error {}
  1080. class DeferredData {
  1081. constructor(data, responseInit) {
  1082. this.pendingKeysSet = new Set();
  1083. this.subscribers = new Set();
  1084. this.deferredKeys = [];
  1085. invariant(data && typeof data === "object" && !Array.isArray(data), "defer() only accepts plain objects");
  1086. // Set up an AbortController + Promise we can race against to exit early
  1087. // cancellation
  1088. let reject;
  1089. this.abortPromise = new Promise((_, r) => reject = r);
  1090. this.controller = new AbortController();
  1091. let onAbort = () => reject(new AbortedDeferredError("Deferred data aborted"));
  1092. this.unlistenAbortSignal = () => this.controller.signal.removeEventListener("abort", onAbort);
  1093. this.controller.signal.addEventListener("abort", onAbort);
  1094. this.data = Object.entries(data).reduce((acc, _ref2) => {
  1095. let [key, value] = _ref2;
  1096. return Object.assign(acc, {
  1097. [key]: this.trackPromise(key, value)
  1098. });
  1099. }, {});
  1100. if (this.done) {
  1101. // All incoming values were resolved
  1102. this.unlistenAbortSignal();
  1103. }
  1104. this.init = responseInit;
  1105. }
  1106. trackPromise(key, value) {
  1107. if (!(value instanceof Promise)) {
  1108. return value;
  1109. }
  1110. this.deferredKeys.push(key);
  1111. this.pendingKeysSet.add(key);
  1112. // We store a little wrapper promise that will be extended with
  1113. // _data/_error props upon resolve/reject
  1114. let promise = Promise.race([value, this.abortPromise]).then(data => this.onSettle(promise, key, undefined, data), error => this.onSettle(promise, key, error));
  1115. // Register rejection listeners to avoid uncaught promise rejections on
  1116. // errors or aborted deferred values
  1117. promise.catch(() => {});
  1118. Object.defineProperty(promise, "_tracked", {
  1119. get: () => true
  1120. });
  1121. return promise;
  1122. }
  1123. onSettle(promise, key, error, data) {
  1124. if (this.controller.signal.aborted && error instanceof AbortedDeferredError) {
  1125. this.unlistenAbortSignal();
  1126. Object.defineProperty(promise, "_error", {
  1127. get: () => error
  1128. });
  1129. return Promise.reject(error);
  1130. }
  1131. this.pendingKeysSet.delete(key);
  1132. if (this.done) {
  1133. // Nothing left to abort!
  1134. this.unlistenAbortSignal();
  1135. }
  1136. // If the promise was resolved/rejected with undefined, we'll throw an error as you
  1137. // should always resolve with a value or null
  1138. if (error === undefined && data === undefined) {
  1139. let undefinedError = new Error("Deferred data for key \"" + key + "\" resolved/rejected with `undefined`, " + "you must resolve/reject with a value or `null`.");
  1140. Object.defineProperty(promise, "_error", {
  1141. get: () => undefinedError
  1142. });
  1143. this.emit(false, key);
  1144. return Promise.reject(undefinedError);
  1145. }
  1146. if (data === undefined) {
  1147. Object.defineProperty(promise, "_error", {
  1148. get: () => error
  1149. });
  1150. this.emit(false, key);
  1151. return Promise.reject(error);
  1152. }
  1153. Object.defineProperty(promise, "_data", {
  1154. get: () => data
  1155. });
  1156. this.emit(false, key);
  1157. return data;
  1158. }
  1159. emit(aborted, settledKey) {
  1160. this.subscribers.forEach(subscriber => subscriber(aborted, settledKey));
  1161. }
  1162. subscribe(fn) {
  1163. this.subscribers.add(fn);
  1164. return () => this.subscribers.delete(fn);
  1165. }
  1166. cancel() {
  1167. this.controller.abort();
  1168. this.pendingKeysSet.forEach((v, k) => this.pendingKeysSet.delete(k));
  1169. this.emit(true);
  1170. }
  1171. async resolveData(signal) {
  1172. let aborted = false;
  1173. if (!this.done) {
  1174. let onAbort = () => this.cancel();
  1175. signal.addEventListener("abort", onAbort);
  1176. aborted = await new Promise(resolve => {
  1177. this.subscribe(aborted => {
  1178. signal.removeEventListener("abort", onAbort);
  1179. if (aborted || this.done) {
  1180. resolve(aborted);
  1181. }
  1182. });
  1183. });
  1184. }
  1185. return aborted;
  1186. }
  1187. get done() {
  1188. return this.pendingKeysSet.size === 0;
  1189. }
  1190. get unwrappedData() {
  1191. invariant(this.data !== null && this.done, "Can only unwrap data on initialized and settled deferreds");
  1192. return Object.entries(this.data).reduce((acc, _ref3) => {
  1193. let [key, value] = _ref3;
  1194. return Object.assign(acc, {
  1195. [key]: unwrapTrackedPromise(value)
  1196. });
  1197. }, {});
  1198. }
  1199. get pendingKeys() {
  1200. return Array.from(this.pendingKeysSet);
  1201. }
  1202. }
  1203. function isTrackedPromise(value) {
  1204. return value instanceof Promise && value._tracked === true;
  1205. }
  1206. function unwrapTrackedPromise(value) {
  1207. if (!isTrackedPromise(value)) {
  1208. return value;
  1209. }
  1210. if (value._error) {
  1211. throw value._error;
  1212. }
  1213. return value._data;
  1214. }
  1215. /**
  1216. * @deprecated The `defer` method is deprecated in favor of returning raw
  1217. * objects. This method will be removed in v7.
  1218. */
  1219. const defer = function defer(data, init) {
  1220. if (init === void 0) {
  1221. init = {};
  1222. }
  1223. let responseInit = typeof init === "number" ? {
  1224. status: init
  1225. } : init;
  1226. return new DeferredData(data, responseInit);
  1227. };
  1228. /**
  1229. * A redirect response. Sets the status code and the `Location` header.
  1230. * Defaults to "302 Found".
  1231. */
  1232. const redirect = function redirect(url, init) {
  1233. if (init === void 0) {
  1234. init = 302;
  1235. }
  1236. let responseInit = init;
  1237. if (typeof responseInit === "number") {
  1238. responseInit = {
  1239. status: responseInit
  1240. };
  1241. } else if (typeof responseInit.status === "undefined") {
  1242. responseInit.status = 302;
  1243. }
  1244. let headers = new Headers(responseInit.headers);
  1245. headers.set("Location", url);
  1246. return new Response(null, _extends({}, responseInit, {
  1247. headers
  1248. }));
  1249. };
  1250. /**
  1251. * A redirect response that will force a document reload to the new location.
  1252. * Sets the status code and the `Location` header.
  1253. * Defaults to "302 Found".
  1254. */
  1255. const redirectDocument = (url, init) => {
  1256. let response = redirect(url, init);
  1257. response.headers.set("X-Remix-Reload-Document", "true");
  1258. return response;
  1259. };
  1260. /**
  1261. * A redirect response that will perform a `history.replaceState` instead of a
  1262. * `history.pushState` for client-side navigation redirects.
  1263. * Sets the status code and the `Location` header.
  1264. * Defaults to "302 Found".
  1265. */
  1266. const replace = (url, init) => {
  1267. let response = redirect(url, init);
  1268. response.headers.set("X-Remix-Replace", "true");
  1269. return response;
  1270. };
  1271. /**
  1272. * @private
  1273. * Utility class we use to hold auto-unwrapped 4xx/5xx Response bodies
  1274. *
  1275. * We don't export the class for public use since it's an implementation
  1276. * detail, but we export the interface above so folks can build their own
  1277. * abstractions around instances via isRouteErrorResponse()
  1278. */
  1279. class ErrorResponseImpl {
  1280. constructor(status, statusText, data, internal) {
  1281. if (internal === void 0) {
  1282. internal = false;
  1283. }
  1284. this.status = status;
  1285. this.statusText = statusText || "";
  1286. this.internal = internal;
  1287. if (data instanceof Error) {
  1288. this.data = data.toString();
  1289. this.error = data;
  1290. } else {
  1291. this.data = data;
  1292. }
  1293. }
  1294. }
  1295. /**
  1296. * Check if the given error is an ErrorResponse generated from a 4xx/5xx
  1297. * Response thrown from an action/loader
  1298. */
  1299. function isRouteErrorResponse(error) {
  1300. return error != null && typeof error.status === "number" && typeof error.statusText === "string" && typeof error.internal === "boolean" && "data" in error;
  1301. }
  1302. const validMutationMethodsArr = ["post", "put", "patch", "delete"];
  1303. const validMutationMethods = new Set(validMutationMethodsArr);
  1304. const validRequestMethodsArr = ["get", ...validMutationMethodsArr];
  1305. const validRequestMethods = new Set(validRequestMethodsArr);
  1306. const redirectStatusCodes = new Set([301, 302, 303, 307, 308]);
  1307. const redirectPreserveMethodStatusCodes = new Set([307, 308]);
  1308. const IDLE_NAVIGATION = {
  1309. state: "idle",
  1310. location: undefined,
  1311. formMethod: undefined,
  1312. formAction: undefined,
  1313. formEncType: undefined,
  1314. formData: undefined,
  1315. json: undefined,
  1316. text: undefined
  1317. };
  1318. const IDLE_FETCHER = {
  1319. state: "idle",
  1320. data: undefined,
  1321. formMethod: undefined,
  1322. formAction: undefined,
  1323. formEncType: undefined,
  1324. formData: undefined,
  1325. json: undefined,
  1326. text: undefined
  1327. };
  1328. const IDLE_BLOCKER = {
  1329. state: "unblocked",
  1330. proceed: undefined,
  1331. reset: undefined,
  1332. location: undefined
  1333. };
  1334. const ABSOLUTE_URL_REGEX = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i;
  1335. const defaultMapRouteProperties = route => ({
  1336. hasErrorBoundary: Boolean(route.hasErrorBoundary)
  1337. });
  1338. const TRANSITIONS_STORAGE_KEY = "remix-router-transitions";
  1339. //#endregion
  1340. ////////////////////////////////////////////////////////////////////////////////
  1341. //#region createRouter
  1342. ////////////////////////////////////////////////////////////////////////////////
  1343. /**
  1344. * Create a router and listen to history POP navigations
  1345. */
  1346. function createRouter(init) {
  1347. const routerWindow = init.window ? init.window : typeof window !== "undefined" ? window : undefined;
  1348. const isBrowser = typeof routerWindow !== "undefined" && typeof routerWindow.document !== "undefined" && typeof routerWindow.document.createElement !== "undefined";
  1349. const isServer = !isBrowser;
  1350. invariant(init.routes.length > 0, "You must provide a non-empty routes array to createRouter");
  1351. let mapRouteProperties;
  1352. if (init.mapRouteProperties) {
  1353. mapRouteProperties = init.mapRouteProperties;
  1354. } else if (init.detectErrorBoundary) {
  1355. // If they are still using the deprecated version, wrap it with the new API
  1356. let detectErrorBoundary = init.detectErrorBoundary;
  1357. mapRouteProperties = route => ({
  1358. hasErrorBoundary: detectErrorBoundary(route)
  1359. });
  1360. } else {
  1361. mapRouteProperties = defaultMapRouteProperties;
  1362. }
  1363. // Routes keyed by ID
  1364. let manifest = {};
  1365. // Routes in tree format for matching
  1366. let dataRoutes = convertRoutesToDataRoutes(init.routes, mapRouteProperties, undefined, manifest);
  1367. let inFlightDataRoutes;
  1368. let basename = init.basename || "/";
  1369. let dataStrategyImpl = init.dataStrategy || defaultDataStrategy;
  1370. let patchRoutesOnNavigationImpl = init.patchRoutesOnNavigation;
  1371. // Config driven behavior flags
  1372. let future = _extends({
  1373. v7_fetcherPersist: false,
  1374. v7_normalizeFormMethod: false,
  1375. v7_partialHydration: false,
  1376. v7_prependBasename: false,
  1377. v7_relativeSplatPath: false,
  1378. v7_skipActionErrorRevalidation: false
  1379. }, init.future);
  1380. // Cleanup function for history
  1381. let unlistenHistory = null;
  1382. // Externally-provided functions to call on all state changes
  1383. let subscribers = new Set();
  1384. // Externally-provided object to hold scroll restoration locations during routing
  1385. let savedScrollPositions = null;
  1386. // Externally-provided function to get scroll restoration keys
  1387. let getScrollRestorationKey = null;
  1388. // Externally-provided function to get current scroll position
  1389. let getScrollPosition = null;
  1390. // One-time flag to control the initial hydration scroll restoration. Because
  1391. // we don't get the saved positions from <ScrollRestoration /> until _after_
  1392. // the initial render, we need to manually trigger a separate updateState to
  1393. // send along the restoreScrollPosition
  1394. // Set to true if we have `hydrationData` since we assume we were SSR'd and that
  1395. // SSR did the initial scroll restoration.
  1396. let initialScrollRestored = init.hydrationData != null;
  1397. let initialMatches = matchRoutes(dataRoutes, init.history.location, basename);
  1398. let initialMatchesIsFOW = false;
  1399. let initialErrors = null;
  1400. if (initialMatches == null && !patchRoutesOnNavigationImpl) {
  1401. // If we do not match a user-provided-route, fall back to the root
  1402. // to allow the error boundary to take over
  1403. let error = getInternalRouterError(404, {
  1404. pathname: init.history.location.pathname
  1405. });
  1406. let {
  1407. matches,
  1408. route
  1409. } = getShortCircuitMatches(dataRoutes);
  1410. initialMatches = matches;
  1411. initialErrors = {
  1412. [route.id]: error
  1413. };
  1414. }
  1415. // In SPA apps, if the user provided a patchRoutesOnNavigation implementation and
  1416. // our initial match is a splat route, clear them out so we run through lazy
  1417. // discovery on hydration in case there's a more accurate lazy route match.
  1418. // In SSR apps (with `hydrationData`), we expect that the server will send
  1419. // up the proper matched routes so we don't want to run lazy discovery on
  1420. // initial hydration and want to hydrate into the splat route.
  1421. if (initialMatches && !init.hydrationData) {
  1422. let fogOfWar = checkFogOfWar(initialMatches, dataRoutes, init.history.location.pathname);
  1423. if (fogOfWar.active) {
  1424. initialMatches = null;
  1425. }
  1426. }
  1427. let initialized;
  1428. if (!initialMatches) {
  1429. initialized = false;
  1430. initialMatches = [];
  1431. // If partial hydration and fog of war is enabled, we will be running
  1432. // `patchRoutesOnNavigation` during hydration so include any partial matches as
  1433. // the initial matches so we can properly render `HydrateFallback`'s
  1434. if (future.v7_partialHydration) {
  1435. let fogOfWar = checkFogOfWar(null, dataRoutes, init.history.location.pathname);
  1436. if (fogOfWar.active && fogOfWar.matches) {
  1437. initialMatchesIsFOW = true;
  1438. initialMatches = fogOfWar.matches;
  1439. }
  1440. }
  1441. } else if (initialMatches.some(m => m.route.lazy)) {
  1442. // All initialMatches need to be loaded before we're ready. If we have lazy
  1443. // functions around still then we'll need to run them in initialize()
  1444. initialized = false;
  1445. } else if (!initialMatches.some(m => m.route.loader)) {
  1446. // If we've got no loaders to run, then we're good to go
  1447. initialized = true;
  1448. } else if (future.v7_partialHydration) {
  1449. // If partial hydration is enabled, we're initialized so long as we were
  1450. // provided with hydrationData for every route with a loader, and no loaders
  1451. // were marked for explicit hydration
  1452. let loaderData = init.hydrationData ? init.hydrationData.loaderData : null;
  1453. let errors = init.hydrationData ? init.hydrationData.errors : null;
  1454. // If errors exist, don't consider routes below the boundary
  1455. if (errors) {
  1456. let idx = initialMatches.findIndex(m => errors[m.route.id] !== undefined);
  1457. initialized = initialMatches.slice(0, idx + 1).every(m => !shouldLoadRouteOnHydration(m.route, loaderData, errors));
  1458. } else {
  1459. initialized = initialMatches.every(m => !shouldLoadRouteOnHydration(m.route, loaderData, errors));
  1460. }
  1461. } else {
  1462. // Without partial hydration - we're initialized if we were provided any
  1463. // hydrationData - which is expected to be complete
  1464. initialized = init.hydrationData != null;
  1465. }
  1466. let router;
  1467. let state = {
  1468. historyAction: init.history.action,
  1469. location: init.history.location,
  1470. matches: initialMatches,
  1471. initialized,
  1472. navigation: IDLE_NAVIGATION,
  1473. // Don't restore on initial updateState() if we were SSR'd
  1474. restoreScrollPosition: init.hydrationData != null ? false : null,
  1475. preventScrollReset: false,
  1476. revalidation: "idle",
  1477. loaderData: init.hydrationData && init.hydrationData.loaderData || {},
  1478. actionData: init.hydrationData && init.hydrationData.actionData || null,
  1479. errors: init.hydrationData && init.hydrationData.errors || initialErrors,
  1480. fetchers: new Map(),
  1481. blockers: new Map()
  1482. };
  1483. // -- Stateful internal variables to manage navigations --
  1484. // Current navigation in progress (to be committed in completeNavigation)
  1485. let pendingAction = Action.Pop;
  1486. // Should the current navigation prevent the scroll reset if scroll cannot
  1487. // be restored?
  1488. let pendingPreventScrollReset = false;
  1489. // AbortController for the active navigation
  1490. let pendingNavigationController;
  1491. // Should the current navigation enable document.startViewTransition?
  1492. let pendingViewTransitionEnabled = false;
  1493. // Store applied view transitions so we can apply them on POP
  1494. let appliedViewTransitions = new Map();
  1495. // Cleanup function for persisting applied transitions to sessionStorage
  1496. let removePageHideEventListener = null;
  1497. // We use this to avoid touching history in completeNavigation if a
  1498. // revalidation is entirely uninterrupted
  1499. let isUninterruptedRevalidation = false;
  1500. // Use this internal flag to force revalidation of all loaders:
  1501. // - submissions (completed or interrupted)
  1502. // - useRevalidator()
  1503. // - X-Remix-Revalidate (from redirect)
  1504. let isRevalidationRequired = false;
  1505. // Use this internal array to capture routes that require revalidation due
  1506. // to a cancelled deferred on action submission
  1507. let cancelledDeferredRoutes = [];
  1508. // Use this internal array to capture fetcher loads that were cancelled by an
  1509. // action navigation and require revalidation
  1510. let cancelledFetcherLoads = new Set();
  1511. // AbortControllers for any in-flight fetchers
  1512. let fetchControllers = new Map();
  1513. // Track loads based on the order in which they started
  1514. let incrementingLoadId = 0;
  1515. // Track the outstanding pending navigation data load to be compared against
  1516. // the globally incrementing load when a fetcher load lands after a completed
  1517. // navigation
  1518. let pendingNavigationLoadId = -1;
  1519. // Fetchers that triggered data reloads as a result of their actions
  1520. let fetchReloadIds = new Map();
  1521. // Fetchers that triggered redirect navigations
  1522. let fetchRedirectIds = new Set();
  1523. // Most recent href/match for fetcher.load calls for fetchers
  1524. let fetchLoadMatches = new Map();
  1525. // Ref-count mounted fetchers so we know when it's ok to clean them up
  1526. let activeFetchers = new Map();
  1527. // Fetchers that have requested a delete when using v7_fetcherPersist,
  1528. // they'll be officially removed after they return to idle
  1529. let deletedFetchers = new Set();
  1530. // Store DeferredData instances for active route matches. When a
  1531. // route loader returns defer() we stick one in here. Then, when a nested
  1532. // promise resolves we update loaderData. If a new navigation starts we
  1533. // cancel active deferreds for eliminated routes.
  1534. let activeDeferreds = new Map();
  1535. // Store blocker functions in a separate Map outside of router state since
  1536. // we don't need to update UI state if they change
  1537. let blockerFunctions = new Map();
  1538. // Flag to ignore the next history update, so we can revert the URL change on
  1539. // a POP navigation that was blocked by the user without touching router state
  1540. let unblockBlockerHistoryUpdate = undefined;
  1541. // Initialize the router, all side effects should be kicked off from here.
  1542. // Implemented as a Fluent API for ease of:
  1543. // let router = createRouter(init).initialize();
  1544. function initialize() {
  1545. // If history informs us of a POP navigation, start the navigation but do not update
  1546. // state. We'll update our own state once the navigation completes
  1547. unlistenHistory = init.history.listen(_ref => {
  1548. let {
  1549. action: historyAction,
  1550. location,
  1551. delta
  1552. } = _ref;
  1553. // Ignore this event if it was just us resetting the URL from a
  1554. // blocked POP navigation
  1555. if (unblockBlockerHistoryUpdate) {
  1556. unblockBlockerHistoryUpdate();
  1557. unblockBlockerHistoryUpdate = undefined;
  1558. return;
  1559. }
  1560. warning(blockerFunctions.size === 0 || delta != null, "You are trying to use a blocker on a POP navigation to a location " + "that was not created by @remix-run/router. This will fail silently in " + "production. This can happen if you are navigating outside the router " + "via `window.history.pushState`/`window.location.hash` instead of using " + "router navigation APIs. This can also happen if you are using " + "createHashRouter and the user manually changes the URL.");
  1561. let blockerKey = shouldBlockNavigation({
  1562. currentLocation: state.location,
  1563. nextLocation: location,
  1564. historyAction
  1565. });
  1566. if (blockerKey && delta != null) {
  1567. // Restore the URL to match the current UI, but don't update router state
  1568. let nextHistoryUpdatePromise = new Promise(resolve => {
  1569. unblockBlockerHistoryUpdate = resolve;
  1570. });
  1571. init.history.go(delta * -1);
  1572. // Put the blocker into a blocked state
  1573. updateBlocker(blockerKey, {
  1574. state: "blocked",
  1575. location,
  1576. proceed() {
  1577. updateBlocker(blockerKey, {
  1578. state: "proceeding",
  1579. proceed: undefined,
  1580. reset: undefined,
  1581. location
  1582. });
  1583. // Re-do the same POP navigation we just blocked, after the url
  1584. // restoration is also complete. See:
  1585. // https://github.com/remix-run/react-router/issues/11613
  1586. nextHistoryUpdatePromise.then(() => init.history.go(delta));
  1587. },
  1588. reset() {
  1589. let blockers = new Map(state.blockers);
  1590. blockers.set(blockerKey, IDLE_BLOCKER);
  1591. updateState({
  1592. blockers
  1593. });
  1594. }
  1595. });
  1596. return;
  1597. }
  1598. return startNavigation(historyAction, location);
  1599. });
  1600. if (isBrowser) {
  1601. // FIXME: This feels gross. How can we cleanup the lines between
  1602. // scrollRestoration/appliedTransitions persistance?
  1603. restoreAppliedTransitions(routerWindow, appliedViewTransitions);
  1604. let _saveAppliedTransitions = () => persistAppliedTransitions(routerWindow, appliedViewTransitions);
  1605. routerWindow.addEventListener("pagehide", _saveAppliedTransitions);
  1606. removePageHideEventListener = () => routerWindow.removeEventListener("pagehide", _saveAppliedTransitions);
  1607. }
  1608. // Kick off initial data load if needed. Use Pop to avoid modifying history
  1609. // Note we don't do any handling of lazy here. For SPA's it'll get handled
  1610. // in the normal navigation flow. For SSR it's expected that lazy modules are
  1611. // resolved prior to router creation since we can't go into a fallbackElement
  1612. // UI for SSR'd apps
  1613. if (!state.initialized) {
  1614. startNavigation(Action.Pop, state.location, {
  1615. initialHydration: true
  1616. });
  1617. }
  1618. return router;
  1619. }
  1620. // Clean up a router and it's side effects
  1621. function dispose() {
  1622. if (unlistenHistory) {
  1623. unlistenHistory();
  1624. }
  1625. if (removePageHideEventListener) {
  1626. removePageHideEventListener();
  1627. }
  1628. subscribers.clear();
  1629. pendingNavigationController && pendingNavigationController.abort();
  1630. state.fetchers.forEach((_, key) => deleteFetcher(key));
  1631. state.blockers.forEach((_, key) => deleteBlocker(key));
  1632. }
  1633. // Subscribe to state updates for the router
  1634. function subscribe(fn) {
  1635. subscribers.add(fn);
  1636. return () => subscribers.delete(fn);
  1637. }
  1638. // Update our state and notify the calling context of the change
  1639. function updateState(newState, opts) {
  1640. if (opts === void 0) {
  1641. opts = {};
  1642. }
  1643. state = _extends({}, state, newState);
  1644. // Prep fetcher cleanup so we can tell the UI which fetcher data entries
  1645. // can be removed
  1646. let completedFetchers = [];
  1647. let deletedFetchersKeys = [];
  1648. if (future.v7_fetcherPersist) {
  1649. state.fetchers.forEach((fetcher, key) => {
  1650. if (fetcher.state === "idle") {
  1651. if (deletedFetchers.has(key)) {
  1652. // Unmounted from the UI and can be totally removed
  1653. deletedFetchersKeys.push(key);
  1654. } else {
  1655. // Returned to idle but still mounted in the UI, so semi-remains for
  1656. // revalidations and such
  1657. completedFetchers.push(key);
  1658. }
  1659. }
  1660. });
  1661. }
  1662. // Remove any lingering deleted fetchers that have already been removed
  1663. // from state.fetchers
  1664. deletedFetchers.forEach(key => {
  1665. if (!state.fetchers.has(key) && !fetchControllers.has(key)) {
  1666. deletedFetchersKeys.push(key);
  1667. }
  1668. });
  1669. // Iterate over a local copy so that if flushSync is used and we end up
  1670. // removing and adding a new subscriber due to the useCallback dependencies,
  1671. // we don't get ourselves into a loop calling the new subscriber immediately
  1672. [...subscribers].forEach(subscriber => subscriber(state, {
  1673. deletedFetchers: deletedFetchersKeys,
  1674. viewTransitionOpts: opts.viewTransitionOpts,
  1675. flushSync: opts.flushSync === true
  1676. }));
  1677. // Remove idle fetchers from state since we only care about in-flight fetchers.
  1678. if (future.v7_fetcherPersist) {
  1679. completedFetchers.forEach(key => state.fetchers.delete(key));
  1680. deletedFetchersKeys.forEach(key => deleteFetcher(key));
  1681. } else {
  1682. // We already called deleteFetcher() on these, can remove them from this
  1683. // Set now that we've handed the keys off to the data layer
  1684. deletedFetchersKeys.forEach(key => deletedFetchers.delete(key));
  1685. }
  1686. }
  1687. // Complete a navigation returning the state.navigation back to the IDLE_NAVIGATION
  1688. // and setting state.[historyAction/location/matches] to the new route.
  1689. // - Location is a required param
  1690. // - Navigation will always be set to IDLE_NAVIGATION
  1691. // - Can pass any other state in newState
  1692. function completeNavigation(location, newState, _temp) {
  1693. var _location$state, _location$state2;
  1694. let {
  1695. flushSync
  1696. } = _temp === void 0 ? {} : _temp;
  1697. // Deduce if we're in a loading/actionReload state:
  1698. // - We have committed actionData in the store
  1699. // - The current navigation was a mutation submission
  1700. // - We're past the submitting state and into the loading state
  1701. // - The location being loaded is not the result of a redirect
  1702. let isActionReload = state.actionData != null && state.navigation.formMethod != null && isMutationMethod(state.navigation.formMethod) && state.navigation.state === "loading" && ((_location$state = location.state) == null ? void 0 : _location$state._isRedirect) !== true;
  1703. let actionData;
  1704. if (newState.actionData) {
  1705. if (Object.keys(newState.actionData).length > 0) {
  1706. actionData = newState.actionData;
  1707. } else {
  1708. // Empty actionData -> clear prior actionData due to an action error
  1709. actionData = null;
  1710. }
  1711. } else if (isActionReload) {
  1712. // Keep the current data if we're wrapping up the action reload
  1713. actionData = state.actionData;
  1714. } else {
  1715. // Clear actionData on any other completed navigations
  1716. actionData = null;
  1717. }
  1718. // Always preserve any existing loaderData from re-used routes
  1719. let loaderData = newState.loaderData ? mergeLoaderData(state.loaderData, newState.loaderData, newState.matches || [], newState.errors) : state.loaderData;
  1720. // On a successful navigation we can assume we got through all blockers
  1721. // so we can start fresh
  1722. let blockers = state.blockers;
  1723. if (blockers.size > 0) {
  1724. blockers = new Map(blockers);
  1725. blockers.forEach((_, k) => blockers.set(k, IDLE_BLOCKER));
  1726. }
  1727. // Always respect the user flag. Otherwise don't reset on mutation
  1728. // submission navigations unless they redirect
  1729. let preventScrollReset = pendingPreventScrollReset === true || state.navigation.formMethod != null && isMutationMethod(state.navigation.formMethod) && ((_location$state2 = location.state) == null ? void 0 : _location$state2._isRedirect) !== true;
  1730. // Commit any in-flight routes at the end of the HMR revalidation "navigation"
  1731. if (inFlightDataRoutes) {
  1732. dataRoutes = inFlightDataRoutes;
  1733. inFlightDataRoutes = undefined;
  1734. }
  1735. if (isUninterruptedRevalidation) ; else if (pendingAction === Action.Pop) ; else if (pendingAction === Action.Push) {
  1736. init.history.push(location, location.state);
  1737. } else if (pendingAction === Action.Replace) {
  1738. init.history.replace(location, location.state);
  1739. }
  1740. let viewTransitionOpts;
  1741. // On POP, enable transitions if they were enabled on the original navigation
  1742. if (pendingAction === Action.Pop) {
  1743. // Forward takes precedence so they behave like the original navigation
  1744. let priorPaths = appliedViewTransitions.get(state.location.pathname);
  1745. if (priorPaths && priorPaths.has(location.pathname)) {
  1746. viewTransitionOpts = {
  1747. currentLocation: state.location,
  1748. nextLocation: location
  1749. };
  1750. } else if (appliedViewTransitions.has(location.pathname)) {
  1751. // If we don't have a previous forward nav, assume we're popping back to
  1752. // the new location and enable if that location previously enabled
  1753. viewTransitionOpts = {
  1754. currentLocation: location,
  1755. nextLocation: state.location
  1756. };
  1757. }
  1758. } else if (pendingViewTransitionEnabled) {
  1759. // Store the applied transition on PUSH/REPLACE
  1760. let toPaths = appliedViewTransitions.get(state.location.pathname);
  1761. if (toPaths) {
  1762. toPaths.add(location.pathname);
  1763. } else {
  1764. toPaths = new Set([location.pathname]);
  1765. appliedViewTransitions.set(state.location.pathname, toPaths);
  1766. }
  1767. viewTransitionOpts = {
  1768. currentLocation: state.location,
  1769. nextLocation: location
  1770. };
  1771. }
  1772. updateState(_extends({}, newState, {
  1773. actionData,
  1774. loaderData,
  1775. historyAction: pendingAction,
  1776. location,
  1777. initialized: true,
  1778. navigation: IDLE_NAVIGATION,
  1779. revalidation: "idle",
  1780. restoreScrollPosition: getSavedScrollPosition(location, newState.matches || state.matches),
  1781. preventScrollReset,
  1782. blockers
  1783. }), {
  1784. viewTransitionOpts,
  1785. flushSync: flushSync === true
  1786. });
  1787. // Reset stateful navigation vars
  1788. pendingAction = Action.Pop;
  1789. pendingPreventScrollReset = false;
  1790. pendingViewTransitionEnabled = false;
  1791. isUninterruptedRevalidation = false;
  1792. isRevalidationRequired = false;
  1793. cancelledDeferredRoutes = [];
  1794. }
  1795. // Trigger a navigation event, which can either be a numerical POP or a PUSH
  1796. // replace with an optional submission
  1797. async function navigate(to, opts) {
  1798. if (typeof to === "number") {
  1799. init.history.go(to);
  1800. return;
  1801. }
  1802. let normalizedPath = normalizeTo(state.location, state.matches, basename, future.v7_prependBasename, to, future.v7_relativeSplatPath, opts == null ? void 0 : opts.fromRouteId, opts == null ? void 0 : opts.relative);
  1803. let {
  1804. path,
  1805. submission,
  1806. error
  1807. } = normalizeNavigateOptions(future.v7_normalizeFormMethod, false, normalizedPath, opts);
  1808. let currentLocation = state.location;
  1809. let nextLocation = createLocation(state.location, path, opts && opts.state);
  1810. // When using navigate as a PUSH/REPLACE we aren't reading an already-encoded
  1811. // URL from window.location, so we need to encode it here so the behavior
  1812. // remains the same as POP and non-data-router usages. new URL() does all
  1813. // the same encoding we'd get from a history.pushState/window.location read
  1814. // without having to touch history
  1815. nextLocation = _extends({}, nextLocation, init.history.encodeLocation(nextLocation));
  1816. let userReplace = opts && opts.replace != null ? opts.replace : undefined;
  1817. let historyAction = Action.Push;
  1818. if (userReplace === true) {
  1819. historyAction = Action.Replace;
  1820. } else if (userReplace === false) ; else if (submission != null && isMutationMethod(submission.formMethod) && submission.formAction === state.location.pathname + state.location.search) {
  1821. // By default on submissions to the current location we REPLACE so that
  1822. // users don't have to double-click the back button to get to the prior
  1823. // location. If the user redirects to a different location from the
  1824. // action/loader this will be ignored and the redirect will be a PUSH
  1825. historyAction = Action.Replace;
  1826. }
  1827. let preventScrollReset = opts && "preventScrollReset" in opts ? opts.preventScrollReset === true : undefined;
  1828. let flushSync = (opts && opts.flushSync) === true;
  1829. let blockerKey = shouldBlockNavigation({
  1830. currentLocation,
  1831. nextLocation,
  1832. historyAction
  1833. });
  1834. if (blockerKey) {
  1835. // Put the blocker into a blocked state
  1836. updateBlocker(blockerKey, {
  1837. state: "blocked",
  1838. location: nextLocation,
  1839. proceed() {
  1840. updateBlocker(blockerKey, {
  1841. state: "proceeding",
  1842. proceed: undefined,
  1843. reset: undefined,
  1844. location: nextLocation
  1845. });
  1846. // Send the same navigation through
  1847. navigate(to, opts);
  1848. },
  1849. reset() {
  1850. let blockers = new Map(state.blockers);
  1851. blockers.set(blockerKey, IDLE_BLOCKER);
  1852. updateState({
  1853. blockers
  1854. });
  1855. }
  1856. });
  1857. return;
  1858. }
  1859. return await startNavigation(historyAction, nextLocation, {
  1860. submission,
  1861. // Send through the formData serialization error if we have one so we can
  1862. // render at the right error boundary after we match routes
  1863. pendingError: error,
  1864. preventScrollReset,
  1865. replace: opts && opts.replace,
  1866. enableViewTransition: opts && opts.viewTransition,
  1867. flushSync
  1868. });
  1869. }
  1870. // Revalidate all current loaders. If a navigation is in progress or if this
  1871. // is interrupted by a navigation, allow this to "succeed" by calling all
  1872. // loaders during the next loader round
  1873. function revalidate() {
  1874. interruptActiveLoads();
  1875. updateState({
  1876. revalidation: "loading"
  1877. });
  1878. // If we're currently submitting an action, we don't need to start a new
  1879. // navigation, we'll just let the follow up loader execution call all loaders
  1880. if (state.navigation.state === "submitting") {
  1881. return;
  1882. }
  1883. // If we're currently in an idle state, start a new navigation for the current
  1884. // action/location and mark it as uninterrupted, which will skip the history
  1885. // update in completeNavigation
  1886. if (state.navigation.state === "idle") {
  1887. startNavigation(state.historyAction, state.location, {
  1888. startUninterruptedRevalidation: true
  1889. });
  1890. return;
  1891. }
  1892. // Otherwise, if we're currently in a loading state, just start a new
  1893. // navigation to the navigation.location but do not trigger an uninterrupted
  1894. // revalidation so that history correctly updates once the navigation completes
  1895. startNavigation(pendingAction || state.historyAction, state.navigation.location, {
  1896. overrideNavigation: state.navigation,
  1897. // Proxy through any rending view transition
  1898. enableViewTransition: pendingViewTransitionEnabled === true
  1899. });
  1900. }
  1901. // Start a navigation to the given action/location. Can optionally provide a
  1902. // overrideNavigation which will override the normalLoad in the case of a redirect
  1903. // navigation
  1904. async function startNavigation(historyAction, location, opts) {
  1905. // Abort any in-progress navigations and start a new one. Unset any ongoing
  1906. // uninterrupted revalidations unless told otherwise, since we want this
  1907. // new navigation to update history normally
  1908. pendingNavigationController && pendingNavigationController.abort();
  1909. pendingNavigationController = null;
  1910. pendingAction = historyAction;
  1911. isUninterruptedRevalidation = (opts && opts.startUninterruptedRevalidation) === true;
  1912. // Save the current scroll position every time we start a new navigation,
  1913. // and track whether we should reset scroll on completion
  1914. saveScrollPosition(state.location, state.matches);
  1915. pendingPreventScrollReset = (opts && opts.preventScrollReset) === true;
  1916. pendingViewTransitionEnabled = (opts && opts.enableViewTransition) === true;
  1917. let routesToUse = inFlightDataRoutes || dataRoutes;
  1918. let loadingNavigation = opts && opts.overrideNavigation;
  1919. let matches = opts != null && opts.initialHydration && state.matches && state.matches.length > 0 && !initialMatchesIsFOW ?
  1920. // `matchRoutes()` has already been called if we're in here via `router.initialize()`
  1921. state.matches : matchRoutes(routesToUse, location, basename);
  1922. let flushSync = (opts && opts.flushSync) === true;
  1923. // Short circuit if it's only a hash change and not a revalidation or
  1924. // mutation submission.
  1925. //
  1926. // Ignore on initial page loads because since the initial hydration will always
  1927. // be "same hash". For example, on /page#hash and submit a <Form method="post">
  1928. // which will default to a navigation to /page
  1929. if (matches && state.initialized && !isRevalidationRequired && isHashChangeOnly(state.location, location) && !(opts && opts.submission && isMutationMethod(opts.submission.formMethod))) {
  1930. completeNavigation(location, {
  1931. matches
  1932. }, {
  1933. flushSync
  1934. });
  1935. return;
  1936. }
  1937. let fogOfWar = checkFogOfWar(matches, routesToUse, location.pathname);
  1938. if (fogOfWar.active && fogOfWar.matches) {
  1939. matches = fogOfWar.matches;
  1940. }
  1941. // Short circuit with a 404 on the root error boundary if we match nothing
  1942. if (!matches) {
  1943. let {
  1944. error,
  1945. notFoundMatches,
  1946. route
  1947. } = handleNavigational404(location.pathname);
  1948. completeNavigation(location, {
  1949. matches: notFoundMatches,
  1950. loaderData: {},
  1951. errors: {
  1952. [route.id]: error
  1953. }
  1954. }, {
  1955. flushSync
  1956. });
  1957. return;
  1958. }
  1959. // Create a controller/Request for this navigation
  1960. pendingNavigationController = new AbortController();
  1961. let request = createClientSideRequest(init.history, location, pendingNavigationController.signal, opts && opts.submission);
  1962. let pendingActionResult;
  1963. if (opts && opts.pendingError) {
  1964. // If we have a pendingError, it means the user attempted a GET submission
  1965. // with binary FormData so assign here and skip to handleLoaders. That
  1966. // way we handle calling loaders above the boundary etc. It's not really
  1967. // different from an actionError in that sense.
  1968. pendingActionResult = [findNearestBoundary(matches).route.id, {
  1969. type: ResultType.error,
  1970. error: opts.pendingError
  1971. }];
  1972. } else if (opts && opts.submission && isMutationMethod(opts.submission.formMethod)) {
  1973. // Call action if we received an action submission
  1974. let actionResult = await handleAction(request, location, opts.submission, matches, fogOfWar.active, {
  1975. replace: opts.replace,
  1976. flushSync
  1977. });
  1978. if (actionResult.shortCircuited) {
  1979. return;
  1980. }
  1981. // If we received a 404 from handleAction, it's because we couldn't lazily
  1982. // discover the destination route so we don't want to call loaders
  1983. if (actionResult.pendingActionResult) {
  1984. let [routeId, result] = actionResult.pendingActionResult;
  1985. if (isErrorResult(result) && isRouteErrorResponse(result.error) && result.error.status === 404) {
  1986. pendingNavigationController = null;
  1987. completeNavigation(location, {
  1988. matches: actionResult.matches,
  1989. loaderData: {},
  1990. errors: {
  1991. [routeId]: result.error
  1992. }
  1993. });
  1994. return;
  1995. }
  1996. }
  1997. matches = actionResult.matches || matches;
  1998. pendingActionResult = actionResult.pendingActionResult;
  1999. loadingNavigation = getLoadingNavigation(location, opts.submission);
  2000. flushSync = false;
  2001. // No need to do fog of war matching again on loader execution
  2002. fogOfWar.active = false;
  2003. // Create a GET request for the loaders
  2004. request = createClientSideRequest(init.history, request.url, request.signal);
  2005. }
  2006. // Call loaders
  2007. let {
  2008. shortCircuited,
  2009. matches: updatedMatches,
  2010. loaderData,
  2011. errors
  2012. } = await handleLoaders(request, location, matches, fogOfWar.active, loadingNavigation, opts && opts.submission, opts && opts.fetcherSubmission, opts && opts.replace, opts && opts.initialHydration === true, flushSync, pendingActionResult);
  2013. if (shortCircuited) {
  2014. return;
  2015. }
  2016. // Clean up now that the action/loaders have completed. Don't clean up if
  2017. // we short circuited because pendingNavigationController will have already
  2018. // been assigned to a new controller for the next navigation
  2019. pendingNavigationController = null;
  2020. completeNavigation(location, _extends({
  2021. matches: updatedMatches || matches
  2022. }, getActionDataForCommit(pendingActionResult), {
  2023. loaderData,
  2024. errors
  2025. }));
  2026. }
  2027. // Call the action matched by the leaf route for this navigation and handle
  2028. // redirects/errors
  2029. async function handleAction(request, location, submission, matches, isFogOfWar, opts) {
  2030. if (opts === void 0) {
  2031. opts = {};
  2032. }
  2033. interruptActiveLoads();
  2034. // Put us in a submitting state
  2035. let navigation = getSubmittingNavigation(location, submission);
  2036. updateState({
  2037. navigation
  2038. }, {
  2039. flushSync: opts.flushSync === true
  2040. });
  2041. if (isFogOfWar) {
  2042. let discoverResult = await discoverRoutes(matches, location.pathname, request.signal);
  2043. if (discoverResult.type === "aborted") {
  2044. return {
  2045. shortCircuited: true
  2046. };
  2047. } else if (discoverResult.type === "error") {
  2048. let boundaryId = findNearestBoundary(discoverResult.partialMatches).route.id;
  2049. return {
  2050. matches: discoverResult.partialMatches,
  2051. pendingActionResult: [boundaryId, {
  2052. type: ResultType.error,
  2053. error: discoverResult.error
  2054. }]
  2055. };
  2056. } else if (!discoverResult.matches) {
  2057. let {
  2058. notFoundMatches,
  2059. error,
  2060. route
  2061. } = handleNavigational404(location.pathname);
  2062. return {
  2063. matches: notFoundMatches,
  2064. pendingActionResult: [route.id, {
  2065. type: ResultType.error,
  2066. error
  2067. }]
  2068. };
  2069. } else {
  2070. matches = discoverResult.matches;
  2071. }
  2072. }
  2073. // Call our action and get the result
  2074. let result;
  2075. let actionMatch = getTargetMatch(matches, location);
  2076. if (!actionMatch.route.action && !actionMatch.route.lazy) {
  2077. result = {
  2078. type: ResultType.error,
  2079. error: getInternalRouterError(405, {
  2080. method: request.method,
  2081. pathname: location.pathname,
  2082. routeId: actionMatch.route.id
  2083. })
  2084. };
  2085. } else {
  2086. let results = await callDataStrategy("action", state, request, [actionMatch], matches, null);
  2087. result = results[actionMatch.route.id];
  2088. if (request.signal.aborted) {
  2089. return {
  2090. shortCircuited: true
  2091. };
  2092. }
  2093. }
  2094. if (isRedirectResult(result)) {
  2095. let replace;
  2096. if (opts && opts.replace != null) {
  2097. replace = opts.replace;
  2098. } else {
  2099. // If the user didn't explicity indicate replace behavior, replace if
  2100. // we redirected to the exact same location we're currently at to avoid
  2101. // double back-buttons
  2102. let location = normalizeRedirectLocation(result.response.headers.get("Location"), new URL(request.url), basename);
  2103. replace = location === state.location.pathname + state.location.search;
  2104. }
  2105. await startRedirectNavigation(request, result, true, {
  2106. submission,
  2107. replace
  2108. });
  2109. return {
  2110. shortCircuited: true
  2111. };
  2112. }
  2113. if (isDeferredResult(result)) {
  2114. throw getInternalRouterError(400, {
  2115. type: "defer-action"
  2116. });
  2117. }
  2118. if (isErrorResult(result)) {
  2119. // Store off the pending error - we use it to determine which loaders
  2120. // to call and will commit it when we complete the navigation
  2121. let boundaryMatch = findNearestBoundary(matches, actionMatch.route.id);
  2122. // By default, all submissions to the current location are REPLACE
  2123. // navigations, but if the action threw an error that'll be rendered in
  2124. // an errorElement, we fall back to PUSH so that the user can use the
  2125. // back button to get back to the pre-submission form location to try
  2126. // again
  2127. if ((opts && opts.replace) !== true) {
  2128. pendingAction = Action.Push;
  2129. }
  2130. return {
  2131. matches,
  2132. pendingActionResult: [boundaryMatch.route.id, result]
  2133. };
  2134. }
  2135. return {
  2136. matches,
  2137. pendingActionResult: [actionMatch.route.id, result]
  2138. };
  2139. }
  2140. // Call all applicable loaders for the given matches, handling redirects,
  2141. // errors, etc.
  2142. async function handleLoaders(request, location, matches, isFogOfWar, overrideNavigation, submission, fetcherSubmission, replace, initialHydration, flushSync, pendingActionResult) {
  2143. // Figure out the right navigation we want to use for data loading
  2144. let loadingNavigation = overrideNavigation || getLoadingNavigation(location, submission);
  2145. // If this was a redirect from an action we don't have a "submission" but
  2146. // we have it on the loading navigation so use that if available
  2147. let activeSubmission = submission || fetcherSubmission || getSubmissionFromNavigation(loadingNavigation);
  2148. // If this is an uninterrupted revalidation, we remain in our current idle
  2149. // state. If not, we need to switch to our loading state and load data,
  2150. // preserving any new action data or existing action data (in the case of
  2151. // a revalidation interrupting an actionReload)
  2152. // If we have partialHydration enabled, then don't update the state for the
  2153. // initial data load since it's not a "navigation"
  2154. let shouldUpdateNavigationState = !isUninterruptedRevalidation && (!future.v7_partialHydration || !initialHydration);
  2155. // When fog of war is enabled, we enter our `loading` state earlier so we
  2156. // can discover new routes during the `loading` state. We skip this if
  2157. // we've already run actions since we would have done our matching already.
  2158. // If the children() function threw then, we want to proceed with the
  2159. // partial matches it discovered.
  2160. if (isFogOfWar) {
  2161. if (shouldUpdateNavigationState) {
  2162. let actionData = getUpdatedActionData(pendingActionResult);
  2163. updateState(_extends({
  2164. navigation: loadingNavigation
  2165. }, actionData !== undefined ? {
  2166. actionData
  2167. } : {}), {
  2168. flushSync
  2169. });
  2170. }
  2171. let discoverResult = await discoverRoutes(matches, location.pathname, request.signal);
  2172. if (discoverResult.type === "aborted") {
  2173. return {
  2174. shortCircuited: true
  2175. };
  2176. } else if (discoverResult.type === "error") {
  2177. let boundaryId = findNearestBoundary(discoverResult.partialMatches).route.id;
  2178. return {
  2179. matches: discoverResult.partialMatches,
  2180. loaderData: {},
  2181. errors: {
  2182. [boundaryId]: discoverResult.error
  2183. }
  2184. };
  2185. } else if (!discoverResult.matches) {
  2186. let {
  2187. error,
  2188. notFoundMatches,
  2189. route
  2190. } = handleNavigational404(location.pathname);
  2191. return {
  2192. matches: notFoundMatches,
  2193. loaderData: {},
  2194. errors: {
  2195. [route.id]: error
  2196. }
  2197. };
  2198. } else {
  2199. matches = discoverResult.matches;
  2200. }
  2201. }
  2202. let routesToUse = inFlightDataRoutes || dataRoutes;
  2203. let [matchesToLoad, revalidatingFetchers] = getMatchesToLoad(init.history, state, matches, activeSubmission, location, future.v7_partialHydration && initialHydration === true, future.v7_skipActionErrorRevalidation, isRevalidationRequired, cancelledDeferredRoutes, cancelledFetcherLoads, deletedFetchers, fetchLoadMatches, fetchRedirectIds, routesToUse, basename, pendingActionResult);
  2204. // Cancel pending deferreds for no-longer-matched routes or routes we're
  2205. // about to reload. Note that if this is an action reload we would have
  2206. // already cancelled all pending deferreds so this would be a no-op
  2207. cancelActiveDeferreds(routeId => !(matches && matches.some(m => m.route.id === routeId)) || matchesToLoad && matchesToLoad.some(m => m.route.id === routeId));
  2208. pendingNavigationLoadId = ++incrementingLoadId;
  2209. // Short circuit if we have no loaders to run
  2210. if (matchesToLoad.length === 0 && revalidatingFetchers.length === 0) {
  2211. let updatedFetchers = markFetchRedirectsDone();
  2212. completeNavigation(location, _extends({
  2213. matches,
  2214. loaderData: {},
  2215. // Commit pending error if we're short circuiting
  2216. errors: pendingActionResult && isErrorResult(pendingActionResult[1]) ? {
  2217. [pendingActionResult[0]]: pendingActionResult[1].error
  2218. } : null
  2219. }, getActionDataForCommit(pendingActionResult), updatedFetchers ? {
  2220. fetchers: new Map(state.fetchers)
  2221. } : {}), {
  2222. flushSync
  2223. });
  2224. return {
  2225. shortCircuited: true
  2226. };
  2227. }
  2228. if (shouldUpdateNavigationState) {
  2229. let updates = {};
  2230. if (!isFogOfWar) {
  2231. // Only update navigation/actionNData if we didn't already do it above
  2232. updates.navigation = loadingNavigation;
  2233. let actionData = getUpdatedActionData(pendingActionResult);
  2234. if (actionData !== undefined) {
  2235. updates.actionData = actionData;
  2236. }
  2237. }
  2238. if (revalidatingFetchers.length > 0) {
  2239. updates.fetchers = getUpdatedRevalidatingFetchers(revalidatingFetchers);
  2240. }
  2241. updateState(updates, {
  2242. flushSync
  2243. });
  2244. }
  2245. revalidatingFetchers.forEach(rf => {
  2246. abortFetcher(rf.key);
  2247. if (rf.controller) {
  2248. // Fetchers use an independent AbortController so that aborting a fetcher
  2249. // (via deleteFetcher) does not abort the triggering navigation that
  2250. // triggered the revalidation
  2251. fetchControllers.set(rf.key, rf.controller);
  2252. }
  2253. });
  2254. // Proxy navigation abort through to revalidation fetchers
  2255. let abortPendingFetchRevalidations = () => revalidatingFetchers.forEach(f => abortFetcher(f.key));
  2256. if (pendingNavigationController) {
  2257. pendingNavigationController.signal.addEventListener("abort", abortPendingFetchRevalidations);
  2258. }
  2259. let {
  2260. loaderResults,
  2261. fetcherResults
  2262. } = await callLoadersAndMaybeResolveData(state, matches, matchesToLoad, revalidatingFetchers, request);
  2263. if (request.signal.aborted) {
  2264. return {
  2265. shortCircuited: true
  2266. };
  2267. }
  2268. // Clean up _after_ loaders have completed. Don't clean up if we short
  2269. // circuited because fetchControllers would have been aborted and
  2270. // reassigned to new controllers for the next navigation
  2271. if (pendingNavigationController) {
  2272. pendingNavigationController.signal.removeEventListener("abort", abortPendingFetchRevalidations);
  2273. }
  2274. revalidatingFetchers.forEach(rf => fetchControllers.delete(rf.key));
  2275. // If any loaders returned a redirect Response, start a new REPLACE navigation
  2276. let redirect = findRedirect(loaderResults);
  2277. if (redirect) {
  2278. await startRedirectNavigation(request, redirect.result, true, {
  2279. replace
  2280. });
  2281. return {
  2282. shortCircuited: true
  2283. };
  2284. }
  2285. redirect = findRedirect(fetcherResults);
  2286. if (redirect) {
  2287. // If this redirect came from a fetcher make sure we mark it in
  2288. // fetchRedirectIds so it doesn't get revalidated on the next set of
  2289. // loader executions
  2290. fetchRedirectIds.add(redirect.key);
  2291. await startRedirectNavigation(request, redirect.result, true, {
  2292. replace
  2293. });
  2294. return {
  2295. shortCircuited: true
  2296. };
  2297. }
  2298. // Process and commit output from loaders
  2299. let {
  2300. loaderData,
  2301. errors
  2302. } = processLoaderData(state, matches, loaderResults, pendingActionResult, revalidatingFetchers, fetcherResults, activeDeferreds);
  2303. // Wire up subscribers to update loaderData as promises settle
  2304. activeDeferreds.forEach((deferredData, routeId) => {
  2305. deferredData.subscribe(aborted => {
  2306. // Note: No need to updateState here since the TrackedPromise on
  2307. // loaderData is stable across resolve/reject
  2308. // Remove this instance if we were aborted or if promises have settled
  2309. if (aborted || deferredData.done) {
  2310. activeDeferreds.delete(routeId);
  2311. }
  2312. });
  2313. });
  2314. // Preserve SSR errors during partial hydration
  2315. if (future.v7_partialHydration && initialHydration && state.errors) {
  2316. errors = _extends({}, state.errors, errors);
  2317. }
  2318. let updatedFetchers = markFetchRedirectsDone();
  2319. let didAbortFetchLoads = abortStaleFetchLoads(pendingNavigationLoadId);
  2320. let shouldUpdateFetchers = updatedFetchers || didAbortFetchLoads || revalidatingFetchers.length > 0;
  2321. return _extends({
  2322. matches,
  2323. loaderData,
  2324. errors
  2325. }, shouldUpdateFetchers ? {
  2326. fetchers: new Map(state.fetchers)
  2327. } : {});
  2328. }
  2329. function getUpdatedActionData(pendingActionResult) {
  2330. if (pendingActionResult && !isErrorResult(pendingActionResult[1])) {
  2331. // This is cast to `any` currently because `RouteData`uses any and it
  2332. // would be a breaking change to use any.
  2333. // TODO: v7 - change `RouteData` to use `unknown` instead of `any`
  2334. return {
  2335. [pendingActionResult[0]]: pendingActionResult[1].data
  2336. };
  2337. } else if (state.actionData) {
  2338. if (Object.keys(state.actionData).length === 0) {
  2339. return null;
  2340. } else {
  2341. return state.actionData;
  2342. }
  2343. }
  2344. }
  2345. function getUpdatedRevalidatingFetchers(revalidatingFetchers) {
  2346. revalidatingFetchers.forEach(rf => {
  2347. let fetcher = state.fetchers.get(rf.key);
  2348. let revalidatingFetcher = getLoadingFetcher(undefined, fetcher ? fetcher.data : undefined);
  2349. state.fetchers.set(rf.key, revalidatingFetcher);
  2350. });
  2351. return new Map(state.fetchers);
  2352. }
  2353. // Trigger a fetcher load/submit for the given fetcher key
  2354. function fetch(key, routeId, href, opts) {
  2355. if (isServer) {
  2356. throw new Error("router.fetch() was called during the server render, but it shouldn't be. " + "You are likely calling a useFetcher() method in the body of your component. " + "Try moving it to a useEffect or a callback.");
  2357. }
  2358. abortFetcher(key);
  2359. let flushSync = (opts && opts.flushSync) === true;
  2360. let routesToUse = inFlightDataRoutes || dataRoutes;
  2361. let normalizedPath = normalizeTo(state.location, state.matches, basename, future.v7_prependBasename, href, future.v7_relativeSplatPath, routeId, opts == null ? void 0 : opts.relative);
  2362. let matches = matchRoutes(routesToUse, normalizedPath, basename);
  2363. let fogOfWar = checkFogOfWar(matches, routesToUse, normalizedPath);
  2364. if (fogOfWar.active && fogOfWar.matches) {
  2365. matches = fogOfWar.matches;
  2366. }
  2367. if (!matches) {
  2368. setFetcherError(key, routeId, getInternalRouterError(404, {
  2369. pathname: normalizedPath
  2370. }), {
  2371. flushSync
  2372. });
  2373. return;
  2374. }
  2375. let {
  2376. path,
  2377. submission,
  2378. error
  2379. } = normalizeNavigateOptions(future.v7_normalizeFormMethod, true, normalizedPath, opts);
  2380. if (error) {
  2381. setFetcherError(key, routeId, error, {
  2382. flushSync
  2383. });
  2384. return;
  2385. }
  2386. let match = getTargetMatch(matches, path);
  2387. let preventScrollReset = (opts && opts.preventScrollReset) === true;
  2388. if (submission && isMutationMethod(submission.formMethod)) {
  2389. handleFetcherAction(key, routeId, path, match, matches, fogOfWar.active, flushSync, preventScrollReset, submission);
  2390. return;
  2391. }
  2392. // Store off the match so we can call it's shouldRevalidate on subsequent
  2393. // revalidations
  2394. fetchLoadMatches.set(key, {
  2395. routeId,
  2396. path
  2397. });
  2398. handleFetcherLoader(key, routeId, path, match, matches, fogOfWar.active, flushSync, preventScrollReset, submission);
  2399. }
  2400. // Call the action for the matched fetcher.submit(), and then handle redirects,
  2401. // errors, and revalidation
  2402. async function handleFetcherAction(key, routeId, path, match, requestMatches, isFogOfWar, flushSync, preventScrollReset, submission) {
  2403. interruptActiveLoads();
  2404. fetchLoadMatches.delete(key);
  2405. function detectAndHandle405Error(m) {
  2406. if (!m.route.action && !m.route.lazy) {
  2407. let error = getInternalRouterError(405, {
  2408. method: submission.formMethod,
  2409. pathname: path,
  2410. routeId: routeId
  2411. });
  2412. setFetcherError(key, routeId, error, {
  2413. flushSync
  2414. });
  2415. return true;
  2416. }
  2417. return false;
  2418. }
  2419. if (!isFogOfWar && detectAndHandle405Error(match)) {
  2420. return;
  2421. }
  2422. // Put this fetcher into it's submitting state
  2423. let existingFetcher = state.fetchers.get(key);
  2424. updateFetcherState(key, getSubmittingFetcher(submission, existingFetcher), {
  2425. flushSync
  2426. });
  2427. let abortController = new AbortController();
  2428. let fetchRequest = createClientSideRequest(init.history, path, abortController.signal, submission);
  2429. if (isFogOfWar) {
  2430. let discoverResult = await discoverRoutes(requestMatches, new URL(fetchRequest.url).pathname, fetchRequest.signal, key);
  2431. if (discoverResult.type === "aborted") {
  2432. return;
  2433. } else if (discoverResult.type === "error") {
  2434. setFetcherError(key, routeId, discoverResult.error, {
  2435. flushSync
  2436. });
  2437. return;
  2438. } else if (!discoverResult.matches) {
  2439. setFetcherError(key, routeId, getInternalRouterError(404, {
  2440. pathname: path
  2441. }), {
  2442. flushSync
  2443. });
  2444. return;
  2445. } else {
  2446. requestMatches = discoverResult.matches;
  2447. match = getTargetMatch(requestMatches, path);
  2448. if (detectAndHandle405Error(match)) {
  2449. return;
  2450. }
  2451. }
  2452. }
  2453. // Call the action for the fetcher
  2454. fetchControllers.set(key, abortController);
  2455. let originatingLoadId = incrementingLoadId;
  2456. let actionResults = await callDataStrategy("action", state, fetchRequest, [match], requestMatches, key);
  2457. let actionResult = actionResults[match.route.id];
  2458. if (fetchRequest.signal.aborted) {
  2459. // We can delete this so long as we weren't aborted by our own fetcher
  2460. // re-submit which would have put _new_ controller is in fetchControllers
  2461. if (fetchControllers.get(key) === abortController) {
  2462. fetchControllers.delete(key);
  2463. }
  2464. return;
  2465. }
  2466. // When using v7_fetcherPersist, we don't want errors bubbling up to the UI
  2467. // or redirects processed for unmounted fetchers so we just revert them to
  2468. // idle
  2469. if (future.v7_fetcherPersist && deletedFetchers.has(key)) {
  2470. if (isRedirectResult(actionResult) || isErrorResult(actionResult)) {
  2471. updateFetcherState(key, getDoneFetcher(undefined));
  2472. return;
  2473. }
  2474. // Let SuccessResult's fall through for revalidation
  2475. } else {
  2476. if (isRedirectResult(actionResult)) {
  2477. fetchControllers.delete(key);
  2478. if (pendingNavigationLoadId > originatingLoadId) {
  2479. // A new navigation was kicked off after our action started, so that
  2480. // should take precedence over this redirect navigation. We already
  2481. // set isRevalidationRequired so all loaders for the new route should
  2482. // fire unless opted out via shouldRevalidate
  2483. updateFetcherState(key, getDoneFetcher(undefined));
  2484. return;
  2485. } else {
  2486. fetchRedirectIds.add(key);
  2487. updateFetcherState(key, getLoadingFetcher(submission));
  2488. return startRedirectNavigation(fetchRequest, actionResult, false, {
  2489. fetcherSubmission: submission,
  2490. preventScrollReset
  2491. });
  2492. }
  2493. }
  2494. // Process any non-redirect errors thrown
  2495. if (isErrorResult(actionResult)) {
  2496. setFetcherError(key, routeId, actionResult.error);
  2497. return;
  2498. }
  2499. }
  2500. if (isDeferredResult(actionResult)) {
  2501. throw getInternalRouterError(400, {
  2502. type: "defer-action"
  2503. });
  2504. }
  2505. // Start the data load for current matches, or the next location if we're
  2506. // in the middle of a navigation
  2507. let nextLocation = state.navigation.location || state.location;
  2508. let revalidationRequest = createClientSideRequest(init.history, nextLocation, abortController.signal);
  2509. let routesToUse = inFlightDataRoutes || dataRoutes;
  2510. let matches = state.navigation.state !== "idle" ? matchRoutes(routesToUse, state.navigation.location, basename) : state.matches;
  2511. invariant(matches, "Didn't find any matches after fetcher action");
  2512. let loadId = ++incrementingLoadId;
  2513. fetchReloadIds.set(key, loadId);
  2514. let loadFetcher = getLoadingFetcher(submission, actionResult.data);
  2515. state.fetchers.set(key, loadFetcher);
  2516. let [matchesToLoad, revalidatingFetchers] = getMatchesToLoad(init.history, state, matches, submission, nextLocation, false, future.v7_skipActionErrorRevalidation, isRevalidationRequired, cancelledDeferredRoutes, cancelledFetcherLoads, deletedFetchers, fetchLoadMatches, fetchRedirectIds, routesToUse, basename, [match.route.id, actionResult]);
  2517. // Put all revalidating fetchers into the loading state, except for the
  2518. // current fetcher which we want to keep in it's current loading state which
  2519. // contains it's action submission info + action data
  2520. revalidatingFetchers.filter(rf => rf.key !== key).forEach(rf => {
  2521. let staleKey = rf.key;
  2522. let existingFetcher = state.fetchers.get(staleKey);
  2523. let revalidatingFetcher = getLoadingFetcher(undefined, existingFetcher ? existingFetcher.data : undefined);
  2524. state.fetchers.set(staleKey, revalidatingFetcher);
  2525. abortFetcher(staleKey);
  2526. if (rf.controller) {
  2527. fetchControllers.set(staleKey, rf.controller);
  2528. }
  2529. });
  2530. updateState({
  2531. fetchers: new Map(state.fetchers)
  2532. });
  2533. let abortPendingFetchRevalidations = () => revalidatingFetchers.forEach(rf => abortFetcher(rf.key));
  2534. abortController.signal.addEventListener("abort", abortPendingFetchRevalidations);
  2535. let {
  2536. loaderResults,
  2537. fetcherResults
  2538. } = await callLoadersAndMaybeResolveData(state, matches, matchesToLoad, revalidatingFetchers, revalidationRequest);
  2539. if (abortController.signal.aborted) {
  2540. return;
  2541. }
  2542. abortController.signal.removeEventListener("abort", abortPendingFetchRevalidations);
  2543. fetchReloadIds.delete(key);
  2544. fetchControllers.delete(key);
  2545. revalidatingFetchers.forEach(r => fetchControllers.delete(r.key));
  2546. let redirect = findRedirect(loaderResults);
  2547. if (redirect) {
  2548. return startRedirectNavigation(revalidationRequest, redirect.result, false, {
  2549. preventScrollReset
  2550. });
  2551. }
  2552. redirect = findRedirect(fetcherResults);
  2553. if (redirect) {
  2554. // If this redirect came from a fetcher make sure we mark it in
  2555. // fetchRedirectIds so it doesn't get revalidated on the next set of
  2556. // loader executions
  2557. fetchRedirectIds.add(redirect.key);
  2558. return startRedirectNavigation(revalidationRequest, redirect.result, false, {
  2559. preventScrollReset
  2560. });
  2561. }
  2562. // Process and commit output from loaders
  2563. let {
  2564. loaderData,
  2565. errors
  2566. } = processLoaderData(state, matches, loaderResults, undefined, revalidatingFetchers, fetcherResults, activeDeferreds);
  2567. // Since we let revalidations complete even if the submitting fetcher was
  2568. // deleted, only put it back to idle if it hasn't been deleted
  2569. if (state.fetchers.has(key)) {
  2570. let doneFetcher = getDoneFetcher(actionResult.data);
  2571. state.fetchers.set(key, doneFetcher);
  2572. }
  2573. abortStaleFetchLoads(loadId);
  2574. // If we are currently in a navigation loading state and this fetcher is
  2575. // more recent than the navigation, we want the newer data so abort the
  2576. // navigation and complete it with the fetcher data
  2577. if (state.navigation.state === "loading" && loadId > pendingNavigationLoadId) {
  2578. invariant(pendingAction, "Expected pending action");
  2579. pendingNavigationController && pendingNavigationController.abort();
  2580. completeNavigation(state.navigation.location, {
  2581. matches,
  2582. loaderData,
  2583. errors,
  2584. fetchers: new Map(state.fetchers)
  2585. });
  2586. } else {
  2587. // otherwise just update with the fetcher data, preserving any existing
  2588. // loaderData for loaders that did not need to reload. We have to
  2589. // manually merge here since we aren't going through completeNavigation
  2590. updateState({
  2591. errors,
  2592. loaderData: mergeLoaderData(state.loaderData, loaderData, matches, errors),
  2593. fetchers: new Map(state.fetchers)
  2594. });
  2595. isRevalidationRequired = false;
  2596. }
  2597. }
  2598. // Call the matched loader for fetcher.load(), handling redirects, errors, etc.
  2599. async function handleFetcherLoader(key, routeId, path, match, matches, isFogOfWar, flushSync, preventScrollReset, submission) {
  2600. let existingFetcher = state.fetchers.get(key);
  2601. updateFetcherState(key, getLoadingFetcher(submission, existingFetcher ? existingFetcher.data : undefined), {
  2602. flushSync
  2603. });
  2604. let abortController = new AbortController();
  2605. let fetchRequest = createClientSideRequest(init.history, path, abortController.signal);
  2606. if (isFogOfWar) {
  2607. let discoverResult = await discoverRoutes(matches, new URL(fetchRequest.url).pathname, fetchRequest.signal, key);
  2608. if (discoverResult.type === "aborted") {
  2609. return;
  2610. } else if (discoverResult.type === "error") {
  2611. setFetcherError(key, routeId, discoverResult.error, {
  2612. flushSync
  2613. });
  2614. return;
  2615. } else if (!discoverResult.matches) {
  2616. setFetcherError(key, routeId, getInternalRouterError(404, {
  2617. pathname: path
  2618. }), {
  2619. flushSync
  2620. });
  2621. return;
  2622. } else {
  2623. matches = discoverResult.matches;
  2624. match = getTargetMatch(matches, path);
  2625. }
  2626. }
  2627. // Call the loader for this fetcher route match
  2628. fetchControllers.set(key, abortController);
  2629. let originatingLoadId = incrementingLoadId;
  2630. let results = await callDataStrategy("loader", state, fetchRequest, [match], matches, key);
  2631. let result = results[match.route.id];
  2632. // Deferred isn't supported for fetcher loads, await everything and treat it
  2633. // as a normal load. resolveDeferredData will return undefined if this
  2634. // fetcher gets aborted, so we just leave result untouched and short circuit
  2635. // below if that happens
  2636. if (isDeferredResult(result)) {
  2637. result = (await resolveDeferredData(result, fetchRequest.signal, true)) || result;
  2638. }
  2639. // We can delete this so long as we weren't aborted by our our own fetcher
  2640. // re-load which would have put _new_ controller is in fetchControllers
  2641. if (fetchControllers.get(key) === abortController) {
  2642. fetchControllers.delete(key);
  2643. }
  2644. if (fetchRequest.signal.aborted) {
  2645. return;
  2646. }
  2647. // We don't want errors bubbling up or redirects followed for unmounted
  2648. // fetchers, so short circuit here if it was removed from the UI
  2649. if (deletedFetchers.has(key)) {
  2650. updateFetcherState(key, getDoneFetcher(undefined));
  2651. return;
  2652. }
  2653. // If the loader threw a redirect Response, start a new REPLACE navigation
  2654. if (isRedirectResult(result)) {
  2655. if (pendingNavigationLoadId > originatingLoadId) {
  2656. // A new navigation was kicked off after our loader started, so that
  2657. // should take precedence over this redirect navigation
  2658. updateFetcherState(key, getDoneFetcher(undefined));
  2659. return;
  2660. } else {
  2661. fetchRedirectIds.add(key);
  2662. await startRedirectNavigation(fetchRequest, result, false, {
  2663. preventScrollReset
  2664. });
  2665. return;
  2666. }
  2667. }
  2668. // Process any non-redirect errors thrown
  2669. if (isErrorResult(result)) {
  2670. setFetcherError(key, routeId, result.error);
  2671. return;
  2672. }
  2673. invariant(!isDeferredResult(result), "Unhandled fetcher deferred data");
  2674. // Put the fetcher back into an idle state
  2675. updateFetcherState(key, getDoneFetcher(result.data));
  2676. }
  2677. /**
  2678. * Utility function to handle redirects returned from an action or loader.
  2679. * Normally, a redirect "replaces" the navigation that triggered it. So, for
  2680. * example:
  2681. *
  2682. * - user is on /a
  2683. * - user clicks a link to /b
  2684. * - loader for /b redirects to /c
  2685. *
  2686. * In a non-JS app the browser would track the in-flight navigation to /b and
  2687. * then replace it with /c when it encountered the redirect response. In
  2688. * the end it would only ever update the URL bar with /c.
  2689. *
  2690. * In client-side routing using pushState/replaceState, we aim to emulate
  2691. * this behavior and we also do not update history until the end of the
  2692. * navigation (including processed redirects). This means that we never
  2693. * actually touch history until we've processed redirects, so we just use
  2694. * the history action from the original navigation (PUSH or REPLACE).
  2695. */
  2696. async function startRedirectNavigation(request, redirect, isNavigation, _temp2) {
  2697. let {
  2698. submission,
  2699. fetcherSubmission,
  2700. preventScrollReset,
  2701. replace
  2702. } = _temp2 === void 0 ? {} : _temp2;
  2703. if (redirect.response.headers.has("X-Remix-Revalidate")) {
  2704. isRevalidationRequired = true;
  2705. }
  2706. let location = redirect.response.headers.get("Location");
  2707. invariant(location, "Expected a Location header on the redirect Response");
  2708. location = normalizeRedirectLocation(location, new URL(request.url), basename);
  2709. let redirectLocation = createLocation(state.location, location, {
  2710. _isRedirect: true
  2711. });
  2712. if (isBrowser) {
  2713. let isDocumentReload = false;
  2714. if (redirect.response.headers.has("X-Remix-Reload-Document")) {
  2715. // Hard reload if the response contained X-Remix-Reload-Document
  2716. isDocumentReload = true;
  2717. } else if (ABSOLUTE_URL_REGEX.test(location)) {
  2718. const url = init.history.createURL(location);
  2719. isDocumentReload =
  2720. // Hard reload if it's an absolute URL to a new origin
  2721. url.origin !== routerWindow.location.origin ||
  2722. // Hard reload if it's an absolute URL that does not match our basename
  2723. stripBasename(url.pathname, basename) == null;
  2724. }
  2725. if (isDocumentReload) {
  2726. if (replace) {
  2727. routerWindow.location.replace(location);
  2728. } else {
  2729. routerWindow.location.assign(location);
  2730. }
  2731. return;
  2732. }
  2733. }
  2734. // There's no need to abort on redirects, since we don't detect the
  2735. // redirect until the action/loaders have settled
  2736. pendingNavigationController = null;
  2737. let redirectHistoryAction = replace === true || redirect.response.headers.has("X-Remix-Replace") ? Action.Replace : Action.Push;
  2738. // Use the incoming submission if provided, fallback on the active one in
  2739. // state.navigation
  2740. let {
  2741. formMethod,
  2742. formAction,
  2743. formEncType
  2744. } = state.navigation;
  2745. if (!submission && !fetcherSubmission && formMethod && formAction && formEncType) {
  2746. submission = getSubmissionFromNavigation(state.navigation);
  2747. }
  2748. // If this was a 307/308 submission we want to preserve the HTTP method and
  2749. // re-submit the GET/POST/PUT/PATCH/DELETE as a submission navigation to the
  2750. // redirected location
  2751. let activeSubmission = submission || fetcherSubmission;
  2752. if (redirectPreserveMethodStatusCodes.has(redirect.response.status) && activeSubmission && isMutationMethod(activeSubmission.formMethod)) {
  2753. await startNavigation(redirectHistoryAction, redirectLocation, {
  2754. submission: _extends({}, activeSubmission, {
  2755. formAction: location
  2756. }),
  2757. // Preserve these flags across redirects
  2758. preventScrollReset: preventScrollReset || pendingPreventScrollReset,
  2759. enableViewTransition: isNavigation ? pendingViewTransitionEnabled : undefined
  2760. });
  2761. } else {
  2762. // If we have a navigation submission, we will preserve it through the
  2763. // redirect navigation
  2764. let overrideNavigation = getLoadingNavigation(redirectLocation, submission);
  2765. await startNavigation(redirectHistoryAction, redirectLocation, {
  2766. overrideNavigation,
  2767. // Send fetcher submissions through for shouldRevalidate
  2768. fetcherSubmission,
  2769. // Preserve these flags across redirects
  2770. preventScrollReset: preventScrollReset || pendingPreventScrollReset,
  2771. enableViewTransition: isNavigation ? pendingViewTransitionEnabled : undefined
  2772. });
  2773. }
  2774. }
  2775. // Utility wrapper for calling dataStrategy client-side without having to
  2776. // pass around the manifest, mapRouteProperties, etc.
  2777. async function callDataStrategy(type, state, request, matchesToLoad, matches, fetcherKey) {
  2778. let results;
  2779. let dataResults = {};
  2780. try {
  2781. results = await callDataStrategyImpl(dataStrategyImpl, type, state, request, matchesToLoad, matches, fetcherKey, manifest, mapRouteProperties);
  2782. } catch (e) {
  2783. // If the outer dataStrategy method throws, just return the error for all
  2784. // matches - and it'll naturally bubble to the root
  2785. matchesToLoad.forEach(m => {
  2786. dataResults[m.route.id] = {
  2787. type: ResultType.error,
  2788. error: e
  2789. };
  2790. });
  2791. return dataResults;
  2792. }
  2793. for (let [routeId, result] of Object.entries(results)) {
  2794. if (isRedirectDataStrategyResultResult(result)) {
  2795. let response = result.result;
  2796. dataResults[routeId] = {
  2797. type: ResultType.redirect,
  2798. response: normalizeRelativeRoutingRedirectResponse(response, request, routeId, matches, basename, future.v7_relativeSplatPath)
  2799. };
  2800. } else {
  2801. dataResults[routeId] = await convertDataStrategyResultToDataResult(result);
  2802. }
  2803. }
  2804. return dataResults;
  2805. }
  2806. async function callLoadersAndMaybeResolveData(state, matches, matchesToLoad, fetchersToLoad, request) {
  2807. let currentMatches = state.matches;
  2808. // Kick off loaders and fetchers in parallel
  2809. let loaderResultsPromise = callDataStrategy("loader", state, request, matchesToLoad, matches, null);
  2810. let fetcherResultsPromise = Promise.all(fetchersToLoad.map(async f => {
  2811. if (f.matches && f.match && f.controller) {
  2812. let results = await callDataStrategy("loader", state, createClientSideRequest(init.history, f.path, f.controller.signal), [f.match], f.matches, f.key);
  2813. let result = results[f.match.route.id];
  2814. // Fetcher results are keyed by fetcher key from here on out, not routeId
  2815. return {
  2816. [f.key]: result
  2817. };
  2818. } else {
  2819. return Promise.resolve({
  2820. [f.key]: {
  2821. type: ResultType.error,
  2822. error: getInternalRouterError(404, {
  2823. pathname: f.path
  2824. })
  2825. }
  2826. });
  2827. }
  2828. }));
  2829. let loaderResults = await loaderResultsPromise;
  2830. let fetcherResults = (await fetcherResultsPromise).reduce((acc, r) => Object.assign(acc, r), {});
  2831. await Promise.all([resolveNavigationDeferredResults(matches, loaderResults, request.signal, currentMatches, state.loaderData), resolveFetcherDeferredResults(matches, fetcherResults, fetchersToLoad)]);
  2832. return {
  2833. loaderResults,
  2834. fetcherResults
  2835. };
  2836. }
  2837. function interruptActiveLoads() {
  2838. // Every interruption triggers a revalidation
  2839. isRevalidationRequired = true;
  2840. // Cancel pending route-level deferreds and mark cancelled routes for
  2841. // revalidation
  2842. cancelledDeferredRoutes.push(...cancelActiveDeferreds());
  2843. // Abort in-flight fetcher loads
  2844. fetchLoadMatches.forEach((_, key) => {
  2845. if (fetchControllers.has(key)) {
  2846. cancelledFetcherLoads.add(key);
  2847. }
  2848. abortFetcher(key);
  2849. });
  2850. }
  2851. function updateFetcherState(key, fetcher, opts) {
  2852. if (opts === void 0) {
  2853. opts = {};
  2854. }
  2855. state.fetchers.set(key, fetcher);
  2856. updateState({
  2857. fetchers: new Map(state.fetchers)
  2858. }, {
  2859. flushSync: (opts && opts.flushSync) === true
  2860. });
  2861. }
  2862. function setFetcherError(key, routeId, error, opts) {
  2863. if (opts === void 0) {
  2864. opts = {};
  2865. }
  2866. let boundaryMatch = findNearestBoundary(state.matches, routeId);
  2867. deleteFetcher(key);
  2868. updateState({
  2869. errors: {
  2870. [boundaryMatch.route.id]: error
  2871. },
  2872. fetchers: new Map(state.fetchers)
  2873. }, {
  2874. flushSync: (opts && opts.flushSync) === true
  2875. });
  2876. }
  2877. function getFetcher(key) {
  2878. activeFetchers.set(key, (activeFetchers.get(key) || 0) + 1);
  2879. // If this fetcher was previously marked for deletion, unmark it since we
  2880. // have a new instance
  2881. if (deletedFetchers.has(key)) {
  2882. deletedFetchers.delete(key);
  2883. }
  2884. return state.fetchers.get(key) || IDLE_FETCHER;
  2885. }
  2886. function deleteFetcher(key) {
  2887. let fetcher = state.fetchers.get(key);
  2888. // Don't abort the controller if this is a deletion of a fetcher.submit()
  2889. // in it's loading phase since - we don't want to abort the corresponding
  2890. // revalidation and want them to complete and land
  2891. if (fetchControllers.has(key) && !(fetcher && fetcher.state === "loading" && fetchReloadIds.has(key))) {
  2892. abortFetcher(key);
  2893. }
  2894. fetchLoadMatches.delete(key);
  2895. fetchReloadIds.delete(key);
  2896. fetchRedirectIds.delete(key);
  2897. // If we opted into the flag we can clear this now since we're calling
  2898. // deleteFetcher() at the end of updateState() and we've already handed the
  2899. // deleted fetcher keys off to the data layer.
  2900. // If not, we're eagerly calling deleteFetcher() and we need to keep this
  2901. // Set populated until the next updateState call, and we'll clear
  2902. // `deletedFetchers` then
  2903. if (future.v7_fetcherPersist) {
  2904. deletedFetchers.delete(key);
  2905. }
  2906. cancelledFetcherLoads.delete(key);
  2907. state.fetchers.delete(key);
  2908. }
  2909. function deleteFetcherAndUpdateState(key) {
  2910. let count = (activeFetchers.get(key) || 0) - 1;
  2911. if (count <= 0) {
  2912. activeFetchers.delete(key);
  2913. deletedFetchers.add(key);
  2914. if (!future.v7_fetcherPersist) {
  2915. deleteFetcher(key);
  2916. }
  2917. } else {
  2918. activeFetchers.set(key, count);
  2919. }
  2920. updateState({
  2921. fetchers: new Map(state.fetchers)
  2922. });
  2923. }
  2924. function abortFetcher(key) {
  2925. let controller = fetchControllers.get(key);
  2926. if (controller) {
  2927. controller.abort();
  2928. fetchControllers.delete(key);
  2929. }
  2930. }
  2931. function markFetchersDone(keys) {
  2932. for (let key of keys) {
  2933. let fetcher = getFetcher(key);
  2934. let doneFetcher = getDoneFetcher(fetcher.data);
  2935. state.fetchers.set(key, doneFetcher);
  2936. }
  2937. }
  2938. function markFetchRedirectsDone() {
  2939. let doneKeys = [];
  2940. let updatedFetchers = false;
  2941. for (let key of fetchRedirectIds) {
  2942. let fetcher = state.fetchers.get(key);
  2943. invariant(fetcher, "Expected fetcher: " + key);
  2944. if (fetcher.state === "loading") {
  2945. fetchRedirectIds.delete(key);
  2946. doneKeys.push(key);
  2947. updatedFetchers = true;
  2948. }
  2949. }
  2950. markFetchersDone(doneKeys);
  2951. return updatedFetchers;
  2952. }
  2953. function abortStaleFetchLoads(landedId) {
  2954. let yeetedKeys = [];
  2955. for (let [key, id] of fetchReloadIds) {
  2956. if (id < landedId) {
  2957. let fetcher = state.fetchers.get(key);
  2958. invariant(fetcher, "Expected fetcher: " + key);
  2959. if (fetcher.state === "loading") {
  2960. abortFetcher(key);
  2961. fetchReloadIds.delete(key);
  2962. yeetedKeys.push(key);
  2963. }
  2964. }
  2965. }
  2966. markFetchersDone(yeetedKeys);
  2967. return yeetedKeys.length > 0;
  2968. }
  2969. function getBlocker(key, fn) {
  2970. let blocker = state.blockers.get(key) || IDLE_BLOCKER;
  2971. if (blockerFunctions.get(key) !== fn) {
  2972. blockerFunctions.set(key, fn);
  2973. }
  2974. return blocker;
  2975. }
  2976. function deleteBlocker(key) {
  2977. state.blockers.delete(key);
  2978. blockerFunctions.delete(key);
  2979. }
  2980. // Utility function to update blockers, ensuring valid state transitions
  2981. function updateBlocker(key, newBlocker) {
  2982. let blocker = state.blockers.get(key) || IDLE_BLOCKER;
  2983. // Poor mans state machine :)
  2984. // https://mermaid.live/edit#pako:eNqVkc9OwzAMxl8l8nnjAYrEtDIOHEBIgwvKJTReGy3_lDpIqO27k6awMG0XcrLlnz87nwdonESogKXXBuE79rq75XZO3-yHds0RJVuv70YrPlUrCEe2HfrORS3rubqZfuhtpg5C9wk5tZ4VKcRUq88q9Z8RS0-48cE1iHJkL0ugbHuFLus9L6spZy8nX9MP2CNdomVaposqu3fGayT8T8-jJQwhepo_UtpgBQaDEUom04dZhAN1aJBDlUKJBxE1ceB2Smj0Mln-IBW5AFU2dwUiktt_2Qaq2dBfaKdEup85UV7Yd-dKjlnkabl2Pvr0DTkTreM
  2985. invariant(blocker.state === "unblocked" && newBlocker.state === "blocked" || blocker.state === "blocked" && newBlocker.state === "blocked" || blocker.state === "blocked" && newBlocker.state === "proceeding" || blocker.state === "blocked" && newBlocker.state === "unblocked" || blocker.state === "proceeding" && newBlocker.state === "unblocked", "Invalid blocker state transition: " + blocker.state + " -> " + newBlocker.state);
  2986. let blockers = new Map(state.blockers);
  2987. blockers.set(key, newBlocker);
  2988. updateState({
  2989. blockers
  2990. });
  2991. }
  2992. function shouldBlockNavigation(_ref2) {
  2993. let {
  2994. currentLocation,
  2995. nextLocation,
  2996. historyAction
  2997. } = _ref2;
  2998. if (blockerFunctions.size === 0) {
  2999. return;
  3000. }
  3001. // We ony support a single active blocker at the moment since we don't have
  3002. // any compelling use cases for multi-blocker yet
  3003. if (blockerFunctions.size > 1) {
  3004. warning(false, "A router only supports one blocker at a time");
  3005. }
  3006. let entries = Array.from(blockerFunctions.entries());
  3007. let [blockerKey, blockerFunction] = entries[entries.length - 1];
  3008. let blocker = state.blockers.get(blockerKey);
  3009. if (blocker && blocker.state === "proceeding") {
  3010. // If the blocker is currently proceeding, we don't need to re-check
  3011. // it and can let this navigation continue
  3012. return;
  3013. }
  3014. // At this point, we know we're unblocked/blocked so we need to check the
  3015. // user-provided blocker function
  3016. if (blockerFunction({
  3017. currentLocation,
  3018. nextLocation,
  3019. historyAction
  3020. })) {
  3021. return blockerKey;
  3022. }
  3023. }
  3024. function handleNavigational404(pathname) {
  3025. let error = getInternalRouterError(404, {
  3026. pathname
  3027. });
  3028. let routesToUse = inFlightDataRoutes || dataRoutes;
  3029. let {
  3030. matches,
  3031. route
  3032. } = getShortCircuitMatches(routesToUse);
  3033. // Cancel all pending deferred on 404s since we don't keep any routes
  3034. cancelActiveDeferreds();
  3035. return {
  3036. notFoundMatches: matches,
  3037. route,
  3038. error
  3039. };
  3040. }
  3041. function cancelActiveDeferreds(predicate) {
  3042. let cancelledRouteIds = [];
  3043. activeDeferreds.forEach((dfd, routeId) => {
  3044. if (!predicate || predicate(routeId)) {
  3045. // Cancel the deferred - but do not remove from activeDeferreds here -
  3046. // we rely on the subscribers to do that so our tests can assert proper
  3047. // cleanup via _internalActiveDeferreds
  3048. dfd.cancel();
  3049. cancelledRouteIds.push(routeId);
  3050. activeDeferreds.delete(routeId);
  3051. }
  3052. });
  3053. return cancelledRouteIds;
  3054. }
  3055. // Opt in to capturing and reporting scroll positions during navigations,
  3056. // used by the <ScrollRestoration> component
  3057. function enableScrollRestoration(positions, getPosition, getKey) {
  3058. savedScrollPositions = positions;
  3059. getScrollPosition = getPosition;
  3060. getScrollRestorationKey = getKey || null;
  3061. // Perform initial hydration scroll restoration, since we miss the boat on
  3062. // the initial updateState() because we've not yet rendered <ScrollRestoration/>
  3063. // and therefore have no savedScrollPositions available
  3064. if (!initialScrollRestored && state.navigation === IDLE_NAVIGATION) {
  3065. initialScrollRestored = true;
  3066. let y = getSavedScrollPosition(state.location, state.matches);
  3067. if (y != null) {
  3068. updateState({
  3069. restoreScrollPosition: y
  3070. });
  3071. }
  3072. }
  3073. return () => {
  3074. savedScrollPositions = null;
  3075. getScrollPosition = null;
  3076. getScrollRestorationKey = null;
  3077. };
  3078. }
  3079. function getScrollKey(location, matches) {
  3080. if (getScrollRestorationKey) {
  3081. let key = getScrollRestorationKey(location, matches.map(m => convertRouteMatchToUiMatch(m, state.loaderData)));
  3082. return key || location.key;
  3083. }
  3084. return location.key;
  3085. }
  3086. function saveScrollPosition(location, matches) {
  3087. if (savedScrollPositions && getScrollPosition) {
  3088. let key = getScrollKey(location, matches);
  3089. savedScrollPositions[key] = getScrollPosition();
  3090. }
  3091. }
  3092. function getSavedScrollPosition(location, matches) {
  3093. if (savedScrollPositions) {
  3094. let key = getScrollKey(location, matches);
  3095. let y = savedScrollPositions[key];
  3096. if (typeof y === "number") {
  3097. return y;
  3098. }
  3099. }
  3100. return null;
  3101. }
  3102. function checkFogOfWar(matches, routesToUse, pathname) {
  3103. if (patchRoutesOnNavigationImpl) {
  3104. if (!matches) {
  3105. let fogMatches = matchRoutesImpl(routesToUse, pathname, basename, true);
  3106. return {
  3107. active: true,
  3108. matches: fogMatches || []
  3109. };
  3110. } else {
  3111. if (Object.keys(matches[0].params).length > 0) {
  3112. // If we matched a dynamic param or a splat, it might only be because
  3113. // we haven't yet discovered other routes that would match with a
  3114. // higher score. Call patchRoutesOnNavigation just to be sure
  3115. let partialMatches = matchRoutesImpl(routesToUse, pathname, basename, true);
  3116. return {
  3117. active: true,
  3118. matches: partialMatches
  3119. };
  3120. }
  3121. }
  3122. }
  3123. return {
  3124. active: false,
  3125. matches: null
  3126. };
  3127. }
  3128. async function discoverRoutes(matches, pathname, signal, fetcherKey) {
  3129. if (!patchRoutesOnNavigationImpl) {
  3130. return {
  3131. type: "success",
  3132. matches
  3133. };
  3134. }
  3135. let partialMatches = matches;
  3136. while (true) {
  3137. let isNonHMR = inFlightDataRoutes == null;
  3138. let routesToUse = inFlightDataRoutes || dataRoutes;
  3139. let localManifest = manifest;
  3140. try {
  3141. await patchRoutesOnNavigationImpl({
  3142. signal,
  3143. path: pathname,
  3144. matches: partialMatches,
  3145. fetcherKey,
  3146. patch: (routeId, children) => {
  3147. if (signal.aborted) return;
  3148. patchRoutesImpl(routeId, children, routesToUse, localManifest, mapRouteProperties);
  3149. }
  3150. });
  3151. } catch (e) {
  3152. return {
  3153. type: "error",
  3154. error: e,
  3155. partialMatches
  3156. };
  3157. } finally {
  3158. // If we are not in the middle of an HMR revalidation and we changed the
  3159. // routes, provide a new identity so when we `updateState` at the end of
  3160. // this navigation/fetch `router.routes` will be a new identity and
  3161. // trigger a re-run of memoized `router.routes` dependencies.
  3162. // HMR will already update the identity and reflow when it lands
  3163. // `inFlightDataRoutes` in `completeNavigation`
  3164. if (isNonHMR && !signal.aborted) {
  3165. dataRoutes = [...dataRoutes];
  3166. }
  3167. }
  3168. if (signal.aborted) {
  3169. return {
  3170. type: "aborted"
  3171. };
  3172. }
  3173. let newMatches = matchRoutes(routesToUse, pathname, basename);
  3174. if (newMatches) {
  3175. return {
  3176. type: "success",
  3177. matches: newMatches
  3178. };
  3179. }
  3180. let newPartialMatches = matchRoutesImpl(routesToUse, pathname, basename, true);
  3181. // Avoid loops if the second pass results in the same partial matches
  3182. if (!newPartialMatches || partialMatches.length === newPartialMatches.length && partialMatches.every((m, i) => m.route.id === newPartialMatches[i].route.id)) {
  3183. return {
  3184. type: "success",
  3185. matches: null
  3186. };
  3187. }
  3188. partialMatches = newPartialMatches;
  3189. }
  3190. }
  3191. function _internalSetRoutes(newRoutes) {
  3192. manifest = {};
  3193. inFlightDataRoutes = convertRoutesToDataRoutes(newRoutes, mapRouteProperties, undefined, manifest);
  3194. }
  3195. function patchRoutes(routeId, children) {
  3196. let isNonHMR = inFlightDataRoutes == null;
  3197. let routesToUse = inFlightDataRoutes || dataRoutes;
  3198. patchRoutesImpl(routeId, children, routesToUse, manifest, mapRouteProperties);
  3199. // If we are not in the middle of an HMR revalidation and we changed the
  3200. // routes, provide a new identity and trigger a reflow via `updateState`
  3201. // to re-run memoized `router.routes` dependencies.
  3202. // HMR will already update the identity and reflow when it lands
  3203. // `inFlightDataRoutes` in `completeNavigation`
  3204. if (isNonHMR) {
  3205. dataRoutes = [...dataRoutes];
  3206. updateState({});
  3207. }
  3208. }
  3209. router = {
  3210. get basename() {
  3211. return basename;
  3212. },
  3213. get future() {
  3214. return future;
  3215. },
  3216. get state() {
  3217. return state;
  3218. },
  3219. get routes() {
  3220. return dataRoutes;
  3221. },
  3222. get window() {
  3223. return routerWindow;
  3224. },
  3225. initialize,
  3226. subscribe,
  3227. enableScrollRestoration,
  3228. navigate,
  3229. fetch,
  3230. revalidate,
  3231. // Passthrough to history-aware createHref used by useHref so we get proper
  3232. // hash-aware URLs in DOM paths
  3233. createHref: to => init.history.createHref(to),
  3234. encodeLocation: to => init.history.encodeLocation(to),
  3235. getFetcher,
  3236. deleteFetcher: deleteFetcherAndUpdateState,
  3237. dispose,
  3238. getBlocker,
  3239. deleteBlocker,
  3240. patchRoutes,
  3241. _internalFetchControllers: fetchControllers,
  3242. _internalActiveDeferreds: activeDeferreds,
  3243. // TODO: Remove setRoutes, it's temporary to avoid dealing with
  3244. // updating the tree while validating the update algorithm.
  3245. _internalSetRoutes
  3246. };
  3247. return router;
  3248. }
  3249. //#endregion
  3250. ////////////////////////////////////////////////////////////////////////////////
  3251. //#region createStaticHandler
  3252. ////////////////////////////////////////////////////////////////////////////////
  3253. const UNSAFE_DEFERRED_SYMBOL = Symbol("deferred");
  3254. function createStaticHandler(routes, opts) {
  3255. invariant(routes.length > 0, "You must provide a non-empty routes array to createStaticHandler");
  3256. let manifest = {};
  3257. let basename = (opts ? opts.basename : null) || "/";
  3258. let mapRouteProperties;
  3259. if (opts != null && opts.mapRouteProperties) {
  3260. mapRouteProperties = opts.mapRouteProperties;
  3261. } else if (opts != null && opts.detectErrorBoundary) {
  3262. // If they are still using the deprecated version, wrap it with the new API
  3263. let detectErrorBoundary = opts.detectErrorBoundary;
  3264. mapRouteProperties = route => ({
  3265. hasErrorBoundary: detectErrorBoundary(route)
  3266. });
  3267. } else {
  3268. mapRouteProperties = defaultMapRouteProperties;
  3269. }
  3270. // Config driven behavior flags
  3271. let future = _extends({
  3272. v7_relativeSplatPath: false,
  3273. v7_throwAbortReason: false
  3274. }, opts ? opts.future : null);
  3275. let dataRoutes = convertRoutesToDataRoutes(routes, mapRouteProperties, undefined, manifest);
  3276. /**
  3277. * The query() method is intended for document requests, in which we want to
  3278. * call an optional action and potentially multiple loaders for all nested
  3279. * routes. It returns a StaticHandlerContext object, which is very similar
  3280. * to the router state (location, loaderData, actionData, errors, etc.) and
  3281. * also adds SSR-specific information such as the statusCode and headers
  3282. * from action/loaders Responses.
  3283. *
  3284. * It _should_ never throw and should report all errors through the
  3285. * returned context.errors object, properly associating errors to their error
  3286. * boundary. Additionally, it tracks _deepestRenderedBoundaryId which can be
  3287. * used to emulate React error boundaries during SSr by performing a second
  3288. * pass only down to the boundaryId.
  3289. *
  3290. * The one exception where we do not return a StaticHandlerContext is when a
  3291. * redirect response is returned or thrown from any action/loader. We
  3292. * propagate that out and return the raw Response so the HTTP server can
  3293. * return it directly.
  3294. *
  3295. * - `opts.requestContext` is an optional server context that will be passed
  3296. * to actions/loaders in the `context` parameter
  3297. * - `opts.skipLoaderErrorBubbling` is an optional parameter that will prevent
  3298. * the bubbling of errors which allows single-fetch-type implementations
  3299. * where the client will handle the bubbling and we may need to return data
  3300. * for the handling route
  3301. */
  3302. async function query(request, _temp3) {
  3303. let {
  3304. requestContext,
  3305. skipLoaderErrorBubbling,
  3306. dataStrategy
  3307. } = _temp3 === void 0 ? {} : _temp3;
  3308. let url = new URL(request.url);
  3309. let method = request.method;
  3310. let location = createLocation("", createPath(url), null, "default");
  3311. let matches = matchRoutes(dataRoutes, location, basename);
  3312. // SSR supports HEAD requests while SPA doesn't
  3313. if (!isValidMethod(method) && method !== "HEAD") {
  3314. let error = getInternalRouterError(405, {
  3315. method
  3316. });
  3317. let {
  3318. matches: methodNotAllowedMatches,
  3319. route
  3320. } = getShortCircuitMatches(dataRoutes);
  3321. return {
  3322. basename,
  3323. location,
  3324. matches: methodNotAllowedMatches,
  3325. loaderData: {},
  3326. actionData: null,
  3327. errors: {
  3328. [route.id]: error
  3329. },
  3330. statusCode: error.status,
  3331. loaderHeaders: {},
  3332. actionHeaders: {},
  3333. activeDeferreds: null
  3334. };
  3335. } else if (!matches) {
  3336. let error = getInternalRouterError(404, {
  3337. pathname: location.pathname
  3338. });
  3339. let {
  3340. matches: notFoundMatches,
  3341. route
  3342. } = getShortCircuitMatches(dataRoutes);
  3343. return {
  3344. basename,
  3345. location,
  3346. matches: notFoundMatches,
  3347. loaderData: {},
  3348. actionData: null,
  3349. errors: {
  3350. [route.id]: error
  3351. },
  3352. statusCode: error.status,
  3353. loaderHeaders: {},
  3354. actionHeaders: {},
  3355. activeDeferreds: null
  3356. };
  3357. }
  3358. let result = await queryImpl(request, location, matches, requestContext, dataStrategy || null, skipLoaderErrorBubbling === true, null);
  3359. if (isResponse(result)) {
  3360. return result;
  3361. }
  3362. // When returning StaticHandlerContext, we patch back in the location here
  3363. // since we need it for React Context. But this helps keep our submit and
  3364. // loadRouteData operating on a Request instead of a Location
  3365. return _extends({
  3366. location,
  3367. basename
  3368. }, result);
  3369. }
  3370. /**
  3371. * The queryRoute() method is intended for targeted route requests, either
  3372. * for fetch ?_data requests or resource route requests. In this case, we
  3373. * are only ever calling a single action or loader, and we are returning the
  3374. * returned value directly. In most cases, this will be a Response returned
  3375. * from the action/loader, but it may be a primitive or other value as well -
  3376. * and in such cases the calling context should handle that accordingly.
  3377. *
  3378. * We do respect the throw/return differentiation, so if an action/loader
  3379. * throws, then this method will throw the value. This is important so we
  3380. * can do proper boundary identification in Remix where a thrown Response
  3381. * must go to the Catch Boundary but a returned Response is happy-path.
  3382. *
  3383. * One thing to note is that any Router-initiated Errors that make sense
  3384. * to associate with a status code will be thrown as an ErrorResponse
  3385. * instance which include the raw Error, such that the calling context can
  3386. * serialize the error as they see fit while including the proper response
  3387. * code. Examples here are 404 and 405 errors that occur prior to reaching
  3388. * any user-defined loaders.
  3389. *
  3390. * - `opts.routeId` allows you to specify the specific route handler to call.
  3391. * If not provided the handler will determine the proper route by matching
  3392. * against `request.url`
  3393. * - `opts.requestContext` is an optional server context that will be passed
  3394. * to actions/loaders in the `context` parameter
  3395. */
  3396. async function queryRoute(request, _temp4) {
  3397. let {
  3398. routeId,
  3399. requestContext,
  3400. dataStrategy
  3401. } = _temp4 === void 0 ? {} : _temp4;
  3402. let url = new URL(request.url);
  3403. let method = request.method;
  3404. let location = createLocation("", createPath(url), null, "default");
  3405. let matches = matchRoutes(dataRoutes, location, basename);
  3406. // SSR supports HEAD requests while SPA doesn't
  3407. if (!isValidMethod(method) && method !== "HEAD" && method !== "OPTIONS") {
  3408. throw getInternalRouterError(405, {
  3409. method
  3410. });
  3411. } else if (!matches) {
  3412. throw getInternalRouterError(404, {
  3413. pathname: location.pathname
  3414. });
  3415. }
  3416. let match = routeId ? matches.find(m => m.route.id === routeId) : getTargetMatch(matches, location);
  3417. if (routeId && !match) {
  3418. throw getInternalRouterError(403, {
  3419. pathname: location.pathname,
  3420. routeId
  3421. });
  3422. } else if (!match) {
  3423. // This should never hit I don't think?
  3424. throw getInternalRouterError(404, {
  3425. pathname: location.pathname
  3426. });
  3427. }
  3428. let result = await queryImpl(request, location, matches, requestContext, dataStrategy || null, false, match);
  3429. if (isResponse(result)) {
  3430. return result;
  3431. }
  3432. let error = result.errors ? Object.values(result.errors)[0] : undefined;
  3433. if (error !== undefined) {
  3434. // If we got back result.errors, that means the loader/action threw
  3435. // _something_ that wasn't a Response, but it's not guaranteed/required
  3436. // to be an `instanceof Error` either, so we have to use throw here to
  3437. // preserve the "error" state outside of queryImpl.
  3438. throw error;
  3439. }
  3440. // Pick off the right state value to return
  3441. if (result.actionData) {
  3442. return Object.values(result.actionData)[0];
  3443. }
  3444. if (result.loaderData) {
  3445. var _result$activeDeferre;
  3446. let data = Object.values(result.loaderData)[0];
  3447. if ((_result$activeDeferre = result.activeDeferreds) != null && _result$activeDeferre[match.route.id]) {
  3448. data[UNSAFE_DEFERRED_SYMBOL] = result.activeDeferreds[match.route.id];
  3449. }
  3450. return data;
  3451. }
  3452. return undefined;
  3453. }
  3454. async function queryImpl(request, location, matches, requestContext, dataStrategy, skipLoaderErrorBubbling, routeMatch) {
  3455. invariant(request.signal, "query()/queryRoute() requests must contain an AbortController signal");
  3456. try {
  3457. if (isMutationMethod(request.method.toLowerCase())) {
  3458. let result = await submit(request, matches, routeMatch || getTargetMatch(matches, location), requestContext, dataStrategy, skipLoaderErrorBubbling, routeMatch != null);
  3459. return result;
  3460. }
  3461. let result = await loadRouteData(request, matches, requestContext, dataStrategy, skipLoaderErrorBubbling, routeMatch);
  3462. return isResponse(result) ? result : _extends({}, result, {
  3463. actionData: null,
  3464. actionHeaders: {}
  3465. });
  3466. } catch (e) {
  3467. // If the user threw/returned a Response in callLoaderOrAction for a
  3468. // `queryRoute` call, we throw the `DataStrategyResult` to bail out early
  3469. // and then return or throw the raw Response here accordingly
  3470. if (isDataStrategyResult(e) && isResponse(e.result)) {
  3471. if (e.type === ResultType.error) {
  3472. throw e.result;
  3473. }
  3474. return e.result;
  3475. }
  3476. // Redirects are always returned since they don't propagate to catch
  3477. // boundaries
  3478. if (isRedirectResponse(e)) {
  3479. return e;
  3480. }
  3481. throw e;
  3482. }
  3483. }
  3484. async function submit(request, matches, actionMatch, requestContext, dataStrategy, skipLoaderErrorBubbling, isRouteRequest) {
  3485. let result;
  3486. if (!actionMatch.route.action && !actionMatch.route.lazy) {
  3487. let error = getInternalRouterError(405, {
  3488. method: request.method,
  3489. pathname: new URL(request.url).pathname,
  3490. routeId: actionMatch.route.id
  3491. });
  3492. if (isRouteRequest) {
  3493. throw error;
  3494. }
  3495. result = {
  3496. type: ResultType.error,
  3497. error
  3498. };
  3499. } else {
  3500. let results = await callDataStrategy("action", request, [actionMatch], matches, isRouteRequest, requestContext, dataStrategy);
  3501. result = results[actionMatch.route.id];
  3502. if (request.signal.aborted) {
  3503. throwStaticHandlerAbortedError(request, isRouteRequest, future);
  3504. }
  3505. }
  3506. if (isRedirectResult(result)) {
  3507. // Uhhhh - this should never happen, we should always throw these from
  3508. // callLoaderOrAction, but the type narrowing here keeps TS happy and we
  3509. // can get back on the "throw all redirect responses" train here should
  3510. // this ever happen :/
  3511. throw new Response(null, {
  3512. status: result.response.status,
  3513. headers: {
  3514. Location: result.response.headers.get("Location")
  3515. }
  3516. });
  3517. }
  3518. if (isDeferredResult(result)) {
  3519. let error = getInternalRouterError(400, {
  3520. type: "defer-action"
  3521. });
  3522. if (isRouteRequest) {
  3523. throw error;
  3524. }
  3525. result = {
  3526. type: ResultType.error,
  3527. error
  3528. };
  3529. }
  3530. if (isRouteRequest) {
  3531. // Note: This should only be non-Response values if we get here, since
  3532. // isRouteRequest should throw any Response received in callLoaderOrAction
  3533. if (isErrorResult(result)) {
  3534. throw result.error;
  3535. }
  3536. return {
  3537. matches: [actionMatch],
  3538. loaderData: {},
  3539. actionData: {
  3540. [actionMatch.route.id]: result.data
  3541. },
  3542. errors: null,
  3543. // Note: statusCode + headers are unused here since queryRoute will
  3544. // return the raw Response or value
  3545. statusCode: 200,
  3546. loaderHeaders: {},
  3547. actionHeaders: {},
  3548. activeDeferreds: null
  3549. };
  3550. }
  3551. // Create a GET request for the loaders
  3552. let loaderRequest = new Request(request.url, {
  3553. headers: request.headers,
  3554. redirect: request.redirect,
  3555. signal: request.signal
  3556. });
  3557. if (isErrorResult(result)) {
  3558. // Store off the pending error - we use it to determine which loaders
  3559. // to call and will commit it when we complete the navigation
  3560. let boundaryMatch = skipLoaderErrorBubbling ? actionMatch : findNearestBoundary(matches, actionMatch.route.id);
  3561. let context = await loadRouteData(loaderRequest, matches, requestContext, dataStrategy, skipLoaderErrorBubbling, null, [boundaryMatch.route.id, result]);
  3562. // action status codes take precedence over loader status codes
  3563. return _extends({}, context, {
  3564. statusCode: isRouteErrorResponse(result.error) ? result.error.status : result.statusCode != null ? result.statusCode : 500,
  3565. actionData: null,
  3566. actionHeaders: _extends({}, result.headers ? {
  3567. [actionMatch.route.id]: result.headers
  3568. } : {})
  3569. });
  3570. }
  3571. let context = await loadRouteData(loaderRequest, matches, requestContext, dataStrategy, skipLoaderErrorBubbling, null);
  3572. return _extends({}, context, {
  3573. actionData: {
  3574. [actionMatch.route.id]: result.data
  3575. }
  3576. }, result.statusCode ? {
  3577. statusCode: result.statusCode
  3578. } : {}, {
  3579. actionHeaders: result.headers ? {
  3580. [actionMatch.route.id]: result.headers
  3581. } : {}
  3582. });
  3583. }
  3584. async function loadRouteData(request, matches, requestContext, dataStrategy, skipLoaderErrorBubbling, routeMatch, pendingActionResult) {
  3585. let isRouteRequest = routeMatch != null;
  3586. // Short circuit if we have no loaders to run (queryRoute())
  3587. if (isRouteRequest && !(routeMatch != null && routeMatch.route.loader) && !(routeMatch != null && routeMatch.route.lazy)) {
  3588. throw getInternalRouterError(400, {
  3589. method: request.method,
  3590. pathname: new URL(request.url).pathname,
  3591. routeId: routeMatch == null ? void 0 : routeMatch.route.id
  3592. });
  3593. }
  3594. let requestMatches = routeMatch ? [routeMatch] : pendingActionResult && isErrorResult(pendingActionResult[1]) ? getLoaderMatchesUntilBoundary(matches, pendingActionResult[0]) : matches;
  3595. let matchesToLoad = requestMatches.filter(m => m.route.loader || m.route.lazy);
  3596. // Short circuit if we have no loaders to run (query())
  3597. if (matchesToLoad.length === 0) {
  3598. return {
  3599. matches,
  3600. // Add a null for all matched routes for proper revalidation on the client
  3601. loaderData: matches.reduce((acc, m) => Object.assign(acc, {
  3602. [m.route.id]: null
  3603. }), {}),
  3604. errors: pendingActionResult && isErrorResult(pendingActionResult[1]) ? {
  3605. [pendingActionResult[0]]: pendingActionResult[1].error
  3606. } : null,
  3607. statusCode: 200,
  3608. loaderHeaders: {},
  3609. activeDeferreds: null
  3610. };
  3611. }
  3612. let results = await callDataStrategy("loader", request, matchesToLoad, matches, isRouteRequest, requestContext, dataStrategy);
  3613. if (request.signal.aborted) {
  3614. throwStaticHandlerAbortedError(request, isRouteRequest, future);
  3615. }
  3616. // Process and commit output from loaders
  3617. let activeDeferreds = new Map();
  3618. let context = processRouteLoaderData(matches, results, pendingActionResult, activeDeferreds, skipLoaderErrorBubbling);
  3619. // Add a null for any non-loader matches for proper revalidation on the client
  3620. let executedLoaders = new Set(matchesToLoad.map(match => match.route.id));
  3621. matches.forEach(match => {
  3622. if (!executedLoaders.has(match.route.id)) {
  3623. context.loaderData[match.route.id] = null;
  3624. }
  3625. });
  3626. return _extends({}, context, {
  3627. matches,
  3628. activeDeferreds: activeDeferreds.size > 0 ? Object.fromEntries(activeDeferreds.entries()) : null
  3629. });
  3630. }
  3631. // Utility wrapper for calling dataStrategy server-side without having to
  3632. // pass around the manifest, mapRouteProperties, etc.
  3633. async function callDataStrategy(type, request, matchesToLoad, matches, isRouteRequest, requestContext, dataStrategy) {
  3634. let results = await callDataStrategyImpl(dataStrategy || defaultDataStrategy, type, null, request, matchesToLoad, matches, null, manifest, mapRouteProperties, requestContext);
  3635. let dataResults = {};
  3636. await Promise.all(matches.map(async match => {
  3637. if (!(match.route.id in results)) {
  3638. return;
  3639. }
  3640. let result = results[match.route.id];
  3641. if (isRedirectDataStrategyResultResult(result)) {
  3642. let response = result.result;
  3643. // Throw redirects and let the server handle them with an HTTP redirect
  3644. throw normalizeRelativeRoutingRedirectResponse(response, request, match.route.id, matches, basename, future.v7_relativeSplatPath);
  3645. }
  3646. if (isResponse(result.result) && isRouteRequest) {
  3647. // For SSR single-route requests, we want to hand Responses back
  3648. // directly without unwrapping
  3649. throw result;
  3650. }
  3651. dataResults[match.route.id] = await convertDataStrategyResultToDataResult(result);
  3652. }));
  3653. return dataResults;
  3654. }
  3655. return {
  3656. dataRoutes,
  3657. query,
  3658. queryRoute
  3659. };
  3660. }
  3661. //#endregion
  3662. ////////////////////////////////////////////////////////////////////////////////
  3663. //#region Helpers
  3664. ////////////////////////////////////////////////////////////////////////////////
  3665. /**
  3666. * Given an existing StaticHandlerContext and an error thrown at render time,
  3667. * provide an updated StaticHandlerContext suitable for a second SSR render
  3668. */
  3669. function getStaticContextFromError(routes, context, error) {
  3670. let newContext = _extends({}, context, {
  3671. statusCode: isRouteErrorResponse(error) ? error.status : 500,
  3672. errors: {
  3673. [context._deepestRenderedBoundaryId || routes[0].id]: error
  3674. }
  3675. });
  3676. return newContext;
  3677. }
  3678. function throwStaticHandlerAbortedError(request, isRouteRequest, future) {
  3679. if (future.v7_throwAbortReason && request.signal.reason !== undefined) {
  3680. throw request.signal.reason;
  3681. }
  3682. let method = isRouteRequest ? "queryRoute" : "query";
  3683. throw new Error(method + "() call aborted: " + request.method + " " + request.url);
  3684. }
  3685. function isSubmissionNavigation(opts) {
  3686. return opts != null && ("formData" in opts && opts.formData != null || "body" in opts && opts.body !== undefined);
  3687. }
  3688. function normalizeTo(location, matches, basename, prependBasename, to, v7_relativeSplatPath, fromRouteId, relative) {
  3689. let contextualMatches;
  3690. let activeRouteMatch;
  3691. if (fromRouteId) {
  3692. // Grab matches up to the calling route so our route-relative logic is
  3693. // relative to the correct source route
  3694. contextualMatches = [];
  3695. for (let match of matches) {
  3696. contextualMatches.push(match);
  3697. if (match.route.id === fromRouteId) {
  3698. activeRouteMatch = match;
  3699. break;
  3700. }
  3701. }
  3702. } else {
  3703. contextualMatches = matches;
  3704. activeRouteMatch = matches[matches.length - 1];
  3705. }
  3706. // Resolve the relative path
  3707. let path = resolveTo(to ? to : ".", getResolveToMatches(contextualMatches, v7_relativeSplatPath), stripBasename(location.pathname, basename) || location.pathname, relative === "path");
  3708. // When `to` is not specified we inherit search/hash from the current
  3709. // location, unlike when to="." and we just inherit the path.
  3710. // See https://github.com/remix-run/remix/issues/927
  3711. if (to == null) {
  3712. path.search = location.search;
  3713. path.hash = location.hash;
  3714. }
  3715. // Account for `?index` params when routing to the current location
  3716. if ((to == null || to === "" || to === ".") && activeRouteMatch) {
  3717. let nakedIndex = hasNakedIndexQuery(path.search);
  3718. if (activeRouteMatch.route.index && !nakedIndex) {
  3719. // Add one when we're targeting an index route
  3720. path.search = path.search ? path.search.replace(/^\?/, "?index&") : "?index";
  3721. } else if (!activeRouteMatch.route.index && nakedIndex) {
  3722. // Remove existing ones when we're not
  3723. let params = new URLSearchParams(path.search);
  3724. let indexValues = params.getAll("index");
  3725. params.delete("index");
  3726. indexValues.filter(v => v).forEach(v => params.append("index", v));
  3727. let qs = params.toString();
  3728. path.search = qs ? "?" + qs : "";
  3729. }
  3730. }
  3731. // If we're operating within a basename, prepend it to the pathname. If
  3732. // this is a root navigation, then just use the raw basename which allows
  3733. // the basename to have full control over the presence of a trailing slash
  3734. // on root actions
  3735. if (prependBasename && basename !== "/") {
  3736. path.pathname = path.pathname === "/" ? basename : joinPaths([basename, path.pathname]);
  3737. }
  3738. return createPath(path);
  3739. }
  3740. // Normalize navigation options by converting formMethod=GET formData objects to
  3741. // URLSearchParams so they behave identically to links with query params
  3742. function normalizeNavigateOptions(normalizeFormMethod, isFetcher, path, opts) {
  3743. // Return location verbatim on non-submission navigations
  3744. if (!opts || !isSubmissionNavigation(opts)) {
  3745. return {
  3746. path
  3747. };
  3748. }
  3749. if (opts.formMethod && !isValidMethod(opts.formMethod)) {
  3750. return {
  3751. path,
  3752. error: getInternalRouterError(405, {
  3753. method: opts.formMethod
  3754. })
  3755. };
  3756. }
  3757. let getInvalidBodyError = () => ({
  3758. path,
  3759. error: getInternalRouterError(400, {
  3760. type: "invalid-body"
  3761. })
  3762. });
  3763. // Create a Submission on non-GET navigations
  3764. let rawFormMethod = opts.formMethod || "get";
  3765. let formMethod = normalizeFormMethod ? rawFormMethod.toUpperCase() : rawFormMethod.toLowerCase();
  3766. let formAction = stripHashFromPath(path);
  3767. if (opts.body !== undefined) {
  3768. if (opts.formEncType === "text/plain") {
  3769. // text only support POST/PUT/PATCH/DELETE submissions
  3770. if (!isMutationMethod(formMethod)) {
  3771. return getInvalidBodyError();
  3772. }
  3773. let text = typeof opts.body === "string" ? opts.body : opts.body instanceof FormData || opts.body instanceof URLSearchParams ?
  3774. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#plain-text-form-data
  3775. Array.from(opts.body.entries()).reduce((acc, _ref3) => {
  3776. let [name, value] = _ref3;
  3777. return "" + acc + name + "=" + value + "\n";
  3778. }, "") : String(opts.body);
  3779. return {
  3780. path,
  3781. submission: {
  3782. formMethod,
  3783. formAction,
  3784. formEncType: opts.formEncType,
  3785. formData: undefined,
  3786. json: undefined,
  3787. text
  3788. }
  3789. };
  3790. } else if (opts.formEncType === "application/json") {
  3791. // json only supports POST/PUT/PATCH/DELETE submissions
  3792. if (!isMutationMethod(formMethod)) {
  3793. return getInvalidBodyError();
  3794. }
  3795. try {
  3796. let json = typeof opts.body === "string" ? JSON.parse(opts.body) : opts.body;
  3797. return {
  3798. path,
  3799. submission: {
  3800. formMethod,
  3801. formAction,
  3802. formEncType: opts.formEncType,
  3803. formData: undefined,
  3804. json,
  3805. text: undefined
  3806. }
  3807. };
  3808. } catch (e) {
  3809. return getInvalidBodyError();
  3810. }
  3811. }
  3812. }
  3813. invariant(typeof FormData === "function", "FormData is not available in this environment");
  3814. let searchParams;
  3815. let formData;
  3816. if (opts.formData) {
  3817. searchParams = convertFormDataToSearchParams(opts.formData);
  3818. formData = opts.formData;
  3819. } else if (opts.body instanceof FormData) {
  3820. searchParams = convertFormDataToSearchParams(opts.body);
  3821. formData = opts.body;
  3822. } else if (opts.body instanceof URLSearchParams) {
  3823. searchParams = opts.body;
  3824. formData = convertSearchParamsToFormData(searchParams);
  3825. } else if (opts.body == null) {
  3826. searchParams = new URLSearchParams();
  3827. formData = new FormData();
  3828. } else {
  3829. try {
  3830. searchParams = new URLSearchParams(opts.body);
  3831. formData = convertSearchParamsToFormData(searchParams);
  3832. } catch (e) {
  3833. return getInvalidBodyError();
  3834. }
  3835. }
  3836. let submission = {
  3837. formMethod,
  3838. formAction,
  3839. formEncType: opts && opts.formEncType || "application/x-www-form-urlencoded",
  3840. formData,
  3841. json: undefined,
  3842. text: undefined
  3843. };
  3844. if (isMutationMethod(submission.formMethod)) {
  3845. return {
  3846. path,
  3847. submission
  3848. };
  3849. }
  3850. // Flatten submission onto URLSearchParams for GET submissions
  3851. let parsedPath = parsePath(path);
  3852. // On GET navigation submissions we can drop the ?index param from the
  3853. // resulting location since all loaders will run. But fetcher GET submissions
  3854. // only run a single loader so we need to preserve any incoming ?index params
  3855. if (isFetcher && parsedPath.search && hasNakedIndexQuery(parsedPath.search)) {
  3856. searchParams.append("index", "");
  3857. }
  3858. parsedPath.search = "?" + searchParams;
  3859. return {
  3860. path: createPath(parsedPath),
  3861. submission
  3862. };
  3863. }
  3864. // Filter out all routes at/below any caught error as they aren't going to
  3865. // render so we don't need to load them
  3866. function getLoaderMatchesUntilBoundary(matches, boundaryId, includeBoundary) {
  3867. if (includeBoundary === void 0) {
  3868. includeBoundary = false;
  3869. }
  3870. let index = matches.findIndex(m => m.route.id === boundaryId);
  3871. if (index >= 0) {
  3872. return matches.slice(0, includeBoundary ? index + 1 : index);
  3873. }
  3874. return matches;
  3875. }
  3876. function getMatchesToLoad(history, state, matches, submission, location, initialHydration, skipActionErrorRevalidation, isRevalidationRequired, cancelledDeferredRoutes, cancelledFetcherLoads, deletedFetchers, fetchLoadMatches, fetchRedirectIds, routesToUse, basename, pendingActionResult) {
  3877. let actionResult = pendingActionResult ? isErrorResult(pendingActionResult[1]) ? pendingActionResult[1].error : pendingActionResult[1].data : undefined;
  3878. let currentUrl = history.createURL(state.location);
  3879. let nextUrl = history.createURL(location);
  3880. // Pick navigation matches that are net-new or qualify for revalidation
  3881. let boundaryMatches = matches;
  3882. if (initialHydration && state.errors) {
  3883. // On initial hydration, only consider matches up to _and including_ the boundary.
  3884. // This is inclusive to handle cases where a server loader ran successfully,
  3885. // a child server loader bubbled up to this route, but this route has
  3886. // `clientLoader.hydrate` so we want to still run the `clientLoader` so that
  3887. // we have a complete version of `loaderData`
  3888. boundaryMatches = getLoaderMatchesUntilBoundary(matches, Object.keys(state.errors)[0], true);
  3889. } else if (pendingActionResult && isErrorResult(pendingActionResult[1])) {
  3890. // If an action threw an error, we call loaders up to, but not including the
  3891. // boundary
  3892. boundaryMatches = getLoaderMatchesUntilBoundary(matches, pendingActionResult[0]);
  3893. }
  3894. // Don't revalidate loaders by default after action 4xx/5xx responses
  3895. // when the flag is enabled. They can still opt-into revalidation via
  3896. // `shouldRevalidate` via `actionResult`
  3897. let actionStatus = pendingActionResult ? pendingActionResult[1].statusCode : undefined;
  3898. let shouldSkipRevalidation = skipActionErrorRevalidation && actionStatus && actionStatus >= 400;
  3899. let navigationMatches = boundaryMatches.filter((match, index) => {
  3900. let {
  3901. route
  3902. } = match;
  3903. if (route.lazy) {
  3904. // We haven't loaded this route yet so we don't know if it's got a loader!
  3905. return true;
  3906. }
  3907. if (route.loader == null) {
  3908. return false;
  3909. }
  3910. if (initialHydration) {
  3911. return shouldLoadRouteOnHydration(route, state.loaderData, state.errors);
  3912. }
  3913. // Always call the loader on new route instances and pending defer cancellations
  3914. if (isNewLoader(state.loaderData, state.matches[index], match) || cancelledDeferredRoutes.some(id => id === match.route.id)) {
  3915. return true;
  3916. }
  3917. // This is the default implementation for when we revalidate. If the route
  3918. // provides it's own implementation, then we give them full control but
  3919. // provide this value so they can leverage it if needed after they check
  3920. // their own specific use cases
  3921. let currentRouteMatch = state.matches[index];
  3922. let nextRouteMatch = match;
  3923. return shouldRevalidateLoader(match, _extends({
  3924. currentUrl,
  3925. currentParams: currentRouteMatch.params,
  3926. nextUrl,
  3927. nextParams: nextRouteMatch.params
  3928. }, submission, {
  3929. actionResult,
  3930. actionStatus,
  3931. defaultShouldRevalidate: shouldSkipRevalidation ? false :
  3932. // Forced revalidation due to submission, useRevalidator, or X-Remix-Revalidate
  3933. isRevalidationRequired || currentUrl.pathname + currentUrl.search === nextUrl.pathname + nextUrl.search ||
  3934. // Search params affect all loaders
  3935. currentUrl.search !== nextUrl.search || isNewRouteInstance(currentRouteMatch, nextRouteMatch)
  3936. }));
  3937. });
  3938. // Pick fetcher.loads that need to be revalidated
  3939. let revalidatingFetchers = [];
  3940. fetchLoadMatches.forEach((f, key) => {
  3941. // Don't revalidate:
  3942. // - on initial hydration (shouldn't be any fetchers then anyway)
  3943. // - if fetcher won't be present in the subsequent render
  3944. // - no longer matches the URL (v7_fetcherPersist=false)
  3945. // - was unmounted but persisted due to v7_fetcherPersist=true
  3946. if (initialHydration || !matches.some(m => m.route.id === f.routeId) || deletedFetchers.has(key)) {
  3947. return;
  3948. }
  3949. let fetcherMatches = matchRoutes(routesToUse, f.path, basename);
  3950. // If the fetcher path no longer matches, push it in with null matches so
  3951. // we can trigger a 404 in callLoadersAndMaybeResolveData. Note this is
  3952. // currently only a use-case for Remix HMR where the route tree can change
  3953. // at runtime and remove a route previously loaded via a fetcher
  3954. if (!fetcherMatches) {
  3955. revalidatingFetchers.push({
  3956. key,
  3957. routeId: f.routeId,
  3958. path: f.path,
  3959. matches: null,
  3960. match: null,
  3961. controller: null
  3962. });
  3963. return;
  3964. }
  3965. // Revalidating fetchers are decoupled from the route matches since they
  3966. // load from a static href. They revalidate based on explicit revalidation
  3967. // (submission, useRevalidator, or X-Remix-Revalidate)
  3968. let fetcher = state.fetchers.get(key);
  3969. let fetcherMatch = getTargetMatch(fetcherMatches, f.path);
  3970. let shouldRevalidate = false;
  3971. if (fetchRedirectIds.has(key)) {
  3972. // Never trigger a revalidation of an actively redirecting fetcher
  3973. shouldRevalidate = false;
  3974. } else if (cancelledFetcherLoads.has(key)) {
  3975. // Always mark for revalidation if the fetcher was cancelled
  3976. cancelledFetcherLoads.delete(key);
  3977. shouldRevalidate = true;
  3978. } else if (fetcher && fetcher.state !== "idle" && fetcher.data === undefined) {
  3979. // If the fetcher hasn't ever completed loading yet, then this isn't a
  3980. // revalidation, it would just be a brand new load if an explicit
  3981. // revalidation is required
  3982. shouldRevalidate = isRevalidationRequired;
  3983. } else {
  3984. // Otherwise fall back on any user-defined shouldRevalidate, defaulting
  3985. // to explicit revalidations only
  3986. shouldRevalidate = shouldRevalidateLoader(fetcherMatch, _extends({
  3987. currentUrl,
  3988. currentParams: state.matches[state.matches.length - 1].params,
  3989. nextUrl,
  3990. nextParams: matches[matches.length - 1].params
  3991. }, submission, {
  3992. actionResult,
  3993. actionStatus,
  3994. defaultShouldRevalidate: shouldSkipRevalidation ? false : isRevalidationRequired
  3995. }));
  3996. }
  3997. if (shouldRevalidate) {
  3998. revalidatingFetchers.push({
  3999. key,
  4000. routeId: f.routeId,
  4001. path: f.path,
  4002. matches: fetcherMatches,
  4003. match: fetcherMatch,
  4004. controller: new AbortController()
  4005. });
  4006. }
  4007. });
  4008. return [navigationMatches, revalidatingFetchers];
  4009. }
  4010. function shouldLoadRouteOnHydration(route, loaderData, errors) {
  4011. // We dunno if we have a loader - gotta find out!
  4012. if (route.lazy) {
  4013. return true;
  4014. }
  4015. // No loader, nothing to initialize
  4016. if (!route.loader) {
  4017. return false;
  4018. }
  4019. let hasData = loaderData != null && loaderData[route.id] !== undefined;
  4020. let hasError = errors != null && errors[route.id] !== undefined;
  4021. // Don't run if we error'd during SSR
  4022. if (!hasData && hasError) {
  4023. return false;
  4024. }
  4025. // Explicitly opting-in to running on hydration
  4026. if (typeof route.loader === "function" && route.loader.hydrate === true) {
  4027. return true;
  4028. }
  4029. // Otherwise, run if we're not yet initialized with anything
  4030. return !hasData && !hasError;
  4031. }
  4032. function isNewLoader(currentLoaderData, currentMatch, match) {
  4033. let isNew =
  4034. // [a] -> [a, b]
  4035. !currentMatch ||
  4036. // [a, b] -> [a, c]
  4037. match.route.id !== currentMatch.route.id;
  4038. // Handle the case that we don't have data for a re-used route, potentially
  4039. // from a prior error or from a cancelled pending deferred
  4040. let isMissingData = currentLoaderData[match.route.id] === undefined;
  4041. // Always load if this is a net-new route or we don't yet have data
  4042. return isNew || isMissingData;
  4043. }
  4044. function isNewRouteInstance(currentMatch, match) {
  4045. let currentPath = currentMatch.route.path;
  4046. return (
  4047. // param change for this match, /users/123 -> /users/456
  4048. currentMatch.pathname !== match.pathname ||
  4049. // splat param changed, which is not present in match.path
  4050. // e.g. /files/images/avatar.jpg -> files/finances.xls
  4051. currentPath != null && currentPath.endsWith("*") && currentMatch.params["*"] !== match.params["*"]
  4052. );
  4053. }
  4054. function shouldRevalidateLoader(loaderMatch, arg) {
  4055. if (loaderMatch.route.shouldRevalidate) {
  4056. let routeChoice = loaderMatch.route.shouldRevalidate(arg);
  4057. if (typeof routeChoice === "boolean") {
  4058. return routeChoice;
  4059. }
  4060. }
  4061. return arg.defaultShouldRevalidate;
  4062. }
  4063. function patchRoutesImpl(routeId, children, routesToUse, manifest, mapRouteProperties) {
  4064. var _childrenToPatch;
  4065. let childrenToPatch;
  4066. if (routeId) {
  4067. let route = manifest[routeId];
  4068. invariant(route, "No route found to patch children into: routeId = " + routeId);
  4069. if (!route.children) {
  4070. route.children = [];
  4071. }
  4072. childrenToPatch = route.children;
  4073. } else {
  4074. childrenToPatch = routesToUse;
  4075. }
  4076. // Don't patch in routes we already know about so that `patch` is idempotent
  4077. // to simplify user-land code. This is useful because we re-call the
  4078. // `patchRoutesOnNavigation` function for matched routes with params.
  4079. let uniqueChildren = children.filter(newRoute => !childrenToPatch.some(existingRoute => isSameRoute(newRoute, existingRoute)));
  4080. let newRoutes = convertRoutesToDataRoutes(uniqueChildren, mapRouteProperties, [routeId || "_", "patch", String(((_childrenToPatch = childrenToPatch) == null ? void 0 : _childrenToPatch.length) || "0")], manifest);
  4081. childrenToPatch.push(...newRoutes);
  4082. }
  4083. function isSameRoute(newRoute, existingRoute) {
  4084. // Most optimal check is by id
  4085. if ("id" in newRoute && "id" in existingRoute && newRoute.id === existingRoute.id) {
  4086. return true;
  4087. }
  4088. // Second is by pathing differences
  4089. if (!(newRoute.index === existingRoute.index && newRoute.path === existingRoute.path && newRoute.caseSensitive === existingRoute.caseSensitive)) {
  4090. return false;
  4091. }
  4092. // Pathless layout routes are trickier since we need to check children.
  4093. // If they have no children then they're the same as far as we can tell
  4094. if ((!newRoute.children || newRoute.children.length === 0) && (!existingRoute.children || existingRoute.children.length === 0)) {
  4095. return true;
  4096. }
  4097. // Otherwise, we look to see if every child in the new route is already
  4098. // represented in the existing route's children
  4099. return newRoute.children.every((aChild, i) => {
  4100. var _existingRoute$childr;
  4101. return (_existingRoute$childr = existingRoute.children) == null ? void 0 : _existingRoute$childr.some(bChild => isSameRoute(aChild, bChild));
  4102. });
  4103. }
  4104. /**
  4105. * Execute route.lazy() methods to lazily load route modules (loader, action,
  4106. * shouldRevalidate) and update the routeManifest in place which shares objects
  4107. * with dataRoutes so those get updated as well.
  4108. */
  4109. async function loadLazyRouteModule(route, mapRouteProperties, manifest) {
  4110. if (!route.lazy) {
  4111. return;
  4112. }
  4113. let lazyRoute = await route.lazy();
  4114. // If the lazy route function was executed and removed by another parallel
  4115. // call then we can return - first lazy() to finish wins because the return
  4116. // value of lazy is expected to be static
  4117. if (!route.lazy) {
  4118. return;
  4119. }
  4120. let routeToUpdate = manifest[route.id];
  4121. invariant(routeToUpdate, "No route found in manifest");
  4122. // Update the route in place. This should be safe because there's no way
  4123. // we could yet be sitting on this route as we can't get there without
  4124. // resolving lazy() first.
  4125. //
  4126. // This is different than the HMR "update" use-case where we may actively be
  4127. // on the route being updated. The main concern boils down to "does this
  4128. // mutation affect any ongoing navigations or any current state.matches
  4129. // values?". If not, it should be safe to update in place.
  4130. let routeUpdates = {};
  4131. for (let lazyRouteProperty in lazyRoute) {
  4132. let staticRouteValue = routeToUpdate[lazyRouteProperty];
  4133. let isPropertyStaticallyDefined = staticRouteValue !== undefined &&
  4134. // This property isn't static since it should always be updated based
  4135. // on the route updates
  4136. lazyRouteProperty !== "hasErrorBoundary";
  4137. warning(!isPropertyStaticallyDefined, "Route \"" + routeToUpdate.id + "\" has a static property \"" + lazyRouteProperty + "\" " + "defined but its lazy function is also returning a value for this property. " + ("The lazy route property \"" + lazyRouteProperty + "\" will be ignored."));
  4138. if (!isPropertyStaticallyDefined && !immutableRouteKeys.has(lazyRouteProperty)) {
  4139. routeUpdates[lazyRouteProperty] = lazyRoute[lazyRouteProperty];
  4140. }
  4141. }
  4142. // Mutate the route with the provided updates. Do this first so we pass
  4143. // the updated version to mapRouteProperties
  4144. Object.assign(routeToUpdate, routeUpdates);
  4145. // Mutate the `hasErrorBoundary` property on the route based on the route
  4146. // updates and remove the `lazy` function so we don't resolve the lazy
  4147. // route again.
  4148. Object.assign(routeToUpdate, _extends({}, mapRouteProperties(routeToUpdate), {
  4149. lazy: undefined
  4150. }));
  4151. }
  4152. // Default implementation of `dataStrategy` which fetches all loaders in parallel
  4153. async function defaultDataStrategy(_ref4) {
  4154. let {
  4155. matches
  4156. } = _ref4;
  4157. let matchesToLoad = matches.filter(m => m.shouldLoad);
  4158. let results = await Promise.all(matchesToLoad.map(m => m.resolve()));
  4159. return results.reduce((acc, result, i) => Object.assign(acc, {
  4160. [matchesToLoad[i].route.id]: result
  4161. }), {});
  4162. }
  4163. async function callDataStrategyImpl(dataStrategyImpl, type, state, request, matchesToLoad, matches, fetcherKey, manifest, mapRouteProperties, requestContext) {
  4164. let loadRouteDefinitionsPromises = matches.map(m => m.route.lazy ? loadLazyRouteModule(m.route, mapRouteProperties, manifest) : undefined);
  4165. let dsMatches = matches.map((match, i) => {
  4166. let loadRoutePromise = loadRouteDefinitionsPromises[i];
  4167. let shouldLoad = matchesToLoad.some(m => m.route.id === match.route.id);
  4168. // `resolve` encapsulates route.lazy(), executing the loader/action,
  4169. // and mapping return values/thrown errors to a `DataStrategyResult`. Users
  4170. // can pass a callback to take fine-grained control over the execution
  4171. // of the loader/action
  4172. let resolve = async handlerOverride => {
  4173. if (handlerOverride && request.method === "GET" && (match.route.lazy || match.route.loader)) {
  4174. shouldLoad = true;
  4175. }
  4176. return shouldLoad ? callLoaderOrAction(type, request, match, loadRoutePromise, handlerOverride, requestContext) : Promise.resolve({
  4177. type: ResultType.data,
  4178. result: undefined
  4179. });
  4180. };
  4181. return _extends({}, match, {
  4182. shouldLoad,
  4183. resolve
  4184. });
  4185. });
  4186. // Send all matches here to allow for a middleware-type implementation.
  4187. // handler will be a no-op for unneeded routes and we filter those results
  4188. // back out below.
  4189. let results = await dataStrategyImpl({
  4190. matches: dsMatches,
  4191. request,
  4192. params: matches[0].params,
  4193. fetcherKey,
  4194. context: requestContext
  4195. });
  4196. // Wait for all routes to load here but 'swallow the error since we want
  4197. // it to bubble up from the `await loadRoutePromise` in `callLoaderOrAction` -
  4198. // called from `match.resolve()`
  4199. try {
  4200. await Promise.all(loadRouteDefinitionsPromises);
  4201. } catch (e) {
  4202. // No-op
  4203. }
  4204. return results;
  4205. }
  4206. // Default logic for calling a loader/action is the user has no specified a dataStrategy
  4207. async function callLoaderOrAction(type, request, match, loadRoutePromise, handlerOverride, staticContext) {
  4208. let result;
  4209. let onReject;
  4210. let runHandler = handler => {
  4211. // Setup a promise we can race against so that abort signals short circuit
  4212. let reject;
  4213. // This will never resolve so safe to type it as Promise<DataStrategyResult> to
  4214. // satisfy the function return value
  4215. let abortPromise = new Promise((_, r) => reject = r);
  4216. onReject = () => reject();
  4217. request.signal.addEventListener("abort", onReject);
  4218. let actualHandler = ctx => {
  4219. if (typeof handler !== "function") {
  4220. return Promise.reject(new Error("You cannot call the handler for a route which defines a boolean " + ("\"" + type + "\" [routeId: " + match.route.id + "]")));
  4221. }
  4222. return handler({
  4223. request,
  4224. params: match.params,
  4225. context: staticContext
  4226. }, ...(ctx !== undefined ? [ctx] : []));
  4227. };
  4228. let handlerPromise = (async () => {
  4229. try {
  4230. let val = await (handlerOverride ? handlerOverride(ctx => actualHandler(ctx)) : actualHandler());
  4231. return {
  4232. type: "data",
  4233. result: val
  4234. };
  4235. } catch (e) {
  4236. return {
  4237. type: "error",
  4238. result: e
  4239. };
  4240. }
  4241. })();
  4242. return Promise.race([handlerPromise, abortPromise]);
  4243. };
  4244. try {
  4245. let handler = match.route[type];
  4246. // If we have a route.lazy promise, await that first
  4247. if (loadRoutePromise) {
  4248. if (handler) {
  4249. // Run statically defined handler in parallel with lazy()
  4250. let handlerError;
  4251. let [value] = await Promise.all([
  4252. // If the handler throws, don't let it immediately bubble out,
  4253. // since we need to let the lazy() execution finish so we know if this
  4254. // route has a boundary that can handle the error
  4255. runHandler(handler).catch(e => {
  4256. handlerError = e;
  4257. }), loadRoutePromise]);
  4258. if (handlerError !== undefined) {
  4259. throw handlerError;
  4260. }
  4261. result = value;
  4262. } else {
  4263. // Load lazy route module, then run any returned handler
  4264. await loadRoutePromise;
  4265. handler = match.route[type];
  4266. if (handler) {
  4267. // Handler still runs even if we got interrupted to maintain consistency
  4268. // with un-abortable behavior of handler execution on non-lazy or
  4269. // previously-lazy-loaded routes
  4270. result = await runHandler(handler);
  4271. } else if (type === "action") {
  4272. let url = new URL(request.url);
  4273. let pathname = url.pathname + url.search;
  4274. throw getInternalRouterError(405, {
  4275. method: request.method,
  4276. pathname,
  4277. routeId: match.route.id
  4278. });
  4279. } else {
  4280. // lazy() route has no loader to run. Short circuit here so we don't
  4281. // hit the invariant below that errors on returning undefined.
  4282. return {
  4283. type: ResultType.data,
  4284. result: undefined
  4285. };
  4286. }
  4287. }
  4288. } else if (!handler) {
  4289. let url = new URL(request.url);
  4290. let pathname = url.pathname + url.search;
  4291. throw getInternalRouterError(404, {
  4292. pathname
  4293. });
  4294. } else {
  4295. result = await runHandler(handler);
  4296. }
  4297. invariant(result.result !== undefined, "You defined " + (type === "action" ? "an action" : "a loader") + " for route " + ("\"" + match.route.id + "\" but didn't return anything from your `" + type + "` ") + "function. Please return a value or `null`.");
  4298. } catch (e) {
  4299. // We should already be catching and converting normal handler executions to
  4300. // DataStrategyResults and returning them, so anything that throws here is an
  4301. // unexpected error we still need to wrap
  4302. return {
  4303. type: ResultType.error,
  4304. result: e
  4305. };
  4306. } finally {
  4307. if (onReject) {
  4308. request.signal.removeEventListener("abort", onReject);
  4309. }
  4310. }
  4311. return result;
  4312. }
  4313. async function convertDataStrategyResultToDataResult(dataStrategyResult) {
  4314. let {
  4315. result,
  4316. type
  4317. } = dataStrategyResult;
  4318. if (isResponse(result)) {
  4319. let data;
  4320. try {
  4321. let contentType = result.headers.get("Content-Type");
  4322. // Check between word boundaries instead of startsWith() due to the last
  4323. // paragraph of https://httpwg.org/specs/rfc9110.html#field.content-type
  4324. if (contentType && /\bapplication\/json\b/.test(contentType)) {
  4325. if (result.body == null) {
  4326. data = null;
  4327. } else {
  4328. data = await result.json();
  4329. }
  4330. } else {
  4331. data = await result.text();
  4332. }
  4333. } catch (e) {
  4334. return {
  4335. type: ResultType.error,
  4336. error: e
  4337. };
  4338. }
  4339. if (type === ResultType.error) {
  4340. return {
  4341. type: ResultType.error,
  4342. error: new ErrorResponseImpl(result.status, result.statusText, data),
  4343. statusCode: result.status,
  4344. headers: result.headers
  4345. };
  4346. }
  4347. return {
  4348. type: ResultType.data,
  4349. data,
  4350. statusCode: result.status,
  4351. headers: result.headers
  4352. };
  4353. }
  4354. if (type === ResultType.error) {
  4355. if (isDataWithResponseInit(result)) {
  4356. var _result$init3, _result$init4;
  4357. if (result.data instanceof Error) {
  4358. var _result$init, _result$init2;
  4359. return {
  4360. type: ResultType.error,
  4361. error: result.data,
  4362. statusCode: (_result$init = result.init) == null ? void 0 : _result$init.status,
  4363. headers: (_result$init2 = result.init) != null && _result$init2.headers ? new Headers(result.init.headers) : undefined
  4364. };
  4365. }
  4366. // Convert thrown data() to ErrorResponse instances
  4367. return {
  4368. type: ResultType.error,
  4369. error: new ErrorResponseImpl(((_result$init3 = result.init) == null ? void 0 : _result$init3.status) || 500, undefined, result.data),
  4370. statusCode: isRouteErrorResponse(result) ? result.status : undefined,
  4371. headers: (_result$init4 = result.init) != null && _result$init4.headers ? new Headers(result.init.headers) : undefined
  4372. };
  4373. }
  4374. return {
  4375. type: ResultType.error,
  4376. error: result,
  4377. statusCode: isRouteErrorResponse(result) ? result.status : undefined
  4378. };
  4379. }
  4380. if (isDeferredData(result)) {
  4381. var _result$init5, _result$init6;
  4382. return {
  4383. type: ResultType.deferred,
  4384. deferredData: result,
  4385. statusCode: (_result$init5 = result.init) == null ? void 0 : _result$init5.status,
  4386. headers: ((_result$init6 = result.init) == null ? void 0 : _result$init6.headers) && new Headers(result.init.headers)
  4387. };
  4388. }
  4389. if (isDataWithResponseInit(result)) {
  4390. var _result$init7, _result$init8;
  4391. return {
  4392. type: ResultType.data,
  4393. data: result.data,
  4394. statusCode: (_result$init7 = result.init) == null ? void 0 : _result$init7.status,
  4395. headers: (_result$init8 = result.init) != null && _result$init8.headers ? new Headers(result.init.headers) : undefined
  4396. };
  4397. }
  4398. return {
  4399. type: ResultType.data,
  4400. data: result
  4401. };
  4402. }
  4403. // Support relative routing in internal redirects
  4404. function normalizeRelativeRoutingRedirectResponse(response, request, routeId, matches, basename, v7_relativeSplatPath) {
  4405. let location = response.headers.get("Location");
  4406. invariant(location, "Redirects returned/thrown from loaders/actions must have a Location header");
  4407. if (!ABSOLUTE_URL_REGEX.test(location)) {
  4408. let trimmedMatches = matches.slice(0, matches.findIndex(m => m.route.id === routeId) + 1);
  4409. location = normalizeTo(new URL(request.url), trimmedMatches, basename, true, location, v7_relativeSplatPath);
  4410. response.headers.set("Location", location);
  4411. }
  4412. return response;
  4413. }
  4414. function normalizeRedirectLocation(location, currentUrl, basename) {
  4415. if (ABSOLUTE_URL_REGEX.test(location)) {
  4416. // Strip off the protocol+origin for same-origin + same-basename absolute redirects
  4417. let normalizedLocation = location;
  4418. let url = normalizedLocation.startsWith("//") ? new URL(currentUrl.protocol + normalizedLocation) : new URL(normalizedLocation);
  4419. let isSameBasename = stripBasename(url.pathname, basename) != null;
  4420. if (url.origin === currentUrl.origin && isSameBasename) {
  4421. return url.pathname + url.search + url.hash;
  4422. }
  4423. }
  4424. return location;
  4425. }
  4426. // Utility method for creating the Request instances for loaders/actions during
  4427. // client-side navigations and fetches. During SSR we will always have a
  4428. // Request instance from the static handler (query/queryRoute)
  4429. function createClientSideRequest(history, location, signal, submission) {
  4430. let url = history.createURL(stripHashFromPath(location)).toString();
  4431. let init = {
  4432. signal
  4433. };
  4434. if (submission && isMutationMethod(submission.formMethod)) {
  4435. let {
  4436. formMethod,
  4437. formEncType
  4438. } = submission;
  4439. // Didn't think we needed this but it turns out unlike other methods, patch
  4440. // won't be properly normalized to uppercase and results in a 405 error.
  4441. // See: https://fetch.spec.whatwg.org/#concept-method
  4442. init.method = formMethod.toUpperCase();
  4443. if (formEncType === "application/json") {
  4444. init.headers = new Headers({
  4445. "Content-Type": formEncType
  4446. });
  4447. init.body = JSON.stringify(submission.json);
  4448. } else if (formEncType === "text/plain") {
  4449. // Content-Type is inferred (https://fetch.spec.whatwg.org/#dom-request)
  4450. init.body = submission.text;
  4451. } else if (formEncType === "application/x-www-form-urlencoded" && submission.formData) {
  4452. // Content-Type is inferred (https://fetch.spec.whatwg.org/#dom-request)
  4453. init.body = convertFormDataToSearchParams(submission.formData);
  4454. } else {
  4455. // Content-Type is inferred (https://fetch.spec.whatwg.org/#dom-request)
  4456. init.body = submission.formData;
  4457. }
  4458. }
  4459. return new Request(url, init);
  4460. }
  4461. function convertFormDataToSearchParams(formData) {
  4462. let searchParams = new URLSearchParams();
  4463. for (let [key, value] of formData.entries()) {
  4464. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#converting-an-entry-list-to-a-list-of-name-value-pairs
  4465. searchParams.append(key, typeof value === "string" ? value : value.name);
  4466. }
  4467. return searchParams;
  4468. }
  4469. function convertSearchParamsToFormData(searchParams) {
  4470. let formData = new FormData();
  4471. for (let [key, value] of searchParams.entries()) {
  4472. formData.append(key, value);
  4473. }
  4474. return formData;
  4475. }
  4476. function processRouteLoaderData(matches, results, pendingActionResult, activeDeferreds, skipLoaderErrorBubbling) {
  4477. // Fill in loaderData/errors from our loaders
  4478. let loaderData = {};
  4479. let errors = null;
  4480. let statusCode;
  4481. let foundError = false;
  4482. let loaderHeaders = {};
  4483. let pendingError = pendingActionResult && isErrorResult(pendingActionResult[1]) ? pendingActionResult[1].error : undefined;
  4484. // Process loader results into state.loaderData/state.errors
  4485. matches.forEach(match => {
  4486. if (!(match.route.id in results)) {
  4487. return;
  4488. }
  4489. let id = match.route.id;
  4490. let result = results[id];
  4491. invariant(!isRedirectResult(result), "Cannot handle redirect results in processLoaderData");
  4492. if (isErrorResult(result)) {
  4493. let error = result.error;
  4494. // If we have a pending action error, we report it at the highest-route
  4495. // that throws a loader error, and then clear it out to indicate that
  4496. // it was consumed
  4497. if (pendingError !== undefined) {
  4498. error = pendingError;
  4499. pendingError = undefined;
  4500. }
  4501. errors = errors || {};
  4502. if (skipLoaderErrorBubbling) {
  4503. errors[id] = error;
  4504. } else {
  4505. // Look upwards from the matched route for the closest ancestor error
  4506. // boundary, defaulting to the root match. Prefer higher error values
  4507. // if lower errors bubble to the same boundary
  4508. let boundaryMatch = findNearestBoundary(matches, id);
  4509. if (errors[boundaryMatch.route.id] == null) {
  4510. errors[boundaryMatch.route.id] = error;
  4511. }
  4512. }
  4513. // Clear our any prior loaderData for the throwing route
  4514. loaderData[id] = undefined;
  4515. // Once we find our first (highest) error, we set the status code and
  4516. // prevent deeper status codes from overriding
  4517. if (!foundError) {
  4518. foundError = true;
  4519. statusCode = isRouteErrorResponse(result.error) ? result.error.status : 500;
  4520. }
  4521. if (result.headers) {
  4522. loaderHeaders[id] = result.headers;
  4523. }
  4524. } else {
  4525. if (isDeferredResult(result)) {
  4526. activeDeferreds.set(id, result.deferredData);
  4527. loaderData[id] = result.deferredData.data;
  4528. // Error status codes always override success status codes, but if all
  4529. // loaders are successful we take the deepest status code.
  4530. if (result.statusCode != null && result.statusCode !== 200 && !foundError) {
  4531. statusCode = result.statusCode;
  4532. }
  4533. if (result.headers) {
  4534. loaderHeaders[id] = result.headers;
  4535. }
  4536. } else {
  4537. loaderData[id] = result.data;
  4538. // Error status codes always override success status codes, but if all
  4539. // loaders are successful we take the deepest status code.
  4540. if (result.statusCode && result.statusCode !== 200 && !foundError) {
  4541. statusCode = result.statusCode;
  4542. }
  4543. if (result.headers) {
  4544. loaderHeaders[id] = result.headers;
  4545. }
  4546. }
  4547. }
  4548. });
  4549. // If we didn't consume the pending action error (i.e., all loaders
  4550. // resolved), then consume it here. Also clear out any loaderData for the
  4551. // throwing route
  4552. if (pendingError !== undefined && pendingActionResult) {
  4553. errors = {
  4554. [pendingActionResult[0]]: pendingError
  4555. };
  4556. loaderData[pendingActionResult[0]] = undefined;
  4557. }
  4558. return {
  4559. loaderData,
  4560. errors,
  4561. statusCode: statusCode || 200,
  4562. loaderHeaders
  4563. };
  4564. }
  4565. function processLoaderData(state, matches, results, pendingActionResult, revalidatingFetchers, fetcherResults, activeDeferreds) {
  4566. let {
  4567. loaderData,
  4568. errors
  4569. } = processRouteLoaderData(matches, results, pendingActionResult, activeDeferreds, false // This method is only called client side so we always want to bubble
  4570. );
  4571. // Process results from our revalidating fetchers
  4572. revalidatingFetchers.forEach(rf => {
  4573. let {
  4574. key,
  4575. match,
  4576. controller
  4577. } = rf;
  4578. let result = fetcherResults[key];
  4579. invariant(result, "Did not find corresponding fetcher result");
  4580. // Process fetcher non-redirect errors
  4581. if (controller && controller.signal.aborted) {
  4582. // Nothing to do for aborted fetchers
  4583. return;
  4584. } else if (isErrorResult(result)) {
  4585. let boundaryMatch = findNearestBoundary(state.matches, match == null ? void 0 : match.route.id);
  4586. if (!(errors && errors[boundaryMatch.route.id])) {
  4587. errors = _extends({}, errors, {
  4588. [boundaryMatch.route.id]: result.error
  4589. });
  4590. }
  4591. state.fetchers.delete(key);
  4592. } else if (isRedirectResult(result)) {
  4593. // Should never get here, redirects should get processed above, but we
  4594. // keep this to type narrow to a success result in the else
  4595. invariant(false, "Unhandled fetcher revalidation redirect");
  4596. } else if (isDeferredResult(result)) {
  4597. // Should never get here, deferred data should be awaited for fetchers
  4598. // in resolveDeferredResults
  4599. invariant(false, "Unhandled fetcher deferred data");
  4600. } else {
  4601. let doneFetcher = getDoneFetcher(result.data);
  4602. state.fetchers.set(key, doneFetcher);
  4603. }
  4604. });
  4605. return {
  4606. loaderData,
  4607. errors
  4608. };
  4609. }
  4610. function mergeLoaderData(loaderData, newLoaderData, matches, errors) {
  4611. let mergedLoaderData = _extends({}, newLoaderData);
  4612. for (let match of matches) {
  4613. let id = match.route.id;
  4614. if (newLoaderData.hasOwnProperty(id)) {
  4615. if (newLoaderData[id] !== undefined) {
  4616. mergedLoaderData[id] = newLoaderData[id];
  4617. }
  4618. } else if (loaderData[id] !== undefined && match.route.loader) {
  4619. // Preserve existing keys not included in newLoaderData and where a loader
  4620. // wasn't removed by HMR
  4621. mergedLoaderData[id] = loaderData[id];
  4622. }
  4623. if (errors && errors.hasOwnProperty(id)) {
  4624. // Don't keep any loader data below the boundary
  4625. break;
  4626. }
  4627. }
  4628. return mergedLoaderData;
  4629. }
  4630. function getActionDataForCommit(pendingActionResult) {
  4631. if (!pendingActionResult) {
  4632. return {};
  4633. }
  4634. return isErrorResult(pendingActionResult[1]) ? {
  4635. // Clear out prior actionData on errors
  4636. actionData: {}
  4637. } : {
  4638. actionData: {
  4639. [pendingActionResult[0]]: pendingActionResult[1].data
  4640. }
  4641. };
  4642. }
  4643. // Find the nearest error boundary, looking upwards from the leaf route (or the
  4644. // route specified by routeId) for the closest ancestor error boundary,
  4645. // defaulting to the root match
  4646. function findNearestBoundary(matches, routeId) {
  4647. let eligibleMatches = routeId ? matches.slice(0, matches.findIndex(m => m.route.id === routeId) + 1) : [...matches];
  4648. return eligibleMatches.reverse().find(m => m.route.hasErrorBoundary === true) || matches[0];
  4649. }
  4650. function getShortCircuitMatches(routes) {
  4651. // Prefer a root layout route if present, otherwise shim in a route object
  4652. let route = routes.length === 1 ? routes[0] : routes.find(r => r.index || !r.path || r.path === "/") || {
  4653. id: "__shim-error-route__"
  4654. };
  4655. return {
  4656. matches: [{
  4657. params: {},
  4658. pathname: "",
  4659. pathnameBase: "",
  4660. route
  4661. }],
  4662. route
  4663. };
  4664. }
  4665. function getInternalRouterError(status, _temp5) {
  4666. let {
  4667. pathname,
  4668. routeId,
  4669. method,
  4670. type,
  4671. message
  4672. } = _temp5 === void 0 ? {} : _temp5;
  4673. let statusText = "Unknown Server Error";
  4674. let errorMessage = "Unknown @remix-run/router error";
  4675. if (status === 400) {
  4676. statusText = "Bad Request";
  4677. if (method && pathname && routeId) {
  4678. errorMessage = "You made a " + method + " request to \"" + pathname + "\" but " + ("did not provide a `loader` for route \"" + routeId + "\", ") + "so there is no way to handle the request.";
  4679. } else if (type === "defer-action") {
  4680. errorMessage = "defer() is not supported in actions";
  4681. } else if (type === "invalid-body") {
  4682. errorMessage = "Unable to encode submission body";
  4683. }
  4684. } else if (status === 403) {
  4685. statusText = "Forbidden";
  4686. errorMessage = "Route \"" + routeId + "\" does not match URL \"" + pathname + "\"";
  4687. } else if (status === 404) {
  4688. statusText = "Not Found";
  4689. errorMessage = "No route matches URL \"" + pathname + "\"";
  4690. } else if (status === 405) {
  4691. statusText = "Method Not Allowed";
  4692. if (method && pathname && routeId) {
  4693. errorMessage = "You made a " + method.toUpperCase() + " request to \"" + pathname + "\" but " + ("did not provide an `action` for route \"" + routeId + "\", ") + "so there is no way to handle the request.";
  4694. } else if (method) {
  4695. errorMessage = "Invalid request method \"" + method.toUpperCase() + "\"";
  4696. }
  4697. }
  4698. return new ErrorResponseImpl(status || 500, statusText, new Error(errorMessage), true);
  4699. }
  4700. // Find any returned redirect errors, starting from the lowest match
  4701. function findRedirect(results) {
  4702. let entries = Object.entries(results);
  4703. for (let i = entries.length - 1; i >= 0; i--) {
  4704. let [key, result] = entries[i];
  4705. if (isRedirectResult(result)) {
  4706. return {
  4707. key,
  4708. result
  4709. };
  4710. }
  4711. }
  4712. }
  4713. function stripHashFromPath(path) {
  4714. let parsedPath = typeof path === "string" ? parsePath(path) : path;
  4715. return createPath(_extends({}, parsedPath, {
  4716. hash: ""
  4717. }));
  4718. }
  4719. function isHashChangeOnly(a, b) {
  4720. if (a.pathname !== b.pathname || a.search !== b.search) {
  4721. return false;
  4722. }
  4723. if (a.hash === "") {
  4724. // /page -> /page#hash
  4725. return b.hash !== "";
  4726. } else if (a.hash === b.hash) {
  4727. // /page#hash -> /page#hash
  4728. return true;
  4729. } else if (b.hash !== "") {
  4730. // /page#hash -> /page#other
  4731. return true;
  4732. }
  4733. // If the hash is removed the browser will re-perform a request to the server
  4734. // /page#hash -> /page
  4735. return false;
  4736. }
  4737. function isDataStrategyResult(result) {
  4738. return result != null && typeof result === "object" && "type" in result && "result" in result && (result.type === ResultType.data || result.type === ResultType.error);
  4739. }
  4740. function isRedirectDataStrategyResultResult(result) {
  4741. return isResponse(result.result) && redirectStatusCodes.has(result.result.status);
  4742. }
  4743. function isDeferredResult(result) {
  4744. return result.type === ResultType.deferred;
  4745. }
  4746. function isErrorResult(result) {
  4747. return result.type === ResultType.error;
  4748. }
  4749. function isRedirectResult(result) {
  4750. return (result && result.type) === ResultType.redirect;
  4751. }
  4752. function isDataWithResponseInit(value) {
  4753. return typeof value === "object" && value != null && "type" in value && "data" in value && "init" in value && value.type === "DataWithResponseInit";
  4754. }
  4755. function isDeferredData(value) {
  4756. let deferred = value;
  4757. return deferred && typeof deferred === "object" && typeof deferred.data === "object" && typeof deferred.subscribe === "function" && typeof deferred.cancel === "function" && typeof deferred.resolveData === "function";
  4758. }
  4759. function isResponse(value) {
  4760. return value != null && typeof value.status === "number" && typeof value.statusText === "string" && typeof value.headers === "object" && typeof value.body !== "undefined";
  4761. }
  4762. function isRedirectResponse(result) {
  4763. if (!isResponse(result)) {
  4764. return false;
  4765. }
  4766. let status = result.status;
  4767. let location = result.headers.get("Location");
  4768. return status >= 300 && status <= 399 && location != null;
  4769. }
  4770. function isValidMethod(method) {
  4771. return validRequestMethods.has(method.toLowerCase());
  4772. }
  4773. function isMutationMethod(method) {
  4774. return validMutationMethods.has(method.toLowerCase());
  4775. }
  4776. async function resolveNavigationDeferredResults(matches, results, signal, currentMatches, currentLoaderData) {
  4777. let entries = Object.entries(results);
  4778. for (let index = 0; index < entries.length; index++) {
  4779. let [routeId, result] = entries[index];
  4780. let match = matches.find(m => (m == null ? void 0 : m.route.id) === routeId);
  4781. // If we don't have a match, then we can have a deferred result to do
  4782. // anything with. This is for revalidating fetchers where the route was
  4783. // removed during HMR
  4784. if (!match) {
  4785. continue;
  4786. }
  4787. let currentMatch = currentMatches.find(m => m.route.id === match.route.id);
  4788. let isRevalidatingLoader = currentMatch != null && !isNewRouteInstance(currentMatch, match) && (currentLoaderData && currentLoaderData[match.route.id]) !== undefined;
  4789. if (isDeferredResult(result) && isRevalidatingLoader) {
  4790. // Note: we do not have to touch activeDeferreds here since we race them
  4791. // against the signal in resolveDeferredData and they'll get aborted
  4792. // there if needed
  4793. await resolveDeferredData(result, signal, false).then(result => {
  4794. if (result) {
  4795. results[routeId] = result;
  4796. }
  4797. });
  4798. }
  4799. }
  4800. }
  4801. async function resolveFetcherDeferredResults(matches, results, revalidatingFetchers) {
  4802. for (let index = 0; index < revalidatingFetchers.length; index++) {
  4803. let {
  4804. key,
  4805. routeId,
  4806. controller
  4807. } = revalidatingFetchers[index];
  4808. let result = results[key];
  4809. let match = matches.find(m => (m == null ? void 0 : m.route.id) === routeId);
  4810. // If we don't have a match, then we can have a deferred result to do
  4811. // anything with. This is for revalidating fetchers where the route was
  4812. // removed during HMR
  4813. if (!match) {
  4814. continue;
  4815. }
  4816. if (isDeferredResult(result)) {
  4817. // Note: we do not have to touch activeDeferreds here since we race them
  4818. // against the signal in resolveDeferredData and they'll get aborted
  4819. // there if needed
  4820. invariant(controller, "Expected an AbortController for revalidating fetcher deferred result");
  4821. await resolveDeferredData(result, controller.signal, true).then(result => {
  4822. if (result) {
  4823. results[key] = result;
  4824. }
  4825. });
  4826. }
  4827. }
  4828. }
  4829. async function resolveDeferredData(result, signal, unwrap) {
  4830. if (unwrap === void 0) {
  4831. unwrap = false;
  4832. }
  4833. let aborted = await result.deferredData.resolveData(signal);
  4834. if (aborted) {
  4835. return;
  4836. }
  4837. if (unwrap) {
  4838. try {
  4839. return {
  4840. type: ResultType.data,
  4841. data: result.deferredData.unwrappedData
  4842. };
  4843. } catch (e) {
  4844. // Handle any TrackedPromise._error values encountered while unwrapping
  4845. return {
  4846. type: ResultType.error,
  4847. error: e
  4848. };
  4849. }
  4850. }
  4851. return {
  4852. type: ResultType.data,
  4853. data: result.deferredData.data
  4854. };
  4855. }
  4856. function hasNakedIndexQuery(search) {
  4857. return new URLSearchParams(search).getAll("index").some(v => v === "");
  4858. }
  4859. function getTargetMatch(matches, location) {
  4860. let search = typeof location === "string" ? parsePath(location).search : location.search;
  4861. if (matches[matches.length - 1].route.index && hasNakedIndexQuery(search || "")) {
  4862. // Return the leaf index route when index is present
  4863. return matches[matches.length - 1];
  4864. }
  4865. // Otherwise grab the deepest "path contributing" match (ignoring index and
  4866. // pathless layout routes)
  4867. let pathMatches = getPathContributingMatches(matches);
  4868. return pathMatches[pathMatches.length - 1];
  4869. }
  4870. function getSubmissionFromNavigation(navigation) {
  4871. let {
  4872. formMethod,
  4873. formAction,
  4874. formEncType,
  4875. text,
  4876. formData,
  4877. json
  4878. } = navigation;
  4879. if (!formMethod || !formAction || !formEncType) {
  4880. return;
  4881. }
  4882. if (text != null) {
  4883. return {
  4884. formMethod,
  4885. formAction,
  4886. formEncType,
  4887. formData: undefined,
  4888. json: undefined,
  4889. text
  4890. };
  4891. } else if (formData != null) {
  4892. return {
  4893. formMethod,
  4894. formAction,
  4895. formEncType,
  4896. formData,
  4897. json: undefined,
  4898. text: undefined
  4899. };
  4900. } else if (json !== undefined) {
  4901. return {
  4902. formMethod,
  4903. formAction,
  4904. formEncType,
  4905. formData: undefined,
  4906. json,
  4907. text: undefined
  4908. };
  4909. }
  4910. }
  4911. function getLoadingNavigation(location, submission) {
  4912. if (submission) {
  4913. let navigation = {
  4914. state: "loading",
  4915. location,
  4916. formMethod: submission.formMethod,
  4917. formAction: submission.formAction,
  4918. formEncType: submission.formEncType,
  4919. formData: submission.formData,
  4920. json: submission.json,
  4921. text: submission.text
  4922. };
  4923. return navigation;
  4924. } else {
  4925. let navigation = {
  4926. state: "loading",
  4927. location,
  4928. formMethod: undefined,
  4929. formAction: undefined,
  4930. formEncType: undefined,
  4931. formData: undefined,
  4932. json: undefined,
  4933. text: undefined
  4934. };
  4935. return navigation;
  4936. }
  4937. }
  4938. function getSubmittingNavigation(location, submission) {
  4939. let navigation = {
  4940. state: "submitting",
  4941. location,
  4942. formMethod: submission.formMethod,
  4943. formAction: submission.formAction,
  4944. formEncType: submission.formEncType,
  4945. formData: submission.formData,
  4946. json: submission.json,
  4947. text: submission.text
  4948. };
  4949. return navigation;
  4950. }
  4951. function getLoadingFetcher(submission, data) {
  4952. if (submission) {
  4953. let fetcher = {
  4954. state: "loading",
  4955. formMethod: submission.formMethod,
  4956. formAction: submission.formAction,
  4957. formEncType: submission.formEncType,
  4958. formData: submission.formData,
  4959. json: submission.json,
  4960. text: submission.text,
  4961. data
  4962. };
  4963. return fetcher;
  4964. } else {
  4965. let fetcher = {
  4966. state: "loading",
  4967. formMethod: undefined,
  4968. formAction: undefined,
  4969. formEncType: undefined,
  4970. formData: undefined,
  4971. json: undefined,
  4972. text: undefined,
  4973. data
  4974. };
  4975. return fetcher;
  4976. }
  4977. }
  4978. function getSubmittingFetcher(submission, existingFetcher) {
  4979. let fetcher = {
  4980. state: "submitting",
  4981. formMethod: submission.formMethod,
  4982. formAction: submission.formAction,
  4983. formEncType: submission.formEncType,
  4984. formData: submission.formData,
  4985. json: submission.json,
  4986. text: submission.text,
  4987. data: existingFetcher ? existingFetcher.data : undefined
  4988. };
  4989. return fetcher;
  4990. }
  4991. function getDoneFetcher(data) {
  4992. let fetcher = {
  4993. state: "idle",
  4994. formMethod: undefined,
  4995. formAction: undefined,
  4996. formEncType: undefined,
  4997. formData: undefined,
  4998. json: undefined,
  4999. text: undefined,
  5000. data
  5001. };
  5002. return fetcher;
  5003. }
  5004. function restoreAppliedTransitions(_window, transitions) {
  5005. try {
  5006. let sessionPositions = _window.sessionStorage.getItem(TRANSITIONS_STORAGE_KEY);
  5007. if (sessionPositions) {
  5008. let json = JSON.parse(sessionPositions);
  5009. for (let [k, v] of Object.entries(json || {})) {
  5010. if (v && Array.isArray(v)) {
  5011. transitions.set(k, new Set(v || []));
  5012. }
  5013. }
  5014. }
  5015. } catch (e) {
  5016. // no-op, use default empty object
  5017. }
  5018. }
  5019. function persistAppliedTransitions(_window, transitions) {
  5020. if (transitions.size > 0) {
  5021. let json = {};
  5022. for (let [k, v] of transitions) {
  5023. json[k] = [...v];
  5024. }
  5025. try {
  5026. _window.sessionStorage.setItem(TRANSITIONS_STORAGE_KEY, JSON.stringify(json));
  5027. } catch (error) {
  5028. warning(false, "Failed to save applied view transitions in sessionStorage (" + error + ").");
  5029. }
  5030. }
  5031. }
  5032. //#endregion
  5033. export { AbortedDeferredError, Action, IDLE_BLOCKER, IDLE_FETCHER, IDLE_NAVIGATION, UNSAFE_DEFERRED_SYMBOL, DeferredData as UNSAFE_DeferredData, ErrorResponseImpl as UNSAFE_ErrorResponseImpl, convertRouteMatchToUiMatch as UNSAFE_convertRouteMatchToUiMatch, convertRoutesToDataRoutes as UNSAFE_convertRoutesToDataRoutes, decodePath as UNSAFE_decodePath, getResolveToMatches as UNSAFE_getResolveToMatches, invariant as UNSAFE_invariant, warning as UNSAFE_warning, createBrowserHistory, createHashHistory, createMemoryHistory, createPath, createRouter, createStaticHandler, data, defer, generatePath, getStaticContextFromError, getToPathname, isDataWithResponseInit, isDeferredData, isRouteErrorResponse, joinPaths, json, matchPath, matchRoutes, normalizePathname, parsePath, redirect, redirectDocument, replace, resolvePath, resolveTo, stripBasename };
  5034. //# sourceMappingURL=router.js.map