@@ -4,7 +4,7 @@ import { E400MissingDefaultExportAsComponent, E404Page, ErrorBoundary } from './
44import events from './events.ts'
55import { createPageProps , RouteModule , Routing } from './routing.ts'
66import type { RouterURL } from './types.ts'
7- import util , { hashShort , reModuleExt } from './util.ts'
7+ import util , { hashShort , reModuleExt , reStyleModuleExt } from './util.ts'
88
99export function ALEPH ( { initial } : {
1010 initial : {
@@ -46,11 +46,21 @@ export function ALEPH({ initial }: {
4646 if ( url . pagePath !== '' ) {
4747 const ctree : { id : string , Component ?: ComponentType < any > } [ ] = pageModuleTree . map ( ( { id } ) => ( { id } ) )
4848 const imports = pageModuleTree . map ( async mod => {
49- const { default : C } = await import ( getModuleImportUrl ( baseUrl , mod , e . forceFetch ) )
49+ const { default : C } = await import ( getModuleImportUrl ( baseUrl , mod , e . forceRefetch ) )
5050 if ( mod . asyncDeps ) {
5151 // import async dependencies
52- for ( const dep of mod . asyncDeps ) {
53- await import ( getModuleImportUrl ( baseUrl , { id : dep . url . replace ( reModuleExt , '.js' ) , hash : dep . hash } , e . forceFetch ) )
52+ for ( const dep of mod . asyncDeps . filter ( ( { url } ) => reStyleModuleExt . test ( url ) ) ) {
53+ await import ( getModuleImportUrl ( baseUrl , { id : dep . url . replace ( reModuleExt , '.js' ) , hash : dep . hash } , e . forceRefetch ) )
54+ }
55+ if ( mod . asyncDeps . filter ( ( { url } ) => url . startsWith ( '#useDeno.' ) ) . length > 0 ) {
56+ import ( `/_aleph/data${ [ url . pathname , url . query . toString ( ) ] . filter ( Boolean ) . join ( '@' ) } /data.js` + ( e . forceRefetch ? `?t=${ Date . now ( ) } ` : '' ) ) . then ( ( { default : data } ) => {
57+ if ( util . isPlainObject ( data ) ) {
58+ for ( const key in data ) {
59+ const useDenoUrl = `useDeno://${ url . pathname } ?${ url . query . toString ( ) } #${ key } `
60+ Object . assign ( window , { [ useDenoUrl ] : data [ key ] } )
61+ }
62+ }
63+ } )
5464 }
5565 }
5666 const pc = ctree . find ( pc => pc . id === mod . id )
@@ -105,7 +115,7 @@ export function ALEPH({ initial }: {
105115 if ( mod . id . startsWith ( '/pages/' ) ) {
106116 const { routing } = ref . current
107117 routing . update ( mod )
108- events . emit ( 'popstate' , { type : 'popstate' , forceFetch : true } )
118+ events . emit ( 'popstate' , { type : 'popstate' , forceRefetch : true } )
109119 }
110120 break
111121 }
@@ -128,16 +138,27 @@ export function ALEPH({ initial }: {
128138 break
129139 }
130140 }
131- const onFetchPageModule = async ( { url : pathname } : { url : string } ) => {
132- const [ url , pageModuleTree ] = routing . createRouter ( { pathname } )
141+ const onFetchPageModule = async ( { href } : { href : string } ) => {
142+ const [ pathname , search ] = href . split ( '?' )
143+ const [ url , pageModuleTree ] = routing . createRouter ( { pathname, search } )
133144 if ( url . pagePath !== '' ) {
134145 const imports = pageModuleTree . map ( async mod => {
135146 await import ( getModuleImportUrl ( baseUrl , mod ) )
136147 if ( mod . asyncDeps ) {
137148 // import async dependencies
138- for ( const dep of mod . asyncDeps ) {
149+ for ( const dep of mod . asyncDeps . filter ( ( { url } ) => reStyleModuleExt . test ( url ) ) ) {
139150 await import ( getModuleImportUrl ( baseUrl , { id : dep . url . replace ( reModuleExt , '.js' ) , hash : dep . hash } ) )
140151 }
152+ if ( mod . asyncDeps . filter ( ( { url } ) => url . startsWith ( '#useDeno.' ) ) . length > 0 ) {
153+ import ( `/_aleph/data${ [ url . pathname , url . query . toString ( ) ] . filter ( Boolean ) . join ( '@' ) } /data.js` ) . then ( ( { default : data } ) => {
154+ if ( util . isPlainObject ( data ) ) {
155+ for ( const key in data ) {
156+ const useDenoUrl = `useDeno://${ url . pathname } ?${ url . query . toString ( ) } #${ key } `
157+ Object . assign ( window , { [ useDenoUrl ] : data [ key ] } )
158+ }
159+ }
160+ } )
161+ }
141162 }
142163 } )
143164 await Promise . all ( imports )
@@ -193,6 +214,6 @@ export async function redirect(url: string, replace?: boolean) {
193214 events . emit ( 'popstate' , { type : 'popstate' , scrollTo : 0 } )
194215}
195216
196- export function getModuleImportUrl ( baseUrl : string , mod : RouteModule , forceFetch = false ) {
197- return util . cleanPath ( baseUrl + '/_aleph/' + util . trimSuffix ( mod . id , '.js' ) + `.${ mod . hash . slice ( 0 , hashShort ) } .js` + ( forceFetch ? `?t=${ Date . now ( ) } ` : '' ) )
217+ export function getModuleImportUrl ( baseUrl : string , mod : RouteModule , forceRefetch = false ) {
218+ return util . cleanPath ( baseUrl + '/_aleph/' + util . trimSuffix ( mod . id , '.js' ) + `.${ mod . hash . slice ( 0 , hashShort ) } .js` + ( forceRefetch ? `?t=${ Date . now ( ) } ` : '' ) )
198219}
0 commit comments