Skip to content

fix: remove fs access which isn't available in edge functions #1980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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