Repository: vercel/next.js
Pull request: Routing: Test removing ensureFn from filesystem matcher

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the PR.
- Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to understand the PR)
- When linking to a Slack thread, you might want to share details of the conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes PACK-5109

Changed files:
filename: packages/next/src/server/lib/router-utils/filesystem.ts
patch: @@ -38,7 +38,7 @@ import {
   ROUTES_MANIFEST,
 } from '../../../shared/lib/constants'
 import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep'
-// import { normalizeMetadataRoute } from '../../../lib/metadata/get-metadata-route'
+import { normalizeMetadataRoute } from '../../../lib/metadata/get-metadata-route'
 import { RSCPathnameNormalizer } from '../../normalizers/request/rsc'
 import { PrefetchRSCPathnameNormalizer } from '../../normalizers/request/prefetch-rsc'
 import { encodeURIPath } from '../../../shared/lib/encode-uri-path'
@@ -107,6 +107,9 @@ export async function setupFsCheck(opts: {
   dev: boolean
   minimalMode?: boolean
   config: NextConfigComplete
+  addDevWatcherCallback?: (
+    arg: (files: Map<string, { timestamp: number }>) => void
+  ) => void
 }) {
   const getItemsLru = !opts.dev
     ? new LRUCache<FsOutput | null>(1024 * 1024, function length(value) {
@@ -402,7 +405,7 @@ export async function setupFsCheck(opts: {
   debug('pageFiles', pageFiles)
   debug('appFiles', appFiles)
 
-  // let ensureFn: (item: FsOutput) => Promise<void> | undefined
+  let ensureFn: (item: FsOutput) => Promise<void> | undefined
 
   const normalizers = {
     // Because we can't know if the app directory is enabled or not at this
@@ -435,9 +438,9 @@ export async function setupFsCheck(opts: {
     prerenderManifest,
     middlewareMatcher: middlewareMatcher as MiddlewareRouteMatch | undefined,
 
-    // ensureCallback(fn: typeof ensureFn): void {
-    //   ensureFn = fn
-    // },
+    ensureCallback(fn: typeof ensureFn) {
+      ensureFn = fn
+    },
 
     async getItem(itemPath: string): Promise<FsOutput | null> {
       const originalItemPath = itemPath
@@ -503,8 +506,9 @@ export async function setupFsCheck(opts: {
         let curItemPath = itemPath
         let curDecodedItemPath = decodedItemPath
 
+        const isDynamicOutput = type === 'pageFile' || type === 'appFile'
+
         if (i18n) {
-          const isDynamicOutput = type === 'pageFile' || type === 'appFile'
           const localeResult = handleLocale(
             itemPath,
             // legacy behavior allows visiting static assets under
@@ -657,18 +661,20 @@ export async function setupFsCheck(opts: {
                 }
               }
             } else if (type === 'pageFile' || type === 'appFile') {
-              // const isAppFile = type === 'appFile'
-              // if (
-              //   ensureFn &&
-              //   (await ensureFn({
-              //     type,
-              //     itemPath: isAppFile
-              //       ? normalizeMetadataRoute(curItemPath)
-              //       : curItemPath,
-              //   })?.catch(() => 'ENSURE_FAILED')) === 'ENSURE_FAILED'
-              // ) {
-              //   continue
-              // }
+              const isAppFile = type === 'appFile'
+              if (
+                // Test
+                !isAppFile &&
+                ensureFn &&
+                (await ensureFn({
+                  type,
+                  itemPath: isAppFile
+                    ? normalizeMetadataRoute(curItemPath)
+                    : curItemPath,
+                })?.catch(() => 'ENSURE_FAILED')) === 'ENSURE_FAILED'
+              ) {
+                continue
+              }
             } else {
               continue
             }

filename: packages/next/src/server/lib/router-utils/setup-dev-bundler.ts
patch: @@ -246,16 +246,16 @@ async function startWatcher(
     )
   }
 
-  // opts.fsChecker.ensureCallback(async function ensure(item) {
-  //   if (item.type === 'appFile' || item.type === 'pageFile') {
-  //     await hotReloader.ensurePage({
-  //       clientOnly: false,
-  //       page: item.itemPath,
-  //       isApp: item.type === 'appFile',
-  //       definition: undefined,
-  //     })
-  //   }
-  // })
+  opts.fsChecker.ensureCallback(async function ensure(item) {
+    if (item.type === 'appFile' || item.type === 'pageFile') {
+      await hotReloader.ensurePage({
+        clientOnly: false,
+        page: item.itemPath,
+        isApp: item.type === 'appFile',
+        definition: undefined,
+      })
+    }
+  })
 
   let resolved = false
   let prevSortedRoutes: string[] = []