@@ -18,19 +18,6 @@ if (!('getAll' in Headers.prototype)) {
1818 }
1919}
2020
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- }
3421let idx = 0
3522
3623const handler = async ( req , context ) => {
@@ -45,14 +32,19 @@ const handler = async (req, context) => {
4532 // We don't want to just try importing and use that to test,
4633 // because that would also throw if there's an error in the middleware,
4734 // which we would want to surface not ignore.
48- if ( await exists ( '../../middleware.js' ) ) {
35+ try {
4936 // We need to cache-bust the import because otherwise it will claim it
5037 // doesn't exist if the user creates it after the server starts
5138 const nextMiddleware = await import ( `../../middleware.js#${ idx ++ } ` )
5239 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
5648 }
5749
5850 // This is the format expected by Next.js along with the timezone which we support.
0 commit comments