@@ -26,7 +26,9 @@ import {
2626 createRenderEffect ,
2727 Errored ,
2828 flush ,
29+ lazy ,
2930 Loading ,
31+ NotReadyError ,
3032 untrack ,
3133 type ParentProps
3234} from "solid-js" ;
@@ -64,6 +66,10 @@ type Options = {
6466 errored ?: boolean ;
6567 /** log the intent render and user effects over the location see when they run */
6668 landingProbe ?: boolean ;
69+ /** log what the page shows (a route, the loading fallback) at each `scrollTo` */
70+ scrollProbe ?: boolean ;
71+ /** wrap the router in `<Loading>`, as an app resolving a lazy subtree on arrival does */
72+ outerLoading ?: boolean ;
6773} ;
6874
6975function harness ( initial = "/" , options : Options = { } ) {
@@ -95,7 +101,7 @@ function harness(initial = "/", options: Options = {}) {
95101 Object . defineProperty ( window , "scrollY" , { configurable : true , get : ( ) => y } ) ;
96102 window . scrollTo = ( ( _x : number , newY : number ) => {
97103 y = newY ;
98- log . push ( `scrollTo:${ newY } ` ) ;
104+ log . push ( `scrollTo:${ newY } ` + ( options . scrollProbe ? ` shows= ${ shows ( ) } ` : "" ) ) ;
99105 window . dispatchEvent ( new Event ( "scroll" ) ) ;
100106 } ) as any ;
101107
@@ -229,24 +235,47 @@ function harness(initial = "/", options: Options = {}) {
229235 { path : "/fresh/:id" , component : Fresh } ,
230236 { path : "/hop/:n" , component : Hop } ,
231237 { path : "/guarded" , component : Guarded } ,
232- { path : "/watched" , component : Watched }
238+ { path : "/watched" , component : Watched } ,
239+ {
240+ path : "/lazy" ,
241+ component : lazy ( ( ) => gate ( "lazy" ) . promise . then ( ( ) => ( { default : page ( "lazy" ) } ) ) )
242+ } ,
243+ {
244+ path : "/tree" ,
245+ children : ( ( ) =>
246+ gate ( "tree" ) . promise . then ( ( ) => ( {
247+ default : [ { path : "/" , component : page ( "tree" ) } ]
248+ } ) ) ) as any
249+ }
233250 ] as const ,
234251 links : pendingLinks
235252 } ) ;
236253 const div = document . createElement ( "div" ) ;
254+ const shows = ( ) =>
255+ div . querySelector ( "[data-route]" ) ?. getAttribute ( "data-route" ) ??
256+ ( div . querySelector ( "[data-loading]" ) ? "loading" : "-" ) ;
237257 document . body . appendChild ( div ) ;
258+ const app = ( ) => < Router > { ( props : ParentProps ) => < Layout { ...props } /> } </ Router > ;
238259 const dispose = render (
239- ( ) => < Router > { ( props : ParentProps ) => < Layout { ... props } /> } </ Router > ,
260+ options . outerLoading ? ( ) => < Loading fallback = { < div data-loading /> } > { app ( ) } </ Loading > : app ,
240261 div
241262 ) ;
242263
243264 const mark = ( label : string ) =>
244265 untrack ( ( ) => {
245- const target = pendingTarget ( router ) ?. value ;
266+ // pending state over a still-resolving lazy route subtree is not
267+ // readable from outside the graph yet
268+ let state : string ;
269+ try {
270+ const target = pendingTarget ( router ) ?. value ;
271+ state = `routing=${ routingState ( router ) ( ) } ` + ( target ? ` target=${ target } ` : "" ) ;
272+ } catch ( e ) {
273+ if ( ! ( e instanceof NotReadyError ) ) throw e ;
274+ state = "routing=<not ready>" ;
275+ }
246276 const intent = router . intent ?.( ) ;
247277 log . push (
248- `| ${ label } routing=${ routingState ( router ) ( ) } ` +
249- ( target ? ` target=${ target } ` : "" ) +
278+ `| ${ label } ${ state } ` +
250279 ( intent ? ` intent=${ intent } ` : "" ) +
251280 ` at=${ router . location . pathname } `
252281 ) ;
@@ -701,6 +730,36 @@ describe("navigation pending state (characterization, #655)", () => {
701730 }
702731 } ) ;
703732
733+ test ( "a back navigation's restore joins the traversal's transition and waits for its data" , async ( ) => {
734+ const h = harness ( "/" , { scrollProbe : true } ) ;
735+ try {
736+ await h . wait ( "mounted" ) ;
737+ await h . step ( "navigate /fresh/p" , ( ) => h . navigate ( "/fresh/p" ) ) ;
738+ h . gate ( "p#1" ) . resolve ( "p" ) ;
739+ await h . wait ( "landed /fresh/p" ) ;
740+ window . scrollTo ( 0 , 900 ) ;
741+ await h . step ( "navigate /a" , ( ) => h . navigate ( "/a" ) ) ;
742+ h . reset ( ) ;
743+ await h . traverse ( "back to /fresh/p" , ( ) => window . history . back ( ) ) ;
744+ await h . wait ( "still pending" ) ;
745+ h . gate ( "p#2" ) . resolve ( "p" ) ;
746+ await h . wait ( "resolved" ) ;
747+ expect ( h . log ) . toMatchInlineSnapshot ( `
748+ [
749+ "fresh:p#2",
750+ "routing:true",
751+ "| back to /fresh/p routing=true intent=native at=/a",
752+ "| still pending routing=true intent=native at=/a",
753+ "routing:false",
754+ "scrollTo:900 shows=fresh",
755+ "| resolved routing=false at=/fresh/p",
756+ ]
757+ ` ) ;
758+ } finally {
759+ h . cleanup ( ) ;
760+ }
761+ } ) ;
762+
704763 test ( "beforeLeave blocking a programmatic navigation and a native pop" , async ( ) => {
705764 const h = harness ( ) ;
706765 try {
@@ -814,6 +873,73 @@ describe("navigation pending state (characterization, #655)", () => {
814873 }
815874 } ) ;
816875
876+ // A reload lands on an existing entry: the restore targets a page whose
877+ // initial content is still arriving — route data, a lazy route component,
878+ // a lazy route subtree.
879+ async function reloadInto ( initial : string , id : string ) {
880+ const positions : Record < number , number > = { } ;
881+ for ( let d = 0 ; d < 100 ; d ++ ) positions [ d ] = 400 ;
882+ sessionStorage . setItem ( "solid-router:scroll" , JSON . stringify ( positions ) ) ;
883+ const entries = performance . getEntriesByType ;
884+ performance . getEntriesByType = ( ( type : string ) =>
885+ type === "navigation" ? [ { type : "reload" } ] : [ ] ) as any ;
886+ let h ! : ReturnType < typeof harness > ;
887+ try {
888+ h = harness ( initial , { scrollProbe : true , outerLoading : initial === "/tree" } ) ;
889+ h . mark ( "rendered" ) ;
890+ flush ( ) ;
891+ h . mark ( "flushed" ) ;
892+ await h . wait ( "settled" ) ;
893+ h . gate ( id ) . resolve ( id ) ;
894+ await h . wait ( "resolved" ) ;
895+ return h . log ;
896+ } finally {
897+ performance . getEntriesByType = entries ;
898+ h ?. cleanup ( ) ;
899+ }
900+ }
901+
902+ test ( "scroll restoration on a reload while the route's data is pending" , async ( ) => {
903+ expect ( await reloadInto ( "/slow/initial" , "initial" ) ) . toMatchInlineSnapshot ( `
904+ [
905+ "routing:false",
906+ "preload:initial:initial",
907+ "fetch:initial:-",
908+ "scrollTo:400 shows=loading",
909+ "| rendered routing=false at=/slow/initial",
910+ "| flushed routing=false at=/slow/initial",
911+ "| settled routing=false at=/slow/initial",
912+ "| resolved routing=false at=/slow/initial",
913+ ]
914+ ` ) ;
915+ } ) ;
916+
917+ test ( "scroll restoration on a reload while a lazy route component loads" , async ( ) => {
918+ expect ( await reloadInto ( "/lazy" , "lazy" ) ) . toMatchInlineSnapshot ( `
919+ [
920+ "routing:false",
921+ "scrollTo:400 shows=loading",
922+ "| rendered routing=false at=/lazy",
923+ "| flushed routing=false at=/lazy",
924+ "| settled routing=false at=/lazy",
925+ "| resolved routing=false at=/lazy",
926+ ]
927+ ` ) ;
928+ } ) ;
929+
930+ test ( "scroll restoration on a reload into an unresolved lazy route subtree" , async ( ) => {
931+ expect ( await reloadInto ( "/tree" , "tree" ) ) . toMatchInlineSnapshot ( `
932+ [
933+ "| rendered routing=<not ready> at=/tree",
934+ "| flushed routing=<not ready> at=/tree",
935+ "| settled routing=<not ready> at=/tree",
936+ "routing:false",
937+ "scrollTo:400 shows=tree",
938+ "| resolved routing=false at=/tree",
939+ ]
940+ ` ) ;
941+ } ) ;
942+
817943 test ( "a navigation whose data rejects lands on the error boundary" , async ( ) => {
818944 const h = harness ( "/" , { errored : true } ) ;
819945 try {
0 commit comments