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(swagger-module): lazy load swagger-ui-dist module #2023

Merged
merged 2 commits into from
Sep 1, 2022

Conversation

kaioduarte
Copy link
Contributor

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?

Issue Number: N/A

What is the new behavior?

  • The swagger-ui-dist module is loaded only when serving the Swagger HTML (SwaggerModule.serveStatic).

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Bubbleprof from Clinicjs using the E2E app.

Before After
image image

Copy link
Member

@micalevisk micalevisk left a comment

Choose a reason for hiding this comment

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

this will be great for serverless apps! thanks

@kamilmysliwiec
Copy link
Member

Could we use await import() instead of require 🤔 ?

@kaioduarte
Copy link
Contributor Author

@kamilmysliwiec we could, but that will require users to add await to the SwaggerModule.setup method.

@kamilmysliwiec
Copy link
Member

So this is something that makes me a little concerned. If we wanted to support ESM (eventually), we'd have to use async import anyway (for lazy imports)

@kaioduarte
Copy link
Contributor Author

kaioduarte commented Aug 11, 2022

I'm fine doing that change. However, do I need to update any doc to warn users about the need to await the setup method in the next release?

@micalevisk
Copy link
Member

wouldn't that be a breaking change? Maybe we could keep require() + // TODO(v7): change to import statement.

@kamilmysliwiec
Copy link
Member

Yeah that would be a breaking change, and that's why I'm concerned too. If we stick with the current implementation (top-level require), even if we decide to move to ESM one day, we wouldn't have to introduce any breaking changes

@micalevisk
Copy link
Member

micalevisk commented Aug 11, 2022

yeah, indeed.

Another solution that I can think of:

SwaggerModule.setup('api', app, document) // this won't use lazy loading

await SwaggerModule.setup('api', app, document, { lazy: true })

then in the next major release of @nestjs/swagger we'll remove that parameter to always use the lazy loading mode. Not sure if this is feasible tho

@kaioduarte
Copy link
Contributor Author

Hey @kamilmysliwiec, what approach should we take? I'm not an ESM expert, but I'm happy to help and get this PR merged 🙂

@kaioduarte
Copy link
Contributor Author

🏓

@kamilmysliwiec
Copy link
Member

Let's ignore ESM for now. LGTM

@kamilmysliwiec kamilmysliwiec merged commit 1dc851c into nestjs:master Sep 1, 2022
@kaioduarte kaioduarte deleted the perf/lazy-load-swagger-ui branch September 1, 2022 09:42
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.

3 participants