Skip to content

Commit 8b8ed08

Browse files
committed
chore: added some comments and renamed some functions
1 parent ff45167 commit 8b8ed08

File tree

4 files changed

+35
-18
lines changed

4 files changed

+35
-18
lines changed

packages/runtime/src/helpers/compiler.ts

+20-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ import { join } from 'path'
44
import { build } from '@netlify/esbuild'
55
import { FSWatcher, watch } from 'chokidar'
66

7+
// For more information on Next.js middleware, see https://nextjs.org/docs/advanced-features/middleware
8+
9+
// These are the locations that a middleware file can exist in a Next.js application
10+
// If other possible locations are added by Next.js, they should be added here.
11+
const MIDDLEWARE_FILE_LOCATIONS: Readonly<string[]> = [
12+
'middleware.js',
13+
'middleware.ts',
14+
'src/middleware.js',
15+
'src/middleware.ts',
16+
]
17+
718
const toFileList = (watched: Record<string, Array<string>>) =>
819
Object.entries(watched).flatMap(([dir, files]) => files.map((file) => join(dir, file)))
920

@@ -66,8 +77,14 @@ const updateWatchedFiles = async (watcher: FSWatcher, base: string, isFirstRun =
6677
watcher.emit('build')
6778
}
6879

69-
export const startWatching = (base: string) => {
70-
const watcher = watch(['middleware.js', 'middleware.ts', 'src/middleware.js', 'src/middleware.ts'], {
80+
/**
81+
* Watch for changes to the middleware file location. When a change is detected, recompile the middleware file.
82+
*
83+
* @param base The base directory to watch
84+
* @returns a file watcher and a promise that resolves when the initial scan is complete.
85+
*/
86+
export const watchForMiddlewareChanges = (base: string) => {
87+
const watcher = watch(MIDDLEWARE_FILE_LOCATIONS, {
7188
// Try and ensure renames just emit one event
7289
atomic: true,
7390
// Don't emit for every watched file, just once after the scan is done
@@ -93,7 +110,7 @@ export const startWatching = (base: string) => {
93110
watcher,
94111
isReady: new Promise<void>((resolve) => {
95112
watcher.on('ready', async () => {
96-
console.log('Initial scan complete. Ready for changes')
113+
console.log('Initial scan for middleware file complete. Ready for changes')
97114
// This only happens on the first scan
98115
await updateWatchedFiles(watcher, base, true)
99116
console.log('Ready')

packages/runtime/src/helpers/dev.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const onPreDev: OnPreBuild = async ({ constants, netlifyConfig }) => {
1616
await writeDevEdgeFunction(constants)
1717
// Don't await this or it will never finish
1818
execa.node(
19-
resolve(__dirname, '..', '..', 'lib', 'helpers', 'watcher.js'),
19+
resolve(__dirname, '..', '..', 'lib', 'helpers', 'middlewareWatcher.js'),
2020
[base, process.env.NODE_ENV === 'test' ? '--once' : ''],
2121
{
2222
stdio: 'inherit',

packages/runtime/src/helpers/watcher.ts renamed to packages/runtime/src/helpers/middlewareWatcher.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { resolve } from 'path'
22

3-
import { startWatching } from './compiler'
3+
import { watchForMiddlewareChanges } from './compiler'
44

55
const run = async () => {
6-
const { isReady, watcher } = startWatching(resolve(process.argv[2]))
6+
const { isReady, watcher } = watchForMiddlewareChanges(resolve(process.argv[2]))
77
await isReady
88
if (process.argv[3] === '--once') {
99
watcher.close()

test/index.spec.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const { downloadFile } = require('../packages/runtime/src/templates/handlerUtils
3131
const { getExtendedApiRouteConfigs } = require('../packages/runtime/src/helpers/functions')
3232
const nextRuntimeFactory = require('../packages/runtime/src')
3333
const nextRuntime = nextRuntimeFactory({})
34-
const { startWatching } = require('../packages/runtime/src/helpers/compiler')
34+
const { watchForMiddlewareChanges } = require('../packages/runtime/src/helpers/compiler')
3535
const { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME, IMAGE_FUNCTION_NAME } = require('../packages/runtime/src/constants')
3636
const { join } = require('pathe')
3737
const {
@@ -1837,7 +1837,7 @@ fdescribe('the dev middleware watcher', () => {
18371837
await moveNextDist('.next', true)
18381838
await writeFile(path.join(process.cwd(), 'middleware.ts'), middlewareSourceTs)
18391839
expect(middlewareExists()).toBeFalsy()
1840-
const { watcher, isReady } = startWatching(process.cwd())
1840+
const { watcher, isReady } = watchForMiddlewareChanges(process.cwd())
18411841
watchers.push(watcher)
18421842
await isReady
18431843
expect(middlewareExists()).toBeTruthy()
@@ -1847,7 +1847,7 @@ fdescribe('the dev middleware watcher', () => {
18471847
it('should compile a file if it is written after the watcher starts', async () => {
18481848
console.log('starting should compile a file if it is written after the watcher starts')
18491849
await moveNextDist('.next', true)
1850-
const { watcher, isReady, nextBuild } = startWatching(process.cwd())
1850+
const { watcher, isReady, nextBuild } = watchForMiddlewareChanges(process.cwd())
18511851
watchers.push(watcher)
18521852
await isReady
18531853
expect(middlewareExists()).toBeFalsy()
@@ -1861,7 +1861,7 @@ fdescribe('the dev middleware watcher', () => {
18611861
it('should remove the output if the middleware is removed after the watcher starts', async () => {
18621862
console.log('starting should remove the output if the middleware is removed after the watcher starts')
18631863
await moveNextDist('.next', true)
1864-
const { watcher, isReady, nextBuild } = startWatching(process.cwd())
1864+
const { watcher, isReady, nextBuild } = watchForMiddlewareChanges(process.cwd())
18651865
watchers.push(watcher)
18661866
await isReady
18671867
expect(middlewareExists()).toBeFalsy()
@@ -1879,7 +1879,7 @@ fdescribe('the dev middleware watcher', () => {
18791879
it('should remove the output if invalid middleware is written after the watcher starts', async () => {
18801880
console.log('starting should remove the output if invalid middleware is written after the watcher starts')
18811881
await moveNextDist('.next', true)
1882-
const { watcher, isReady, nextBuild } = startWatching(process.cwd())
1882+
const { watcher, isReady, nextBuild } = watchForMiddlewareChanges(process.cwd())
18831883
watchers.push(watcher)
18841884
await isReady
18851885
expect(middlewareExists()).toBeFalsy()
@@ -1899,7 +1899,7 @@ fdescribe('the dev middleware watcher', () => {
18991899
'starting should recompile the middleware if it is moved into the src directory after the watcher starts',
19001900
)
19011901
await moveNextDist('.next', true)
1902-
const { watcher, isReady, nextBuild } = startWatching(process.cwd())
1902+
const { watcher, isReady, nextBuild } = watchForMiddlewareChanges(process.cwd())
19031903
watchers.push(watcher)
19041904
await isReady
19051905
expect(middlewareExists()).toBeFalsy()
@@ -1919,7 +1919,7 @@ fdescribe('the dev middleware watcher', () => {
19191919
'starting should recompile the middleware if it is moved into the root directory after the watcher starts',
19201920
)
19211921
await moveNextDist('.next', true)
1922-
const { watcher, isReady, nextBuild } = startWatching(process.cwd())
1922+
const { watcher, isReady, nextBuild } = watchForMiddlewareChanges(process.cwd())
19231923
watchers.push(watcher)
19241924
await isReady
19251925
expect(middlewareExists()).toBeFalsy()
@@ -1940,7 +1940,7 @@ fdescribe('the dev middleware watcher', () => {
19401940
'starting should compile the middleware if invalid source is replaced with valid source after the watcher starts',
19411941
)
19421942
await moveNextDist('.next', true)
1943-
const { watcher, isReady, nextBuild } = startWatching(process.cwd())
1943+
const { watcher, isReady, nextBuild } = watchForMiddlewareChanges(process.cwd())
19441944
watchers.push(watcher)
19451945
await isReady
19461946
expect(middlewareExists()).toBeFalsy()
@@ -1960,7 +1960,7 @@ fdescribe('the dev middleware watcher', () => {
19601960
it('should not compile middleware if more than one middleware file exists', async () => {
19611961
console.log('starting should not compile middleware if more than one middleware file exists')
19621962
await moveNextDist('.next', true)
1963-
const { watcher, isReady, nextBuild } = startWatching(process.cwd())
1963+
const { watcher, isReady, nextBuild } = watchForMiddlewareChanges(process.cwd())
19641964
watchers.push(watcher)
19651965
await isReady
19661966
expect(middlewareExists()).toBeFalsy()
@@ -1975,7 +1975,7 @@ fdescribe('the dev middleware watcher', () => {
19751975
it('should not compile middleware if a second middleware file is added after the watcher starts', async () => {
19761976
console.log('starting should not compile middleware if a second middleware file is added after the watcher starts')
19771977
await moveNextDist('.next', true)
1978-
const { watcher, isReady, nextBuild } = startWatching(process.cwd())
1978+
const { watcher, isReady, nextBuild } = watchForMiddlewareChanges(process.cwd())
19791979
watchers.push(watcher)
19801980
await isReady
19811981
expect(middlewareExists()).toBeFalsy()
@@ -1993,7 +1993,7 @@ fdescribe('the dev middleware watcher', () => {
19931993
it('should compile middleware if a second middleware file is removed after the watcher starts', async () => {
19941994
console.log('starting should compile middleware if a second middleware file is removed after the watcher starts')
19951995
await moveNextDist('.next', true)
1996-
const { watcher, isReady, nextBuild } = startWatching(process.cwd())
1996+
const { watcher, isReady, nextBuild } = watchForMiddlewareChanges(process.cwd())
19971997
watchers.push(watcher)
19981998
await isReady
19991999
expect(middlewareExists()).toBeFalsy()
@@ -2020,7 +2020,7 @@ fdescribe('the dev middleware watcher', () => {
20202020
const consoleErrorSpy = jest
20212021
.spyOn(console, 'error')
20222022
.mockImplementation((args) => console.warn(args?.errors?.[0]?.text))
2023-
const { watcher, isReady, nextBuild } = startWatching(process.cwd())
2023+
const { watcher, isReady, nextBuild } = watchForMiddlewareChanges(process.cwd())
20242024
watchers.push(watcher)
20252025
await isReady
20262026
expect(middlewareExists()).toBeFalsy()

0 commit comments

Comments
 (0)