7777 </ div >
7878
7979 < section class ="section ">
80- < div class ="mt-5 mx-5 ">
80+ < div class ="mt-5 mx-5 " id =" portal " >
8181 < div class ="flex flex-col gap-5 border-b-2 border-lightborder dark:border-darkborder pb-2 mb-3 ">
8282 < div class ="flex flex-col-reverse gap-5 md:flex-row justify-between ">
8383 < div > {% block header %}{% endblock %}</ div >
125125 < script >
126126 stork . register ( "ref-search" , "{{config.base_url | safe}}/search.st" , { showProgress : false } ) ;
127127 </ script >
128+ < script >
129+ const preFetched = { } ;
130+ const parser = new DOMParser ( ) ;
131+
132+ const fetchLink = async ( link , pre ) => {
133+ if ( preFetched [ link ] ) return ;
134+
135+ const fetched = await fetch ( link ) ;
136+
137+ const doc = parser . parseFromString ( await fetched . text ( ) , "text/html" ) ;
138+
139+ const content = doc . getElementById ( "root" ) ;
140+
141+ if ( pre ) preFetched [ link ] = content ;
142+ return content ;
143+ }
144+
145+ const buttonInterceptor = async ( ev ) => {
146+ ev . preventDefault ( ) ;
147+
148+ const portal = document . getElementById ( "root" ) ;
149+ const potentialFetch = preFetched [ ev . target . href ] ;
150+
151+ if ( potentialFetch ) {
152+ portal . replaceChildren ( ...potentialFetch . children ) ;
153+ } else {
154+ const newContents = await fetchLink ( ev . target . href ) ;
155+
156+ if ( ! newContents ) {
157+ return ;
158+ }
159+
160+ portal . replaceChildren ( ...newContents . children ) ;
161+ }
162+
163+ delete preFetched [ ev . target . href ] ;
164+
165+ history . pushState ( null , "" , ev . target . href ) ;
166+
167+ prefetchLinks ( ) ;
168+ }
169+
170+ const hoverInterceptor = ( ev ) => {
171+ if ( ! ev . target . href ) return ;
172+
173+ fetchLink ( ev . target . href , true ) ;
174+ }
175+
176+ const prefetchLinks = ( ) => {
177+ for ( const link of document . getElementsByTagName ( "a" ) ) {
178+ if ( ! link . href || ! link . href . includes ( location . host ) ) {
179+ continue ;
180+ }
181+
182+ link . addEventListener ( "click" , buttonInterceptor ) ;
183+ link . addEventListener ( "mouseover" , hoverInterceptor ) ;
184+ }
185+ }
186+
187+ prefetchLinks ( ) ;
188+ </ script >
128189</ body >
129190
130191</ html >
0 commit comments