mirror of
https://github.com/vercel/next.js.git
synced 2026-09-20 02:25:18 +08:00
20ac9ce9cf
### What? Deprecate undocumented custom-server methods exposed by `next()` while preserving their runtime behavior. - Add TypeScript deprecation annotations and one-time runtime warnings for `setAssetPrefix()`, logging helpers, `revalidate()`, and legacy `render*` helpers. - Extend custom-server tests to cover warnings and warning deduplication. - Move unrelated test fixtures away from deprecated custom-server calls. ### Why? These methods are not part of the documented custom-server API, but existing applications may still rely on them. Warning-only deprecation gives users migration guidance without introducing a breaking runtime change. ### How? Warnings are emitted only through `NextCustomServer`, so framework-owned `NextServer` usage remains quiet. The warning text points `render*` callers toward `app.getRequestHandler()`, `setAssetPrefix()` callers toward configuration, and logging and revalidation callers toward supported application APIs. The tests retain compatibility assertions for existing behavior, add deliberate invocations for previously uncovered methods, verify once-per-process warning deduplication, and remove incidental warning sources in unrelated fixtures. <!-- NEXT_JS_LLM_PR -->
63 lines
1.8 KiB
JavaScript
63 lines
1.8 KiB
JavaScript
const { createServer } = require('http')
|
|
const { parse } = require('url')
|
|
const next = require('next')
|
|
const getPort = require('get-port')
|
|
const { requestIdStorage } = require('./als')
|
|
const quiet = process.env.USE_QUIET === 'true'
|
|
|
|
let requestId = 0
|
|
|
|
async function main() {
|
|
const port = await getPort()
|
|
const hostname = 'localhost'
|
|
let conf = undefined
|
|
|
|
if (process.env.PROVIDED_CONFIG) {
|
|
conf = require('./.next/required-server-files.json').config
|
|
conf.basePath = '/docs'
|
|
}
|
|
|
|
// when using middleware `hostname` and `port` must be provided below
|
|
const app = next({ hostname, port, quiet, conf })
|
|
const handle = app.getRequestHandler()
|
|
|
|
app.prepare().then(() => {
|
|
createServer((req, res) =>
|
|
requestIdStorage.run(requestId++, async () => {
|
|
try {
|
|
// Be sure to pass `true` as the second argument to `url.parse`.
|
|
// This tells it to parse the query portion of the URL.
|
|
const parsedUrl = parse(req.url, true)
|
|
let { pathname } = parsedUrl
|
|
|
|
if (conf?.basePath) {
|
|
pathname = pathname.replace(conf.basePath, '') || '/'
|
|
}
|
|
|
|
parsedUrl.pathname =
|
|
pathname === '/b'
|
|
? '/page-b'
|
|
: pathname === '/error'
|
|
? '/page-error'
|
|
: pathname
|
|
// TODO: Accept WHATWG URLs in Next.js request handlers
|
|
await handle(req, res, parsedUrl)
|
|
} catch (err) {
|
|
console.error('Error occurred handling', req.url, err)
|
|
res.statusCode = 500
|
|
res.end('Internal Server Error')
|
|
}
|
|
})
|
|
).listen(port, undefined, (err) => {
|
|
if (err) throw err
|
|
// Start mode
|
|
console.log(`- Local: http://${hostname}:${port}`)
|
|
})
|
|
})
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error(err)
|
|
process.exit(1)
|
|
})
|