Skip to content
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

fix: enormous memory consumption of the dev server #4148

Merged
merged 12 commits into from
Jun 9, 2023
26 changes: 26 additions & 0 deletions .changeset/good-oranges-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
"@blitzjs/rpc": patch
---

Fixes enormous memory consumption of the dev server by changing the default import strategy to "require" instead of "import" which in webpack causes multiple chunks to be created for each import.

## Blitz Configuration

To configure this behaviour, you can add the following to your next.config.js:

```js
/**
* @type {import('@blitzjs/next').BlitzConfig}
**/
const config = {
blitz: {
resolversDynamicImport: true,
},
}
```

When `resolversDynamicImport` is set to `true`, the default import strategy will be "import" instead of "require".
siddhsuresh marked this conversation as resolved.
Show resolved Hide resolved

### On Vercel

If you are using Vercel, `resolversDynamicImport` will be set to `true` by default, since it is better for the separate chunks to be create for serverless lambdas.
3 changes: 3 additions & 0 deletions apps/toolkit-app/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const { withBlitz } = require("@blitzjs/next")
**/
const config = {
reactStrictMode: true,
blitz: {
resolversDynamicImport: true,
},
}

module.exports = withBlitz(withNextAuthAdapter(config))
1 change: 1 addition & 0 deletions packages/blitz-next/src/index-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ export const setupBlitzServer = <TPlugins extends readonly BlitzServerPlugin<obj
export interface BlitzConfig extends NextConfig {
blitz?: {
resolverPath?: ResolverPathOptions
resolversDynamicImport: boolean
includeRPCFolders?: string[]
customServer?: {
hotReload?: boolean
Expand Down
5 changes: 3 additions & 2 deletions packages/blitz-rpc/src/index-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export function rpcHandler(config: RpcConfig) {
const routePath = "/" + relativeRoutePath

const log = baseLogger().getSubLogger({
name: "blitz-rpc",
prefix: [routePath.replace(/(\/api\/rpc)?\//, "") + "()"],
})
const customChalk = new chalk.Instance({
Expand Down Expand Up @@ -220,7 +221,7 @@ export function rpcHandler(config: RpcConfig) {
const startTime = Date.now()
const result = await resolver(data, (res as any).blitzCtx)
const resolverDuration = Date.now() - startTime
log.debug(customChalk.dim("Result:"), result ? result : JSON.stringify(result))
log.info(customChalk.dim("Result:"), result ? result : JSON.stringify(result))

const serializerStartTime = Date.now()
const serializedResult = superjsonSerialize(result)
Expand All @@ -242,7 +243,7 @@ export function rpcHandler(config: RpcConfig) {
const serializerDuration = Date.now() - serializerStartTime
const duration = Date.now() - startTime

log.info(
log.debug(
customChalk.dim(
`Finished: resolver:${prettyMs(resolverDuration)} serializer:${prettyMs(
serializerDuration,
Expand Down
11 changes: 9 additions & 2 deletions packages/blitz-rpc/src/server/loader/server/loader-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,19 @@ export async function transformBlitzRpcServer(
extraRpcBasePaths: options?.includeRPCFolders,
})

code += `__internal_addBlitzRpcResolver('${routePath}',() => import('${slash(
if (options && !options?.resolversDynamicImport) {
//set resolverDynamicImport to default be true if on vercel
options.resolversDynamicImport = process.env.VERCEL ? true : false
}

const importStrategy = options?.resolversDynamicImport ? "import" : "require"

code += `__internal_addBlitzRpcResolver('${routePath}',() => ${importStrategy}('${slash(
resolverFilePath,
)}'));`
code += "\n"
}
// console.log("NEW CODE", code)

return code
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {ResolverPathOptions} from "../../../index-server"
export interface LoaderOptions {
resolverPath: ResolverPathOptions
includeRPCFolders?: string[]
resolversDynamicImport?: boolean
}

export interface Loader {
Expand Down
10 changes: 3 additions & 7 deletions packages/blitz/src/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@ export const baseLogger = (options: BlitzLoggerSettings = {}): Logger<ILogObj> =

export const BlitzLogger = (settings: BlitzLoggerSettings = {}) => {
const baseLogger = new Logger({
minLevel: 3,
type: "pretty",
prettyLogTemplate:
process.env.NODE_ENV === "production"
? "{{yyyy}}-{{mm}}-{{dd}} {{hh}}:{{MM}}:{{ss}}:{{ms}}"
: "{{hh}}:{{MM}}:{{ss}}:{{ms}}",
prettyLogTimeZone: process.env.NODE_ENV === "production" ? "UTC" : "local",
maskValuesOfKeys: ["password", "passwordConfirmation", "currentPassword"],
// exposeErrorCodeFrame: process.env.NODE_ENV !== "production",
type: process.env.NODE_ENV === "production" ? "json" : "pretty",
prettyLogTemplate:
"{{yyyy}}.{{mm}}.{{dd}} {{hh}}:{{MM}}:{{ss}}:{{ms}}\t{{logLevelName}}\t[{{filePathWithLine}}{{name}}]\t",
...settings,
})

Expand Down