@@ -4,7 +4,7 @@ import { E400MissingDefaultExportAsComponent, E404Page, ErrorBoundary } from './
4
4
import events from './events.ts'
5
5
import { createPageProps , RouteModule , Routing } from './routing.ts'
6
6
import type { RouterURL } from './types.ts'
7
- import util , { hashShort , reModuleExt } from './util.ts'
7
+ import util , { hashShort , reModuleExt , reStyleModuleExt } from './util.ts'
8
8
9
9
export function ALEPH ( { initial } : {
10
10
initial : {
@@ -46,11 +46,21 @@ export function ALEPH({ initial }: {
46
46
if ( url . pagePath !== '' ) {
47
47
const ctree : { id : string , Component ?: ComponentType < any > } [ ] = pageModuleTree . map ( ( { id } ) => ( { id } ) )
48
48
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 ) )
50
50
if ( mod . asyncDeps ) {
51
51
// 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
+ } )
54
64
}
55
65
}
56
66
const pc = ctree . find ( pc => pc . id === mod . id )
@@ -105,7 +115,7 @@ export function ALEPH({ initial }: {
105
115
if ( mod . id . startsWith ( '/pages/' ) ) {
106
116
const { routing } = ref . current
107
117
routing . update ( mod )
108
- events . emit ( 'popstate' , { type : 'popstate' , forceFetch : true } )
118
+ events . emit ( 'popstate' , { type : 'popstate' , forceRefetch : true } )
109
119
}
110
120
break
111
121
}
@@ -128,16 +138,27 @@ export function ALEPH({ initial }: {
128
138
break
129
139
}
130
140
}
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 } )
133
144
if ( url . pagePath !== '' ) {
134
145
const imports = pageModuleTree . map ( async mod => {
135
146
await import ( getModuleImportUrl ( baseUrl , mod ) )
136
147
if ( mod . asyncDeps ) {
137
148
// import async dependencies
138
- for ( const dep of mod . asyncDeps ) {
149
+ for ( const dep of mod . asyncDeps . filter ( ( { url } ) => reStyleModuleExt . test ( url ) ) ) {
139
150
await import ( getModuleImportUrl ( baseUrl , { id : dep . url . replace ( reModuleExt , '.js' ) , hash : dep . hash } ) )
140
151
}
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
+ }
141
162
}
142
163
} )
143
164
await Promise . all ( imports )
@@ -193,6 +214,6 @@ export async function redirect(url: string, replace?: boolean) {
193
214
events . emit ( 'popstate' , { type : 'popstate' , scrollTo : 0 } )
194
215
}
195
216
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 ( ) } ` : '' ) )
198
219
}
0 commit comments