-
Notifications
You must be signed in to change notification settings - Fork 26k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
next.js server router no longer exposed in 10.0.8+ #23558
Comments
The change happened in commit 1ebc9bb in packages/next/server/next.ts when the return type of Would it be possible to make the |
I've opened a PR to update the automatically generated typing: #23573 |
Seems that some people (vercel#23558) are using Next.js internals based on the typings that TS generates automatically even though these values are not documented / should not be used in applications. This ensures the router value on Server is not used by accident.
* Mark this.router protected Seems that some people (#23558) are using Next.js internals based on the typings that TS generates automatically even though these values are not documented / should not be used in applications. This ensures the router value on Server is not used by accident. * Mark all variables as protected #23349 * fix type error Co-authored-by: JJ Kasper <jj@jjsweb.site>
@timneutkens No information in docs about checking routes in custom servers. Now I have to make ugly crutches in the code and track the changes in the Next.js code 😞 |
* Mark this.router protected Seems that some people (vercel#23558) are using Next.js internals based on the typings that TS generates automatically even though these values are not documented / should not be used in applications. This ensures the router value on Server is not used by accident. * Mark all variables as protected vercel#23349 * fix type error Co-authored-by: JJ Kasper <jj@jjsweb.site>
@timneutkens fyi, my crutches import type { NextServer } from 'next/dist/server/next'
import type Server from 'next/dist/next-server/server/next-server'
import LRUCache from 'mnemonist/lru-cache'
const pageCheckingCacheSize = 1000
// Cache for `checkIsPagePath()` results with limit size (LRU) to avoid memory overflow attack.
// Cache-key is a "pathname", without query parameters.
const pageCheckingCache = new LRUCache<string, boolean>(pageCheckingCacheSize)
/**
* Ugly crutches for detection of page request,
* coherent with Next.js undocumented protected mechanics.
* !!! Be careful when update Next.js !!!
* @see https://github.com/vercel/next.js/issues/23558#issuecomment-820655560
* @param app - Accessible next-server.
* @param path - Request path.
* @returns Promise with answer to question "Is this SSR page path?"
*/
export default async function checkIsSSRPath(app: NextServer, path: string): Promise<boolean> {
// https://nodejs.org/api/url.html#url_url_strings_and_url_objects
const searchPosition = path.indexOf('?')
const pathname = searchPosition > -1 ? path.slice(0, searchPosition) : path
let result = pageCheckingCache.get(pathname)
if (result === undefined) {
result =
// Fast cutoff static and SSP (SSP detected separately outside).
!pathname.startsWith('/_next/')
// @ts-expect-error Need access to protected router.
&& ((app.server as Server).router.dynamicRoutes.some(({ match }) => match(pathname))
// @ts-expect-error Need access to protected router.
|| await (app.server as Server).router.pageChecker(pathname))
pageCheckingCache.set(pathname, result)
}
return result
} |
* Mark this.router protected Seems that some people (vercel#23558) are using Next.js internals based on the typings that TS generates automatically even though these values are not documented / should not be used in applications. This ensures the router value on Server is not used by accident. * Mark all variables as protected vercel#23349 * fix type error Co-authored-by: JJ Kasper <jj@jjsweb.site>
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
What version of Next.js are you using?
10.0.8, 10.0.9, 10.1.x
What version of Node.js are you using?
14
What browser are you using?
chrome
What operating system are you using?
macOS
How are you deploying your application?
AWS
Describe the Bug
In next.js 10.0.8+, the return value of calling the next custom server function (i.e.
const app = next({})
) no longer has a router field.This router was exposed in previous versions and was not marked as internal or deprecated.
(Note the 'old' server with the router is still accessible via the .server field, but that is marked as a private field in the new NextServer class)
Expected Behavior
Accessing the router is possible
To Reproduce
install next 10.0.8, 10.0.9, or 10.1.x
The text was updated successfully, but these errors were encountered: