Skip to content

Commit

Permalink
fix: remove fs access which isn't available in edge functions (#1980)
Browse files Browse the repository at this point in the history
* fix: remove fs access which isn't available in edge functions

* chore: fix comment :)

* chore: remove slash because windows
  • Loading branch information
danez committed Mar 13, 2023
1 parent 916a467 commit 6546641
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions packages/runtime/src/templates/edge/next-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,6 @@ if (!('getAll' in Headers.prototype)) {
}
}

// Check if a file exists, given a relative path
const exists = async (relativePath) => {
const path = fromFileUrl(new URL(relativePath, import.meta.url))
try {
await Deno.stat(path)
return true
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
return false
}
throw error
}
}
let idx = 0

const handler = async (req, context) => {
Expand All @@ -45,14 +32,19 @@ const handler = async (req, context) => {
// We don't want to just try importing and use that to test,
// because that would also throw if there's an error in the middleware,
// which we would want to surface not ignore.
if (await exists('../../middleware.js')) {
try {
// We need to cache-bust the import because otherwise it will claim it
// doesn't exist if the user creates it after the server starts
const nextMiddleware = await import(`../../middleware.js#${idx++}`)
middleware = nextMiddleware.middleware
} else {
// No middleware, so we silently return
return
} catch (importError) {
// Error message is `Module not found "file://<path>/middleware.js#123456".` in Deno
if (importError.code === 'ERR_MODULE_NOT_FOUND' && importError.message.includes(`middleware.js#${idx}`)) {
// No middleware, so we silently return
return
}

throw importError
}

// This is the format expected by Next.js along with the timezone which we support.
Expand Down

0 comments on commit 6546641

Please sign in to comment.