Skip to content

fix: only split extended routes to decrease build times #1731

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

Merged
merged 5 commits into from
Nov 2, 2022

Conversation

nickytonline
Copy link

@nickytonline nickytonline commented Nov 1, 2022

Summary

Now we only create custom rewrites and functions for background and scheduled functions. Prior to this PR, we were splitting all API routes and creating separate functions and rewrites for them. This should decrease build times drastically (back to normal) for folks not using extended API routes (scheduled and background functions), and for those who are, the build time should be what it was plus whatever amount of time it takes to build the functions for the extended API routes.

Test plan

From a user facing perspective nothing has changed, so all dmoe sites and their corresponding tests should pass.

As well, the logs for the default demo site deploy preview, https://deploy-preview-1731--netlify-plugin-nextjs-demo.netlify.app/, should only create the following functions (see https://app.netlify.com/sites/netlify-plugin-nextjs-demo/deploys/63619a9da96eb900081e6cdf#L824-L831)

────────────────────────────────────────────────────────────────
  4. Functions bundling                                         
────────────────────────────────────────────────────────────────

The Netlify Functions setting targets a non-existing directory: netlify/functions

Packaging Functions from .netlify/functions-internal directory:
 - ___netlify-handler/___netlify-handler.js
 - ___netlify-odb-handler/___netlify-odb-handler.js
 - _api_hello-background-background/_api_hello-background-background.js
 - _api_hello-scheduled-handler/_api_hello-scheduled-handler.js
 - _ipx/_ipx.js
 - 

Relevant links (GitHub issues, Notion docs, etc.) or a picture of cute animal

Closes https://github.com/netlify/pod-ecosystem-frameworks/issues/284
Closes https://github.com/netlify/pod-ecosystem-frameworks/issues/282 potentially
Relates to #1495

Just a note that on my own site, these behave as expected

CleanShot 2022-11-01 at 19 36 30

and these tests are still passing with the changes made.

https://github.com/netlify/next-runtime/blob/91ea2a67170bebee07db437d5496e58f2911e81e/cypress/integration/default/api.spec.ts#L9-L18

Standard checks:

  • Check the Deploy Preview's Demo site for your PR's functionality
  • Add docs when necessary

Seth Meyers making hand gestures to indicate better


🧪 Once merged, make sure to update the version if needed and that it was published correctly.

@netlify
Copy link

netlify bot commented Nov 1, 2022

Deploy Preview for netlify-plugin-nextjs-nx-monorepo-demo ready!

Name Link
🔨 Latest commit 673dfc0
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-nx-monorepo-demo/deploys/636268e1b567d000085bb7d9
😎 Deploy Preview https://deploy-preview-1731--netlify-plugin-nextjs-nx-monorepo-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Nov 1, 2022

Deploy Preview for netlify-plugin-nextjs-export-demo ready!

Name Link
🔨 Latest commit 673dfc0
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-export-demo/deploys/636268e192d40b0008ee439c
😎 Deploy Preview https://deploy-preview-1731--netlify-plugin-nextjs-export-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Nov 1, 2022

Deploy Preview for netlify-plugin-nextjs-static-root-demo ready!

Name Link
🔨 Latest commit 673dfc0
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-static-root-demo/deploys/636268e1c2a3020008a3ef66
😎 Deploy Preview https://deploy-preview-1731--netlify-plugin-nextjs-static-root-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@github-actions github-actions bot added the type: chore work needed to keep the product and development running smoothly label Nov 1, 2022
@nickytonline nickytonline changed the title chore: potential fix fix: unsplit some routes to decrease build times Nov 1, 2022
@github-actions github-actions bot added the type: bug code to address defects in shipped code label Nov 1, 2022
@netlify
Copy link

netlify bot commented Nov 1, 2022

Deploy Preview for next-i18next-demo ready!

Name Link
🔨 Latest commit 673dfc0
🔍 Latest deploy log https://app.netlify.com/sites/next-i18next-demo/deploys/636268e1ae5ad20008d20294
😎 Deploy Preview https://deploy-preview-1731--next-i18next-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Nov 1, 2022

Deploy Preview for netlify-plugin-nextjs-demo ready!

Name Link
🔨 Latest commit 673dfc0
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-demo/deploys/636268e1c295af0008658190
😎 Deploy Preview https://deploy-preview-1731--netlify-plugin-nextjs-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Nov 1, 2022

Deploy Preview for next-plugin-canary ready!

