router.cjs.js 202 KB

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