Skip to content

feat: improve messaging on middleware detection #1575

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 4 commits into from
Aug 26, 2022
Merged
Changes from 2 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
12 changes: 9 additions & 3 deletions packages/runtime/src/helpers/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,28 @@ export const onPreDev: OnPreBuild = async ({ constants, netlifyConfig }) => {
// Ignore if it doesn't exist
})
await writeDevEdgeFunction(constants)

let middlewareDetected = false
if (!existsSync(resolve(base, 'middleware.ts')) && !existsSync(resolve(base, 'middleware.js'))) {
console.log(
"No middleware found. Create a 'middleware.ts' or 'middleware.js' file in your project root to add custom middleware.",
"No middleware found. If you did intend to use middleware, create a 'middleware.ts' or 'middleware.js' file in your project root to add custom middleware.",
)
} else {
console.log('Watching for changes in Next.js middleware...')
middlewareDetected = true
}
// Eventually we might want to do this via esbuild's API, but for now the CLI works fine

const common = [`--bundle`, `--outdir=${resolve('.netlify')}`, `--format=esm`, `--target=esnext`, '--watch']

const esbuildTSArgs = middlewareDetected ? [...common, resolve(base, 'middleware.ts')] : common
const esbuildJSArgs = middlewareDetected ? [...common, resolve(base, 'middleware.js')] : common

// TypeScript
execa(`esbuild`, [...common, resolve(base, 'middleware.ts')], { all: true }).all.pipe(process.stdout)
execa(`esbuild`, esbuildTSArgs, { all: true }).all.pipe(process.stdout)

// JavaScript
execa(`esbuild`, [...common, resolve(base, 'middleware.js')], { all: true }).all.pipe(process.stdout)
execa(`esbuild`, esbuildJSArgs, { all: true }).all.pipe(process.stdout)

// Don't return the promise because we don't want to wait for the child process to finish
}