Name Link
🔨 Latest commit 673dfc0
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-canary/deploys/636268e180afd30008ced72a
😎 Deploy Preview https://deploy-preview-1731--next-plugin-canary.netlify.app/
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@nickytonline nickytonline force-pushed the nickytonline/unsplit-some-routes branch from a582d74 to 3ba3160 Compare November 1, 2022 15:38
@netlify
Copy link

netlify bot commented Nov 1, 2022

Deploy Preview for netlify-plugin-nextjs-next-auth-demo ready!

Name Link
🔨 Latest commit a582d74
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-next-auth-demo/deploys/63613cd58c66940008522b19
😎 Deploy Preview https://deploy-preview-1731--netlify-plugin-nextjs-next-auth-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Nov 1, 2022

Deploy Preview for next-plugin-edge-middleware ready!

Name Link
🔨 Latest commit a582d74
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-edge-middleware/deploys/63613cd57cd984000859816e
😎 Deploy Preview https://deploy-preview-1731--next-plugin-edge-middleware.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Nov 1, 2022

Deploy Preview for nextjs-plugin-custom-routes-demo ready!

Name Link
🔨 Latest commit a582d74
🔍 Latest deploy log https://app.netlify.com/sites/nextjs-plugin-custom-routes-demo/deploys/63613cd5f6f3d2000891b5b7
😎 Deploy Preview https://deploy-preview-1731--nextjs-plugin-custom-routes-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Nov 1, 2022

Deploy Preview for next-plugin-edge-middleware ready!

Name Link
🔨 Latest commit 673dfc0
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-edge-middleware/deploys/636268e134445700084a5624
😎 Deploy Preview https://deploy-preview-1731--next-plugin-edge-middleware.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Nov 1, 2022

Deploy Preview for netlify-plugin-nextjs-next-auth-demo ready!

Name Link
🔨 Latest commit 673dfc0
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-next-auth-demo/deploys/636268e1c665eb0008230485
😎 Deploy Preview https://deploy-preview-1731--netlify-plugin-nextjs-next-auth-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Nov 1, 2022

Deploy Preview for nextjs-plugin-custom-routes-demo ready!

