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

perf: added possibility to lazy initialize the document #2392

Merged
merged 1 commit into from
Jun 12, 2023

Conversation

H4ad
Copy link
Contributor

@H4ad H4ad commented Apr 15, 2023

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

Currently, NestJS builds the Swagger document synchronously on startup, which it's a waste of time since we don't care about the swagger routes until we need to make a call to it.

What is the new behavior?

So, I added an option to lazy initialize the Swagger document, the document will only be built when we have some request for it.

In my API, it reduced about 70~75ms from initialization, it will vary according to the API and the amount of routes but now it will only take 2ms to initialize the Swagger Module.

Usage

Now, to use the new lazy loading feature, just change these lines:

const swaggerDocument = new DocumentBuilder()
  .setTitle(config.get<string>('SWAGGER_TITLE') ?? 'Base')
  .setDescription(config.get<string>('SWAGGER_DESCRIPTION') ?? 'The base API')
  .setVersion(config.get<string>('VERSION') ?? '1.0.0')
  .addTag('main')
  .addBearerAuth()
  .build();

-const document = SwaggerModule.createDocument(app, swaggerDocument);
+const document = () => SwaggerModule.createDocument(app, swaggerDocument);

SwaggerModule.setup('/swagger', app, document, {
  swaggerOptions: { docExpansion: 'none' },
});

Just one line to use it.

Reproduction

To use it: https://stackblitz.com/edit/nestjs-starter-bql4af?file=src/main.ts

Does this PR introduce a breaking change?

  • Yes
  • No

@micalevisk
Copy link
Member

A good improvement even for that small app 🎉

eager lazy
before after

I wonder if the API couldn't be this instead:

const lazyDocument = () => SwaggerModule.createDocument(app, swaggerDocument)
SwaggerModule.setup('/swagger', app, lazyDocument, {
  swaggerOptions: { docExpansion: 'none' }
})

@H4ad
Copy link
Contributor Author

H4ad commented Apr 15, 2023

@micalevisk Yeah, I think your way could be better since I don't need to add more options to SwaggerCustomOptions.

@kamilmysliwiec
Copy link
Member

@micalevisk suggestions looks good!

Also, @H4ad would you like to create a PR to the docs (describing this feature)?

@H4ad
Copy link
Contributor Author

H4ad commented Apr 17, 2023

Updated with the new version, it was cleaner than the older version.

@H4ad
Copy link
Contributor Author

H4ad commented Apr 17, 2023

Docs PR: nestjs/docs.nestjs.com#2704

Copy link
Contributor

@Tony133 Tony133 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some minor corrections for code identation, for the rest is ok 👍

lib/swagger-module.ts Outdated Show resolved Hide resolved
lib/swagger-module.ts Outdated Show resolved Hide resolved
lib/swagger-module.ts Outdated Show resolved Hide resolved
lib/swagger-module.ts Outdated Show resolved Hide resolved
lib/swagger-module.ts Outdated Show resolved Hide resolved
lib/swagger-module.ts Outdated Show resolved Hide resolved
chore(lib): fix style

Co-authored-by: Antonio Tripodi <Tony133@users.noreply.github.com>

chore(lib): fix style

Co-authored-by: Antonio Tripodi <Tony133@users.noreply.github.com>

chore(lib): fix style

Co-authored-by: Antonio Tripodi <Tony133@users.noreply.github.com>

chore(lib): fix style

Co-authored-by: Antonio Tripodi <Tony133@users.noreply.github.com>

chore(lib): fix style

Co-authored-by: Antonio Tripodi <Tony133@users.noreply.github.com>

chore(lib): fix style

Co-authored-by: Antonio Tripodi <Tony133@users.noreply.github.com>

fix: issue with swagger module on undefined variable
@H4ad H4ad force-pushed the perf/lazy-initialization branch from c247a0d to e876270 Compare May 29, 2023 01:10
@H4ad
Copy link
Contributor Author

H4ad commented May 29, 2023

Fixed issues with e2e tests!

@kamilmysliwiec kamilmysliwiec changed the base branch from master to 7.0.0 June 12, 2023 12:25
@kamilmysliwiec kamilmysliwiec merged commit 9682d79 into nestjs:7.0.0 Jun 12, 2023
@H4ad H4ad deleted the perf/lazy-initialization branch June 12, 2023 13:29
@kamilmysliwiec
Copy link
Member

It seems that the PR caused this regression #2481

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants