Skip to content

Commit 6546641

Browse files
authored
fix: remove fs access which isn't available in edge functions (#1980)
* fix: remove fs access which isn't available in edge functions * chore: fix comment :) * chore: remove slash because windows
1 parent 916a467 commit 6546641

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

packages/runtime/src/templates/edge/next-dev.js

+9-17
Original file line numberDiff line numberDiff line change
@@ -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-
}
3421
let idx = 0
3522

3623
const 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

Comments
 (0)