6bc7c4d9c2
Related to #34185, this PR reduces the size of chunk that contains web-server.ts from 1.14mb to 210.8kb, by splitting base-http and api-utils into different environments. Only affected thing is we can't have SSG preview mode for the web runtime via `getStaticProps`. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint`
75 lines
2.1 KiB
TypeScript
75 lines
2.1 KiB
TypeScript
import { parse as parseUrl } from 'url'
|
|
import { IncomingMessage, ServerResponse } from 'http'
|
|
import { apiResolver } from '../../../../server/api-utils/node'
|
|
import { getUtils, vercelHeader, ServerlessHandlerCtx } from './utils'
|
|
import { DecodeError } from '../../../../shared/lib/utils'
|
|
import {
|
|
NodeNextResponse,
|
|
NodeNextRequest,
|
|
} from '../../../../server/base-http/node'
|
|
|
|
export function getApiHandler(ctx: ServerlessHandlerCtx) {
|
|
const { pageModule, encodedPreviewProps, pageIsDynamic } = ctx
|
|
const {
|
|
handleRewrites,
|
|
handleBasePath,
|
|
dynamicRouteMatcher,
|
|
normalizeDynamicRouteParams,
|
|
} = getUtils(ctx)
|
|
|
|
return async (
|
|
rawReq: NodeNextRequest | IncomingMessage,
|
|
rawRes: NodeNextResponse | ServerResponse
|
|
) => {
|
|
const req =
|
|
rawReq instanceof IncomingMessage ? new NodeNextRequest(rawReq) : rawReq
|
|
const res =
|
|
rawRes instanceof ServerResponse ? new NodeNextResponse(rawRes) : rawRes
|
|
|
|
try {
|
|
// We need to trust the dynamic route params from the proxy
|
|
// to ensure we are using the correct values
|
|
const trustQuery = req.headers[vercelHeader]
|
|
const parsedUrl = handleRewrites(req, parseUrl(req.url!, true))
|
|
|
|
if (parsedUrl.query.nextInternalLocale) {
|
|
delete parsedUrl.query.nextInternalLocale
|
|
}
|
|
handleBasePath(req, parsedUrl)
|
|
|
|
let params = {}
|
|
|
|
if (pageIsDynamic) {
|
|
const result = normalizeDynamicRouteParams(
|
|
trustQuery
|
|
? parsedUrl.query
|
|
: (dynamicRouteMatcher!(parsedUrl.pathname) as Record<
|
|
string,
|
|
string | string[]
|
|
>)
|
|
)
|
|
|
|
params = result.params
|
|
}
|
|
|
|
await apiResolver(
|
|
req.originalRequest,
|
|
res.originalResponse,
|
|
Object.assign({}, parsedUrl.query, params),
|
|
await pageModule,
|
|
encodedPreviewProps,
|
|
true
|
|
)
|
|
} catch (err) {
|
|
console.error(err)
|
|
|
|
if (err instanceof DecodeError) {
|
|
res.statusCode = 400
|
|
res.body('Bad Request').send()
|
|
} else {
|
|
// Throw the error to crash the serverless function
|
|
throw err
|
|
}
|
|
}
|
|
}
|
|
}
|