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(cloudflare): check for asset url #1236

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

Hebilicious
Copy link
Member

@Hebilicious Hebilicious commented May 14, 2023

Remove KV access when an API route is called.

πŸ”— Linked issue

Fix #1001
Resolves #1235

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 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)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

πŸ“ Checklist

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

@Hebilicious Hebilicious changed the title perf(cloudflare-module): check for asset url perf(cloudflare): check for asset url May 14, 2023
@Hebilicious Hebilicious marked this pull request as ready for review May 14, 2023 09:06
@Hebilicious
Copy link
Member Author

Hebilicious commented May 14, 2023

This was useful to debug KV :

  const list = await env.__STATIC_CONTENT.list();
  console.log("GET ASSET FROM KV", list.keys.map((k) => k.name), { publicAssetBases });

Should we add e2e tests ? Especially Nuxt ones. IE deploy nuxt project with nitro + wrangler and hit the endpoints.

@pi0
Copy link
Member

pi0 commented May 16, 2023

This is a nice enhancenment! I think we can use existing isPublicAssetURL(id) utility also to check both both direct ids and public base URLs. Any reason you haven't use that?

@Hebilicious
Copy link
Member Author

Hebilicious commented May 16, 2023

This is a nice enhancenment! I think we can use existing isPublicAssetURL(id) utility also to check both both direct ids and public base URLs. Any reason you haven't use that?

I first tried to use isPublicAssetURL, but during my testing I've noticed that it wasn't matching properly for some of the routes, so I added the extra check. Adding to isPublicAssetURL broke some other provider test iirc.

But maybe isInKV should be in the virtual file generated by the rollup plugin ?

@pi0
Copy link
Member

pi0 commented May 16, 2023

Can you please explain more what routes are not being matched?

@Hebilicious
Copy link
Member Author

Hebilicious commented May 17, 2023

Can you please explain more what routes are not being matched?

Sure ! For the Cloudflare Module for example :

Running the tests with isInPublicAssets
image

Running the tests with isInKv
image

Mabye my assumption is wrong, but because /api/hey/index.html exist, I assumed that calling /api/hey/index should hit the KV store and return the index.html

This is what's in the KV store before getAssetFromKV is called :

GET ASSET FROM KV [
  '$__MINIFLARE_SITES__$/api%2Fhello',
  '$__MINIFLARE_SITES__$/api%2Fhey%2Findex.html',
  '$__MINIFLARE_SITES__$/api%2Fparam%2Ffoo.json%2Findex.html',
  '$__MINIFLARE_SITES__$/api%2Fparam%2Fprerender1%2Findex.html',
  '$__MINIFLARE_SITES__$/api%2Fparam%2Fprerender3%2Findex.html',
  '$__MINIFLARE_SITES__$/api%2Fparam%2Fprerender4%2Findex.html',
  '$__MINIFLARE_SITES__$/build%2Ftest.txt',
  '$__MINIFLARE_SITES__$/favicon.ico',
  '$__MINIFLARE_SITES__$/icon.png',
  '$__MINIFLARE_SITES__$/prerender%2Findex.html',
  '$__MINIFLARE_SITES__$/prerender%2Findex.html.br',
  '$__MINIFLARE_SITES__$/prerender%2Findex.html.gz'
] 

@codecov
Copy link

codecov bot commented Jun 2, 2023

Codecov Report

Merging #1236 (2e14d6c) into main (414f5f5) will increase coverage by 0.30%.
The diff coverage is n/a.

❗ Current head 2e14d6c differs from pull request most recent head 3e8d3e2. Consider uploading reports for the commit 3e8d3e2 to get more accurate results

@@            Coverage Diff             @@
##             main    #1236      +/-   ##
==========================================
+ Coverage   76.58%   76.89%   +0.30%     
==========================================
  Files          71       68       -3     
  Lines        7223     6829     -394     
  Branches      718      693      -25     
==========================================
- Hits         5532     5251     -281     
+ Misses       1690     1577     -113     
  Partials        1        1              

see 23 files with indirect coverage changes

@pi0
Copy link
Member

pi0 commented Jun 7, 2023

Good point about also checking .html suffixes. Since we are checking for public asset existence in other presets, it would be much better if we could merge isInKV functionality into isPublicAssetURL. Otherwise seems really good improvements πŸ’―

Remove KV access when an API route is called.

cfm
Should return a KV hit more often.

remove log

chore: lint cloudflare changes
@Hebilicious
Copy link
Member Author

Good point about also checking .html suffixes. Since we are checking for public asset existence in other presets, it would be much better if we could merge isInKV functionality into isPublicAssetURL. Otherwise seems really good improvements πŸ’―

This was done in the later commits. Reminder to review again πŸ™πŸ½

@pi0 pi0 added pending and removed ready labels Sep 8, 2023
@pi0
Copy link
Member

pi0 commented Sep 8, 2023

Thanks for changes. As an update, i still need to revisit this issue while it is important fix and i value your efforts, i am still not convinced why we are introducing a new util like this so need to check it more in depth (i might be wrong as well!)

@Hebilicious
Copy link
Member Author

Thanks for changes. As an update, i still need to revisit this issue while it is important fix and i value your efforts, i am still not convinced why we are introducing a new util like this so need to check it more in depth (i might be wrong as well!)

Now that look more at it, this PR is more of a fix than a perf, as currently there's a bug where some matched assets aren't returned by KV, and some non existing assets are being accessed.

@zsilbi
Copy link
Contributor

zsilbi commented Feb 10, 2024

This would be a very nice improvement!
I found this as I was investigating why my noop endpoint latency is so unpredictable. (bottom)

kv_asset

@pi0 pi0 marked this pull request as draft February 27, 2024 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cloudflare module and service workers API request un-necessary KV reads Cache Control for 404 response
3 participants