@@ -18,19 +18,6 @@ if (!('getAll' in Headers.prototype)) {
18
18
}
19
19
}
20
20
21
- // Check if a file exists, given a relative path
22
- const exists = async ( relativePath ) => {
23
- const path = fromFileUrl ( new URL ( relativePath , import . meta. url ) )
24
- try {
25
- await Deno . stat ( path )
26
- return true
27
- } catch ( error ) {
28
- if ( error instanceof Deno . errors . NotFound ) {
29
- return false
30
- }
31
- throw error
32
- }
33
- }
34
21
let idx = 0
35
22
36
23
const handler = async ( req , context ) => {
@@ -45,14 +32,19 @@ const handler = async (req, context) => {
45
32
// We don't want to just try importing and use that to test,
46
33
// because that would also throw if there's an error in the middleware,
47
34
// which we would want to surface not ignore.
48
- if ( await exists ( '../../middleware.js' ) ) {
35
+ try {
49
36
// We need to cache-bust the import because otherwise it will claim it
50
37
// doesn't exist if the user creates it after the server starts
51
38
const nextMiddleware = await import ( `../../middleware.js#${ idx ++ } ` )
52
39
middleware = nextMiddleware . middleware
53
- } else {
54
- // No middleware, so we silently return
55
- return
40
+ } catch ( importError ) {
41
+ // Error message is `Module not found "file://<path>/middleware.js#123456".` in Deno
42
+ if ( importError . code === 'ERR_MODULE_NOT_FOUND' && importError . message . includes ( `middleware.js#${ idx } ` ) ) {
43
+ // No middleware, so we silently return
44
+ return
45
+ }
46
+
47
+ throw importError
56
48
}
57
49
58
50
// This is the format expected by Next.js along with the timezone which we support.
0 commit comments