Files
Deep-Package-Inspection/docs/log/2026-07-24-1015-adhoc-fix-middleware.md
T

34 lines
3.0 KiB
Markdown

# Iteration Log - 2026-07-24-1015 - Restore Next.js Middleware Routing (Ad-hoc)
## Request
The user reported an error/bug/crash ("This page couldn't load" screen) after logging in or loading the app on `https://demoplace.my.id/`.
## Steps Taken
1. **System Health Check (SSH)**:
- Checked PM2 status: `backone-backend`, `backone-frontend`, and `backone-proxy` were online, though proxy had high restarts from previous configurations.
- Checked proxy logs (`logs/proxy-out.log`): Connected successfully to MongoDB, schedulers active, deltas fetching successfully.
- Checked frontend logs (`logs/frontend-error.log`): Found older chunk-mismatch warnings ("Failed to find Server Action...") and recurring Node 18 crypto warnings.
2. **Root Cause Identification**:
- Analyzed previous changes which renamed `src/middleware.ts` to `src/proxy.ts` (exporting a `proxy` function) based on an experimental Next.js 16 deprecation warning.
- Discovered that when using `src/proxy.ts` in Next.js, the production build (`npm run build`) generated an empty `middleware-manifest.json` (`"middleware": {}`), resulting in no server-side authentication checks or redirects.
3. **TDD Setup**:
- Wrote a new TDD test: [middleware_verification_test.js](file:///c:/Users/demo/Downloads/Deep%20Package%20Inspection/test/middleware_verification_test.js) asserting that `src/middleware.ts` exists and exports the correct `middleware` function.
- Ran `node test/middleware_verification_test.js` and confirmed it failed as expected.
4. **Resolution**:
- Created [middleware.ts](file:///c:/Users/demo/Downloads/Deep%20Package%20Inspection/src/middleware.ts) with the proper `middleware` function and matches.
- Deleted the obsolete `src/proxy.ts` file.
- Ran local build `npm run build` and verified that `middleware-manifest.json` is now correctly populated with routing matches.
- Ran the TDD test again; it passed successfully!
5. **Deployment & Verification**:
- Deployed updates via SFTP using `node scripts/deploy-sftp.js` and reloaded PM2.
- Verified that `/` correctly redirects to `/login` via remote command checks.
- Ran integration tests [test-remote-me.js](file:///c:/Users/demo/Downloads/Deep%20Package%20Inspection/test/test-remote-me.js) and [test-remote-summary.js](file:///c:/Users/demo/Downloads/Deep%20Package%20Inspection/test/test-remote-summary.js) to verify API data flows. Both returned `200 OK` with valid data.
## Current State
- The Next.js frontend has server-side routing restored via the correct `middleware.ts` setup.
- Authentication checks and redirects work correctly.
- Integration tests and API data fetches pass cleanly on production.
## Considerations for Next Time
- Although Next.js 16 shows a warning recommending renaming `middleware.ts` to `proxy.ts`, Next.js's standalone compiler support for the `proxy.ts` convention is still experimental and can produce empty middleware manifests under certain configurations. Restoring the standard `middleware.ts` naming ensures production builds are stable.