Name Link
🔨 Latest commit 673dfc0
🔍 Latest deploy log https://app.netlify.com/sites/nextjs-plugin-custom-routes-demo/deploys/636268e1a5177100089b2895
😎 Deploy Preview https://deploy-preview-1731--nextjs-plugin-custom-routes-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@nickytonline nickytonline force-pushed the nickytonline/unsplit-some-routes branch from 1050817 to 948363c Compare November 1, 2022 18:05
"from": "/api/hello",
"status": 200,
"to": "/.netlify/functions/_api_hello-handler",
},
Object {
Copy link
Author

Choose a reason for hiding this comment

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

Only generate custom rewrites for advanced API routes (background and scheduled functions).

Copy link
Contributor

Choose a reason for hiding this comment

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

Where are we mapping the other API routes to the SSR handler?

Copy link
Author

Choose a reason for hiding this comment

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

From what I understood of the previous implementation, the /api/* has a handler that catches all other requests.

     { 
       from: `${basePath}/api/*`, 
       to: HANDLER_FUNCTION_PATH, 
       status: 200, 
     },

See https://github.com/netlify/next-runtime/blob/673dfc09dc730f0ab91ee4ceec2ab9e8dd745266/packages/runtime/src/helpers/utils.ts#L147-L173

Copy link
Author

@nickytonline nickytonline left a comment

Choose a reason for hiding this comment

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

Looks like only the required functions are building now. I was seeing the old page routes generate functions still, but it looks like doing a git clean -dfx, npm install, then a fresh ntl build works.

────────────────────────────────────────────────────────────────
  4. Functions bundling                                         
────────────────────────────────────────────────────────────────

The Netlify Functions setting targets a non-existing directory: netlify/functions

Packaging Functions from .netlify/functions-internal directory:
 - ___netlify-handler/___netlify-handler.js
 - ___netlify-odb-handler/___netlify-odb-handler.js
 - _api_hello-background-background/_api_hello-background-background.js
 - _api_hello-scheduled-handler/_api_hello-scheduled-handler.js
 - _ipx/_ipx.js

@nickytonline nickytonline marked this pull request as ready for review November 1, 2022 23:29
@nickytonline nickytonline requested a review from a team November 1, 2022 23:29
@nickytonline nickytonline self-assigned this Nov 1, 2022
@nickytonline nickytonline requested a review from ascorbic November 1, 2022 23:50
@nickytonline nickytonline removed the type: chore work needed to keep the product and development running smoothly label Nov 2, 2022
@nickytonline nickytonline changed the title fix: unsplit some routes to decrease build times fix: only split extended routes to decrease build times Nov 2, 2022
Copy link
Contributor

@ascorbic ascorbic left a comment

Choose a reason for hiding this comment

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

Thanks for handling this. Mostly this looks great: just a couple of questions and comments

"from": "/api/hello",
"status": 200,
"to": "/.netlify/functions/_api_hello-handler",
},
Object {
Copy link
Contributor

Choose a reason for hiding this comment

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

Where are we mapping the other API routes to the SSR handler?

Copy link
Contributor

@ascorbic ascorbic left a comment

Choose a reason for hiding this comment

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

Looks great! :shipit:

@nickytonline nickytonline merged commit 1e6fb8c into main Nov 2, 2022
@nickytonline nickytonline deleted the nickytonline/unsplit-some-routes branch November 2, 2022 16:52
Skn0tt added a commit that referenced this pull request Apr 20, 2023
Brings back the functionality that was reverted in
#1731, but under a flag.
This will be utterly slow in building,
so let's try to speed that up in the next step!
Skn0tt added a commit that referenced this pull request May 12, 2023
…2058)

* feat: split up API Routes

Brings back the functionality that was reverted in
#1731, but under a flag.
This will be utterly slow in building,
so let's try to speed that up in the next step!

* feat: load includedFiles for every page

* refactor: extract function config logic

* refactor: extract flag into own definition

* feat: use "none" bundler for split-up api routes

* feat: list some more dependencies

* feat: use NFT to trace common required files

* refactor: clean up a wee bit

* fix: please don't include /sh

* feat: enable flag by default, so tests use it

* feat: add a naïve packing algo

* feat: write rough sketch for packing lambdas

* refactor: add constructor for APILambda

* feat: pack handlers together into bundles

* fix: linter

* feat: exclude some heavy unneeded files

* fix: trigger CI again, now that it supports `none` bundler

* feat: remove code for old mechanism

If we'll be doing a staged rollout,
and our test suite covers most of the cases,
we should be able to roll this out
without a self-serve opt-out mechanism.

* fix: remove test for deleted code

* fix: ensure that react doesn't try to load development build

* fix: move test directory into repo, so node_modules can be read

* fix: snapshot with API redirects

* fix: remove .only

* fix: don't assert on _api_*

* fix: another test

* fix: apply NODE_ENV=prod to right generated handler

* feat: remove nft tracing

* feat: put change behind flag again

* feat: source flag from plugin input

* fix: add default value for featureflags

* fix: default flag to true for testing

* fix: revert changes to lockfiles

* fix: eslint it/test

* fix: lint

* fix: revert distracting change

* fix: now that we don't use nft anymore, we don't have to copy over node_modules

* fix: swallow require.resolve errors for unit tests

* fix: remove timing logs

* fix: lint name

* fix: lint

* fix: isr needs _document.js

* fix: add _app for ISR

* fix: correct wrong output of some npm versions

* fix: integration test

For some reason,
ZISI doesn't like relative paths in this integration test.
We can fix it by using absolute paths.

Since this PR moves API routes out of __netlify_handler,
we can no longer make the assertion on x-nf-render-mode.

* fix: assemble npm package path correctly, also for monorepos

* fix: try what happens if we use next-netlify server

* fix: check what happens when we skip revalidation

* fix: dont let revalidate request time out

* fix: send x-nextjs-cache header to prevent error

* fix: update error message in test

* fix: resolve relative paths based on data in required-server-files

* fix: test

* fix: keep manually-added `included_files`

* fix: try something

* fix: don't include unneeded _app pages in ISR

* fix: finalize includedFiles before writing it onto netlifyConfig

* chore: update comment

* fix: exclude sass file in monorepos

* Update packages/runtime/src/helpers/functions.ts

* chore: remove comment

* fix: update flag impl

* refactor: use getRequiredServerFiles

* chore: add comment on route[0]

* fix: set NEXT_SPLIT_API_ROUTES in netlify.toml

* fix: put updated revalidate behaviour behind flag

* fix: supply splitApiRoutes in getHandler

* fix: better run your code before committing it and embarrassing yourself

---------

Co-authored-by: Nick Taylor <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants