@@ -3,9 +3,10 @@ import log from "../lib/log.ts";
3
3
import util from "../lib/util.ts" ;
4
4
import type { RouteModule , Routes } from "../lib/route.ts" ;
5
5
import { matchRoutes } from "../lib/route.ts" ;
6
+ import { errorHtml } from "./error.ts" ;
6
7
import type { DependencyGraph , Module } from "./graph.ts" ;
7
8
import { builtinModuleExts , getDeploymentId , getUnoGenerator } from "./helpers.ts" ;
8
- import type { Comment , Element } from "./html.ts" ;
9
+ import type { Element , HTMLRewriterHandlers } from "./html.ts" ;
9
10
import { HTMLRewriter } from "./html.ts" ;
10
11
import { importRouteModule } from "./routing.ts" ;
11
12
@@ -20,11 +21,6 @@ export type SSRContext = {
20
21
readonly onError ?: ( error : unknown ) => void ;
21
22
} ;
22
23
23
- export type HTMLRewriterHandlers = {
24
- element ?: ( element : Element ) => void ;
25
- comments ?: ( element : Comment ) => void ;
26
- } ;
27
-
28
24
export type SSR = {
29
25
suspense : true ;
30
26
render ( ssr : SSRContext ) : Promise < ReadableStream > | ReadableStream ;
@@ -33,7 +29,7 @@ export type SSR = {
33
29
render ( ssr : SSRContext ) : Promise < string | ReadableStream > | string | ReadableStream ;
34
30
} | ( ( ssr : SSRContext ) => Promise < string | ReadableStream > | string | ReadableStream ) ;
35
31
36
- type SSRResult = {
32
+ export type SSRResult = {
37
33
context : SSRContext ;
38
34
errorBoundaryHandlerFilename ?: string ;
39
35
body : ReadableStream | string ;
@@ -142,25 +138,14 @@ export default {
142
138
}
143
139
let message : string ;
144
140
if ( e instanceof Error ) {
145
- const regStackLoc = / ( h t t p : \/ \/ l o c a l h o s t : 6 0 \d { 2 } \/ .+ ) ( : \d + : \d + ) / ;
146
- message = ( e . stack as string ) . split ( "\n" ) . map ( ( line , i ) => {
147
- const ret = line . match ( regStackLoc ) ;
148
- if ( ret ) {
149
- const url = new URL ( ret [ 1 ] ) ;
150
- line = line . replace ( ret [ 0 ] , `.${ url . pathname } ${ ret [ 2 ] } ` ) ;
151
- }
152
- if ( i === 0 ) {
153
- return `<strong>SSR ${ line } </strong>` ;
154
- }
155
- return line ;
156
- } ) . join ( "\n" ) ;
141
+ message = e . stack as string ;
157
142
log . error ( "SSR" , e ) ;
158
143
} else {
159
144
message = e ?. toString ?.( ) || String ( e ) ;
160
145
}
161
146
headers . append ( "Cache-Control" , "public, max-age=0, must-revalidate" ) ;
162
147
headers . append ( "Content-Type" , "text/html; charset=utf-8" ) ;
163
- return new Response ( errorHtml ( message ) , { headers } ) ;
148
+ return new Response ( errorHtml ( message , "SSR" ) , { headers } ) ;
164
149
}
165
150
} else {
166
151
const { mtime, size } = await Deno . lstat ( "./index.html" ) ;
@@ -338,6 +323,8 @@ async function initSSR(
338
323
const url = new URL ( req . url ) ;
339
324
const matches = matchRoutes ( url , routes ) ;
340
325
const suspenseData : Record < string , unknown > = { } ;
326
+
327
+ // import module and fetch data for each matched route
341
328
const modules = await Promise . all ( matches . map ( async ( [ ret , { filename } ] ) => {
342
329
const mod = await importRouteModule ( filename ) ;
343
330
const dataConfig : Record < string , unknown > = util . isPlainObject ( mod . data ) ? mod . data : { } ;
@@ -348,10 +335,24 @@ async function initSSR(
348
335
defaultExport : mod . default ,
349
336
dataCacheTtl : dataConfig ?. cacheTtl as ( number | undefined ) ,
350
337
} ;
338
+
339
+ // assign route params to context
340
+ Object . assign ( ctx . params , ret . pathname . groups ) ;
341
+
342
+ // check `any` fetch of data, throw if it returns a response object
343
+ const anyFetcher = dataConfig . any ;
344
+ if ( typeof anyFetcher === "function" ) {
345
+ const res = await anyFetcher ( req , ctx ) ;
346
+ if ( res instanceof Response ) {
347
+ throw res ;
348
+ }
349
+ }
350
+
351
+ // check `get` of data, if `suspense` is enabled then return a promise instead
351
352
const fetcher = dataConfig . get ;
352
353
if ( typeof fetcher === "function" ) {
353
354
const fetchData = async ( ) => {
354
- let res = fetcher ( req , { ... ctx , params : rmod . params } ) ;
355
+ let res = fetcher ( req , ctx ) ;
355
356
if ( res instanceof Promise ) {
356
357
res = await res ;
357
358
}
@@ -384,79 +385,31 @@ async function initSSR(
384
385
rmod . data = await fetchData ( ) ;
385
386
}
386
387
}
388
+
387
389
return rmod ;
388
390
} ) ) ;
389
- const routeModules = modules . filter ( ( { defaultExport } ) => defaultExport !== undefined ) ;
391
+
392
+ // find error boundary handler
390
393
if ( routes . _error ) {
391
394
const [ _ , meta ] = routes . _error ;
392
395
const mod = await importRouteModule ( meta . filename ) ;
393
396
if ( typeof mod . default === "function" ) {
394
- return [ url , routeModules , suspenseData , { filename : meta . filename , default : mod . default } ] ;
397
+ return [
398
+ url ,
399
+ modules . filter ( ( { defaultExport } ) => defaultExport !== undefined ) ,
400
+ suspenseData ,
401
+ {
402
+ filename : meta . filename ,
403
+ default : mod . default ,
404
+ } ,
405
+ ] ;
395
406
}
396
407
}
397
- return [ url , routeModules , suspenseData , undefined ] ;
398
- }
399
408
400
- const errorHtml = ( message : string ) => `
401
- <!DOCTYPE html>
402
- <html lang="en">
403
- <head>
404
- <meta charset="utf-8">
405
- <title>SSR Error - Aleph.js</title>
406
- <style>
407
- body {
408
- overflow: hidden;
409
- }
410
- .error {
411
- display: flex;
412
- align-items: center;
413
- justify-content: center;
414
- width: 100vw;
415
- height: 100vh;
416
- }
417
- .error .box {
418
- box-sizing: border-box;
419
- position: relative;
420
- max-width: 80%;
421
- max-height: 90%;
422
- overflow: auto;
423
- padding: 24px 36px;
424
- border-radius: 12px;
425
- border: 2px solid rgba(255, 0, 0, 0.8);
426
- background-color: rgba(255, 0, 0, 0.1);
427
- color: rgba(255, 0, 0, 1);
428
- }
429
- .error .logo {
430
- position: absolute;
431
- top: 50%;
432
- left: 50%;
433
- margin-top: -45px;
434
- margin-left: -45px;
435
- opacity: 0.1;
436
- }
437
- .error pre {
438
- position: relative;
439
- line-height: 1.4;
440
- }
441
- .error code {
442
- font-size: 14px;
443
- }
444
- </style>
445
- </head>
446
- <body>
447
- <div class="error">
448
- <div class="box">
449
- <div class="logo">
450
- <svg width="90" height="90" viewBox="0 0 100 100" fill="none" xmlns="http://www.w3.org/2000/svg">
451
- <path d="M52.9528 11.1C54.0959 11.1 55.1522 11.7097 55.7239 12.6995C68.5038 34.8259 81.2837 56.9524 94.0636 79.0788C94.642 80.0802 94.6355 81.316 94.0425 82.3088C93.0466 83.9762 92.028 85.6661 91.0325 87.3331C90.4529 88.3035 89.407 88.9 88.2767 88.9C62.7077 88.9 37.0519 88.9 11.4828 88.9C10.3207 88.9 9.25107 88.2693 8.67747 87.2586C7.75465 85.6326 6.81025 84.0065 5.88797 82.3805C5.33314 81.4023 5.34422 80.2041 5.90662 79.2302C18.6982 57.0794 31.4919 34.8415 44.3746 12.6907C44.9474 11.7058 46.0009 11.1 47.1402 11.1C49.0554 11.1 51.0005 11.1 52.9528 11.1Z" stroke="#f00" stroke-width="3.2" stroke-miterlimit="10" stroke-linejoin="round"/>
452
- <path d="M28.2002 72.8H80.8002L45.8187 12.5494" stroke="#f00" stroke-width="3.2" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round"/>
453
- <path d="M71.4999 72.7L45.1999 27.2L10.6519 87.1991" stroke="#f00" stroke-width="3.2" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round"/>
454
- <path d="M49.8 35.3L23.5 80.8H93.9333" stroke="#f00" stroke-width="3.2" stroke-miterlimit="10" stroke-linecap="round" stroke-linejoin="round"/>
455
- </svg>
456
- </div>
457
- <pre><code>${ message } </code></pre>
458
- </div>
459
- </div>
460
- </body>
461
- </html>
462
- ` ;
409
+ return [
410
+ url ,
411
+ modules . filter ( ( { defaultExport } ) => defaultExport !== undefined ) ,
412
+ suspenseData ,
413
+ undefined ,
414
+ ] ;
415
+ }
0 commit comments