Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Improve Perf #800

Merged
merged 9 commits into from
Apr 7, 2025
Merged

Improve Perf #800

merged 9 commits into from
Apr 7, 2025

Conversation

conico974
Copy link
Contributor

@conico974 conico974 commented Mar 27, 2025

All these changes only apply to Next 15+

Adds patches to the Next.js codebase to remove unused imports at runtime, improving cold start times. Early tests indicate a performance gain of 120-200 ms without any additional code changes.

Disables the default preloading of routes at startup (which was done in the background). Now by default, only the necessary routes are lazily loaded as needed.
Introduces a new configuration setting to control when routes are preloaded. The available options are:

  • none : No preloading, default option
  • withWaitUntil: Preload using platform provided waitUntil - Will only occur on the first request
  • onWarmerEvent : Preload on warmer events - Need to be done by the wrapper itself, only aws-lambda-streaming does it for now.
  • onStart: Preload at startup - Be aware this is a blocking operation

Copy link

changeset-bot bot commented Mar 27, 2025

🦋 Changeset detected

Latest commit: 5b6d8d8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@opennextjs/aws Patch
app-pages-router Patch
app-router Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

pkg-pr-new bot commented Mar 27, 2025

Open in StackBlitz

pnpm add https://pkg.pr.new/@opennextjs/aws@800

commit: 5b6d8d8

@conico974 conico974 marked this pull request as ready for review April 1, 2025 13:12
@martinezdylan
Copy link

Would love to see if we could get this one in - hoping to use onWarmerEvent to keep routes preloaded on the warm instances. When we're using available warm option on NextJS (ref. https://sst.dev/docs/component/aws/nextjs/#warm) the instance is technically up but routes aren't loaded - this causes us to still experience cold-ish starts despite the warmer.

cc. @nguyendon for vis

@conico974
Copy link
Contributor Author

@martinezdylan onWarmerEvent won't work for sst without some change there because sst intercept the warmer event and does it's own thing.
It should work if you use onStart

@martinezdylan
Copy link

I guess I'm confused, the PR description mentions this should technically be happening now by default? Wouldn't using onStart make no difference?

What we're experiencing suggests even with warm set to 20, we still are having to load / compile the routes when we first hit the endpoint.

@conico974
Copy link
Contributor Author

Because as said in the PR description, route preloading at the moment are done in the background, but warmer event return immediately, so the route are not preloaded, only NextServer itself.

The onStart from this PR is blocking, which means that it will not return before having done the preloading, but this would be for everything, not just the warmer event, real cold start would likely be slower (How much will depend on your app and deps)

Ideally you'd only use onWarmerEvent but this means that some change has to be done in sst itself

Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

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

Great PR!

My comments resolve around adding more documentation.

@@ -118,6 +118,20 @@ export interface ResolvedRoute {
type: RouteType;
}

/**
* The route preloading behavior. Only supported in Next 15+.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggest:

  • Add the default behavior of Next is disabled
  • Add some guidelines to pick a value

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

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

a couple nits but LGTM

@sommeeeer
Copy link
Contributor

This PR is so good!

@conico974 conico974 merged commit e9b37fd into opennextjs:main Apr 7, 2025
3 checks passed
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants