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(netlify, netlify-edge): exclude static paths from server handler #2822

Merged

Conversation

serhalp
Copy link
Contributor

@serhalp serhalp commented Oct 30, 2024

πŸ”— Linked issue

N/A - discussed on Discord

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

The netlify preset was already using preferStatic: true which lets existing static assets take precedence over the function, but this now also avoids a function invocation for static paths that don't exist, i.e. this avoids an unnecessary dynamic 404 that could be served directly from the CDN.

The netlify-edge preset wasn't excluding anything, so this addresses both the 404 case and the existing asset case.

The 404 case is important because browsers frequently attempt to request hashed assets from previous deploys that have been invalidated. There's no reason for this to go through functions, since we know that the whole parent path is static.

This PR also adds tests for the netlify-edge preset, which were missing. They're mostly copied from the netlify preset tests.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

src/presets/netlify/utils.ts Outdated Show resolved Hide resolved
src/presets/netlify/utils.ts Outdated Show resolved Hide resolved
test/presets/netlify.test.ts Outdated Show resolved Hide resolved
The netlify preset was already using `preferStatic: true` which lets existing static
assets take precedence over the function, but this now also avoids a function invocation
for static paths that don't exist, i.e. this avoids an unnecessary dynamic 404 that could
be served directly from the CDN.

The `netlify-edge` preset wasn't excluding anything, so this addresses both the 404 case
and the existing asset case.

The 404 case is important because browsers frequently attempt to request hashed assets
from previous deploys that have been invalidated. There's no reason for this to go through
functions, since we know that the whole parent path is static.
@serhalp serhalp force-pushed the perf/netlify/avoid-static-all-asset-invocations branch from 5369c48 to ce72ea3 Compare October 30, 2024 16:31
test/presets/netlify.test.ts Outdated Show resolved Hide resolved
test/presets/netlify.test.ts Outdated Show resolved Hide resolved
test/presets/netlify.test.ts Outdated Show resolved Hide resolved
@pi0
Copy link
Member

pi0 commented Oct 30, 2024

Thanks for the PR dear @serhalp

It seems main blockers on PR are addition of tests (which are so nice) -- although i don't want the main improvement to be missed for 2.10 release, perhaps we can extract work on tests to an additional PR?

Main addition seems could be tested easily both manually or with a simple unit test for getStaticPaths utility.

test/tests.ts Outdated Show resolved Hide resolved
@serhalp serhalp marked this pull request as ready for review October 31, 2024 11:56
@serhalp serhalp requested a review from pi0 October 31, 2024 11:56
serhalp added a commit to serhalp/nitro that referenced this pull request Oct 31, 2024
Backporting nitrojs#2822 to the netlify-legacy preset.

Since it's using "v1" Netlify Functions
(https://docs.netlify.com/functions/lambda-compatibility), it doesn't have access to
`excludedPath` and `preferStatic`, so we implement this with redirects.
src/presets/netlify/utils.ts Outdated Show resolved Hide resolved
src/presets/netlify/utils.ts Outdated Show resolved Hide resolved
test/tests.ts Outdated Show resolved Hide resolved
serhalp added a commit to serhalp/nitro that referenced this pull request Oct 31, 2024
Backporting nitrojs#2822 to the netlify-legacy preset.

Since it's using "v1" Netlify Functions
(https://docs.netlify.com/functions/lambda-compatibility), it doesn't have access to
`excludedPath` and `preferStatic`, so we implement this with redirects.
serhalp added a commit to serhalp/nitro that referenced this pull request Oct 31, 2024
Backporting nitrojs#2822 to the netlify-legacy preset.

Since it's using "v1" Netlify Functions
(https://docs.netlify.com/functions/lambda-compatibility), it doesn't have access to
`excludedPath` and `preferStatic`, so we implement this with redirects.
@pi0 pi0 changed the title perf(netlify,netlify-edge): exclude static paths from SSR function perf(netlify, netlify-edge): exclude static paths from server handler Oct 31, 2024
serhalp added a commit to serhalp/nitro that referenced this pull request Oct 31, 2024
Backporting nitrojs#2822 to the netlify-legacy preset.

Since it's using "v1" Netlify Functions
(https://docs.netlify.com/functions/lambda-compatibility), it doesn't have access to
`excludedPath` and `preferStatic`, so we implement this with redirects.
@serhalp
Copy link
Contributor Author

serhalp commented Oct 31, 2024

@pi0 I made all the requested changes.

Here's a manual QA against a nuxt site (opted in to nuxt v4 + 2014-10-31 compat date for new preset):

@serhalp serhalp requested a review from pi0 October 31, 2024 14:44
@serhalp
Copy link
Contributor Author

serhalp commented Oct 31, 2024

And for good measure here's one against netlify-edge:

test/tests.ts Show resolved Hide resolved
@pi0
Copy link
Member

pi0 commented Oct 31, 2024

Thanks ❀️

@pi0 pi0 merged commit 3a24c3b into nitrojs:v2 Oct 31, 2024
4 checks passed
@pi0 pi0 mentioned this pull request Nov 1, 2024
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.

2 participants