-
Notifications
You must be signed in to change notification settings - Fork 157
Improve Perf #800
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
Improve Perf #800
Changes from all commits
eb88ced
f2bcf13
c50c133
f828165
d40bb77
1217fa3
5b41907
d71afec
5b6d8d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@opennextjs/aws": patch | ||
--- | ||
|
||
Some perf improvements : | ||
- Eliminate unnecessary runtime imports (i.e. dev react dependencies and next precompiled dev or turbopack dependencies) | ||
- Refactor route preloading to be either on-demand or using waitUntil or at the start or during warmerEvent. | ||
- Add a global function to preload routes when needed. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export { envVarRuleCreator, patchEnvVars } from "./patchEnvVar.js"; | ||
export { patchNextServer } from "./patchNextServer.js"; | ||
export { | ||
patchFetchCacheForISR, | ||
patchUnstableCacheForISR, | ||
} from "./patchFetchCacheISR.js"; | ||
export { patchFetchCacheSetMissingWaitUntil } from "./patchFetchCacheWaitUntil.js"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { createPatchCode } from "../astCodePatcher.js"; | ||
import type { CodePatcher } from "../codePatcher"; | ||
|
||
/** | ||
* Creates a rule to replace `process.env.${envVar}` by `value` in the condition of if statements | ||
* This is used to avoid loading unnecessary deps at runtime | ||
* @param envVar The env var that we want to replace | ||
* @param value The value that we want to replace it with | ||
* @returns | ||
*/ | ||
export const envVarRuleCreator = (envVar: string, value: string) => ` | ||
rule: | ||
kind: member_expression | ||
pattern: process.env.${envVar} | ||
inside: | ||
kind: parenthesized_expression | ||
stopBy: end | ||
inside: | ||
kind: if_statement | ||
fix: | ||
'${value}' | ||
`; | ||
|
||
export const patchEnvVars: CodePatcher = { | ||
name: "patch-env-vars", | ||
patches: [ | ||
// This patch will set the `NEXT_RUNTIME` env var to "node" to avoid loading unnecessary edge deps at runtime | ||
{ | ||
versions: ">=15.0.0", | ||
field: { | ||
pathFilter: /module\.compiled\.js$/, | ||
contentFilter: /process\.env\.NEXT_RUNTIME/, | ||
patchCode: createPatchCode(envVarRuleCreator("NEXT_RUNTIME", '"node"')), | ||
}, | ||
}, | ||
// This patch will set `NODE_ENV` to production to avoid loading unnecessary dev deps at runtime | ||
{ | ||
versions: ">=15.0.0", | ||
field: { | ||
pathFilter: | ||
/(module\.compiled|react\/index|react\/jsx-runtime|react-dom\/index)\.js$/, | ||
contentFilter: /process\.env\.NODE_ENV/, | ||
patchCode: createPatchCode( | ||
envVarRuleCreator("NODE_ENV", '"production"'), | ||
), | ||
}, | ||
}, | ||
// This patch will set `TURBOPACK` env to false to avoid loading turbopack related deps at runtime | ||
{ | ||
versions: ">=15.0.0", | ||
field: { | ||
pathFilter: /module\.compiled\.js$/, | ||
contentFilter: /process\.env\.TURBOPACK/, | ||
patchCode: createPatchCode(envVarRuleCreator("TURBOPACK", "false")), | ||
}, | ||
}, | ||
], | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { createPatchCode } from "../astCodePatcher.js"; | ||
import type { CodePatcher } from "../codePatcher.js"; | ||
|
||
// This rule will replace the `NEXT_MINIMAL` env variable with true in multiple places to avoid executing unwanted path (i.e. next middleware, edge functions and image optimization) | ||
export const minimalRule = ` | ||
conico974 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
rule: | ||
kind: member_expression | ||
pattern: process.env.NEXT_MINIMAL | ||
any: | ||
- inside: | ||
kind: parenthesized_expression | ||
stopBy: end | ||
inside: | ||
kind: if_statement | ||
any: | ||
- inside: | ||
kind: statement_block | ||
inside: | ||
kind: method_definition | ||
any: | ||
- has: {kind: property_identifier, field: name, regex: runEdgeFunction} | ||
- has: {kind: property_identifier, field: name, regex: runMiddleware} | ||
- has: {kind: property_identifier, field: name, regex: imageOptimizer} | ||
- has: | ||
kind: statement_block | ||
has: | ||
kind: expression_statement | ||
pattern: res.statusCode = 400; | ||
fix: | ||
'true' | ||
`; | ||
|
||
// This rule will disable the background preloading of route done by NextServer by default during the creation of NextServer | ||
export const disablePreloadingRule = ` | ||
rule: | ||
kind: statement_block | ||
inside: | ||
kind: if_statement | ||
any: | ||
- has: | ||
kind: member_expression | ||
pattern: this.nextConfig.experimental.preloadEntriesOnStart | ||
stopBy: end | ||
- has: | ||
kind: binary_expression | ||
pattern: appDocumentPreloading === true | ||
stopBy: end | ||
fix: | ||
'{}' | ||
`; | ||
|
||
// This rule is mostly for splitted edge functions so that we don't try to match them on the other non edge functions | ||
export const removeMiddlewareManifestRule = ` | ||
rule: | ||
kind: statement_block | ||
inside: | ||
kind: method_definition | ||
has: | ||
kind: property_identifier | ||
regex: getMiddlewareManifest | ||
fix: | ||
'{return null;}' | ||
`; | ||
|
||
export const patchNextServer: CodePatcher = { | ||
name: "patch-next-server", | ||
patches: [ | ||
// Skip executing next middleware, edge functions and image optimization inside NextServer | ||
{ | ||
versions: ">=15.0.0", | ||
field: { | ||
pathFilter: /next-server\.(js)$/, | ||
contentFilter: /process\.env\.NEXT_MINIMAL/, | ||
patchCode: createPatchCode(minimalRule), | ||
}, | ||
}, | ||
// Disable Next background preloading done at creation of `NetxServer` | ||
{ | ||
versions: ">=15.0.0", | ||
field: { | ||
pathFilter: /next-server\.(js)$/, | ||
contentFilter: /this\.nextConfig\.experimental\.preloadEntriesOnStart/, | ||
patchCode: createPatchCode(disablePreloadingRule), | ||
}, | ||
}, | ||
// Don't match edge functions in `NextServer` | ||
{ | ||
versions: ">=15.0.0", | ||
field: { | ||
pathFilter: /next-server\.(js)$/, | ||
contentFilter: /getMiddlewareManifest/, | ||
patchCode: createPatchCode(removeMiddlewareManifestRule), | ||
}, | ||
}, | ||
], | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,6 +118,21 @@ export interface ResolvedRoute { | |
type: RouteType; | ||
} | ||
|
||
/** | ||
* The route preloading behavior. Only supported in Next 15+. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggest:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no general guidelines really, it depends so much on the deployed app that everyone should do their own testing. I'll add a comment about that |
||
* Default behavior of Next is disabled. You should do your own testing to choose which one suits you best | ||
* - "none" - No preloading of the route at all. This is the default | ||
* - "withWaitUntil" - Preload the route using the waitUntil provided by the wrapper - If not supported, it will fallback to "none". At the moment only cloudflare wrappers provide a `waitUntil` | ||
* - "onWarmerEvent" - Preload the route on the warmer event - Needs to be implemented by the wrapper. Only supported in `aws-lambda-streaming` wrapper for now | ||
* - "onStart" - Preload the route before even invoking the wrapper - This is a blocking operation. The handler will only be created after all the routes have been loaded, it may increase the cold start time by a lot in some cases. Useful for long running server or in serverless with some careful testing | ||
* @default "none" | ||
*/ | ||
export type RoutePreloadingBehavior = | ||
| "none" | ||
| "withWaitUntil" | ||
| "onWarmerEvent" | ||
| "onStart"; | ||
|
||
export interface RoutingResult { | ||
internalEvent: InternalEvent; | ||
// If the request is an external rewrite, if used with an external middleware will be false on every server function | ||
|
@@ -170,7 +185,6 @@ export type IncludedWarmer = "aws-lambda" | "dummy"; | |
export type IncludedProxyExternalRequest = "node" | "fetch" | "dummy"; | ||
|
||
export type IncludedCDNInvalidationHandler = "cloudfront" | "dummy"; | ||
|
||
export interface DefaultOverrideOptions< | ||
E extends BaseEventOrResult = InternalEvent, | ||
R extends BaseEventOrResult = InternalResult, | ||
|
@@ -313,6 +327,8 @@ export interface FunctionOptions extends DefaultFunctionOptions { | |
* @deprecated This is not supported in 14.2+ | ||
*/ | ||
experimentalBundledNextServer?: boolean; | ||
|
||
routePreloadingBehavior?: RoutePreloadingBehavior; | ||
} | ||
|
||
export type RouteTemplate = | ||
|
Uh oh!
There was an error while loading. Please reload this page.