Repository: vercel/next.js
File: packages/next/src/server/dev/hot-reloader-turbopack.ts (TypeScript)

Code before:
        ) {
          hasChange = true
          serverPathState.set(localKey, contentHash)
          serverPathState.set(path, contentHash)
        } else {
          if (!localHash) {
            serverPathState.set(localKey, contentHash)
          }
          if (!globalHash) {
            serverPathState.set(path, contentHash)
          }
        }
      }

      if (!hasChange) {
        return false
      }
    }

    const serverPaths = writtenEndpoint.serverPaths.map(({ path: p }) =>
      join(distDir, p)
    )

    const { type: entryType, page: entryPage } = splitEntryKey(key)
    // Server HMR only applies to app router pages since these use the Turbopack runtime.
    // Pages Router uses Node's require(), root entries (middleware/instrumentation)
    // use the edge runtime, and App Router route handlers (app-route) are plain
    // Node.js modules.
    const isAppPage =
      entryType === 'app' &&
      currentEntrypoints.app.get(entryPage)?.type === 'app-page'
    const usesServerHmr =
      experimentalServerFastRefresh &&
      entryType === 'app' &&
      isAppPage &&
      writtenEndpoint.type !== 'edge'

    for (const file of serverPaths) {
      clearModuleContext(file)

      const relativePath = relative(distDir, file)
      if (
        // For Pages Router, edge routes, middleware, and manifest files
        // (e.g., *_client-reference-manifest.js): clear the sharedCache in
        // evalManifest(), Node.js require.cache, and edge runtime module contexts.
        force ||
        !usesServerHmr ||
        !serverHmrSubscriptions?.has(relativePath)
      ) {
        deleteCache(file)
      }

Review comment (refactor):
nit: this condition is duplicated, inline the `currentEntrypoints.app.get(entryPage)?.type === 'app-page'` into the expression?



Code after:
        ) {
          hasChange = true
          serverPathState.set(localKey, contentHash)
          serverPathState.set(path, contentHash)
        } else {
          if (!localHash) {
            serverPathState.set(localKey, contentHash)
          }
          if (!globalHash) {
            serverPathState.set(path, contentHash)
          }
        }
      }

      if (!hasChange) {
        return false
      }
    }

    const serverPaths = writtenEndpoint.serverPaths.map(({ path: p }) =>
      join(distDir, p)
    )

    const { type: entryType, page: entryPage } = splitEntryKey(key)
    const isAppPage =
      entryType === 'app' &&
      currentEntrypoints.app.get(entryPage)?.type === 'app-page'

    // Server HMR only applies to app router pages since these use the Turbopack runtime.
    // Currently, this is only app router pages.
    //
    // This excludes:
    //   - Pages Router pages
    //   - Edge routes
    //   - Middleware
    //   - App Router route handlers (route.ts)
    const usesServerHmr =
      experimentalServerFastRefresh &&
      isAppPage &&
      writtenEndpoint.type !== 'edge'

    for (const file of serverPaths) {
      clearModuleContext(file)

      const relativePath = relative(distDir, file)
      if (
        // For Pages Router, edge routes, middleware, and manifest files
        // (e.g., *_client-reference-manifest.js): clear the sharedCache in
        // evalManifest(), Node.js require.cache, and edge runtime module contexts.
        force ||
        !usesServerHmr ||