next.js
08e7410f - remove inheritance from NextCustomServer (#73005)

Commit
1 year ago
remove inheritance from NextCustomServer (#73005) ### What This PR changes `NextCustomServer` to no longer subclass `NextServer` and instead just delegate all methods to `this.renderServer`. I've also added an interface implemented by both `NextServer` and `NextCustomServer` to make it explicit what we're actually exposing. (Note that we're currently exposing too much for legacy reasons. we might want to decide to clean this up a bit, i've started that in https://github.com/vercel/next.js/pull/73021) I've also cleaned up some random `(_ as any)`s and unnecessary indirections around the area. ### Why currently, `NextCustomServer` is a subclass of `NextServer`, which makes it very confusing to reason about, because in `prepare()` it creates *a nested `NextServer`* (stored in `this.renderServer`). this isn't immediately visible in the code, but that's what `getRequestHandlers` ends up doing. https://github.com/vercel/next.js/blob/58b993c239689f22641682bbcb0d8dea6d85fdf1/packages/next/src/server/next.ts#L281 then, it delegates *some* methods to that (`render`, `getRequestHandler`, `setAssetPrefix`). But some methods (e.g. `render404`) *aren't* overridden/delegated and just run the implementation inherited from `NextServer`. And those implementations are unaware of `this.renderServer`, so they end up initializing a *second* `NextNodeServer` (`this.server` vs `this.renderServer.server`) and run against that instead. that's bad! and it's why e.g. #61676 was necessary -- we needed to update both servers! i think that this is a leftover of the old implementation where we ran a separate process for rendering. for historical context, see: - #49805 which seems to have introduced this split (the proxy only overrides `prepare`, `getRequestHandler`, and `render`) - #53523 which turned the proxy code into `NextCustomServer` also apparently `render404` and others were broken in development (try running the tests in the first commit I've changed `NextCustomServer` and see the server hang forever).
Author
Parents
Loading