kit icon indicating copy to clipboard operation
kit copied to clipboard

feat: add support for read in edge environments

Open vnphanquang opened this issue 6 months ago • 7 comments

This PR helps with the cloudflare part of #12446. Added a test case and I also tested in a project bootstrapped with create cloudflare@latest. Hope i didn't miss anything 🤞.

Thanks all.


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • [x] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • [x] This message body should clearly illustrate what problems it solves.
  • [x] Ideally, include a test that fails without this PR but passes with it.

Tests

  • [x] Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • [x] If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • [x] Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

vnphanquang avatar Jun 05 '25 05:06 vnphanquang

🦋 Changeset detected

Latest commit: 3f0328eb2f323a3d8fde9e1ff7666bd622284ccc

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

This PR includes changesets to release 4 packages
Name Type
@sveltejs/adapter-netlify Minor
@sveltejs/adapter-vercel Minor
@sveltejs/adapter-cloudflare Minor
@sveltejs/kit Minor

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

changeset-bot[bot] avatar Jun 05 '25 05:06 changeset-bot[bot]

Preview: https://svelte-dev-git-preview-kit-13859-svelte.vercel.app/

svelte-docs-bot[bot] avatar Jun 05 '25 05:06 svelte-docs-bot[bot]

Not sure why that particular firefox CI test failed but it did pass on my local machine (pnpm test:cross-platform:build). Let me know, thanks!

image

vnphanquang avatar Jun 05 '25 06:06 vnphanquang

Thanks for this PR! Is there any chance you could continue the work from https://github.com/sveltejs/kit/pull/11674 so that it's implemented in the Netlify and Vercel adapters too? I think if the reusable fetchFile function could allow passing a fetch function to it, we could pass env.ASSETS.fetch in the case of the Cloudflare adapter and it would just work.

Not sure why that particular firefox CI test failed but it did pass on my local machine (pnpm test:cross-platform:build). Let me know, thanks!

I re-ran the test in CI and it passes. Probably just a flaky test that failed.

teemingc avatar Jun 06 '25 01:06 teemingc

Ah yes. Sorry I missed Rich's PR in my search. Will look into it. Thanks for the input!

vnphanquang avatar Jun 06 '25 01:06 vnphanquang

@eltigerchino extracted into a streamFileContent function via https://github.com/sveltejs/kit/pull/13859/commits/6da55ed8bffa335290281405b2fa79cf61aa5bcd. A couple of things:

  • added some unit tests; I don't think they are strictly necessary but nice to have anyway,
  • exported this util via @sveltejs/kit/adapter, as I think kit is already a peer-dependency for all adapters anyway,
  • settled with this name rather than fetchFile since I feel like fetch would indicate a returned Response.

Also, I haven't added read for netlify & vercel yet, since edge environment can't be easily tested locally for them as far as I know (vercel requires connecting to a cloud project, and netlify docs says edge functions don't work locally).

I can spawn up some projects in netlify & vercel and do some manual tests, but I feel like i shouldn't block progress here. How about I do that in a separate PR?

Thanks

vnphanquang avatar Jun 06 '25 06:06 vnphanquang

Thank you!

Also, I haven't added read for netlify & vercel yet, since edge environment can't be easily tested locally for them as far as I know (vercel requires connecting to a cloud project, and netlify docs says edge functions don't work locally).

I can spawn up some projects in netlify & vercel and do some manual tests, but I feel like i shouldn't block progress here. How about I do that in a separate PR?

Yeah, manual testing for those are fine. There is no local environment for Vercel and like you said, Netlify's edge doesn't work locally.

teemingc avatar Jun 06 '25 06:06 teemingc

Can this be prioritized? It's very cumbersome to use static assets with Cloudflare with unexpected outcome. Vercel and other can be added later

vladshcherbin avatar Jun 23 '25 10:06 vladshcherbin

I've added support to the Netlify and Vercel adapters. Also added a test suite for Netlify now that the local dev environment supports edge functions.

teemingc avatar Jun 25 '25 01:06 teemingc

I've added support to the Netlify and Vercel adapters. Also added a test suite for Netlify now that the local dev environment supports edge functions.

Thanks @eltigerchino. Apologies I couldn't find time to follow through with Netlify & Vercel. Appreciate you taking on the responsibility!

vnphanquang avatar Jun 25 '25 03:06 vnphanquang

@eltigerchino I found a solution that avoids adding a new export: tweak SvelteKit's read implementation to handle async values. Could you take another look?

Thanks! That's a really nice solution.

teemingc avatar Jul 16 '25 03:07 teemingc

I'm happy we have tests for the Netlify adapter now. However, adding netlify-cli pulls in almost 1,000 dependencies into the project: http://npmgraph.js.org/?q=netlify-cli. Is there some way we can serve the built files without netlify-cli? With all the recent supply chain attacks going on, I don't love the idea of having all these extra dependencies...

benmccann avatar Jul 24 '25 17:07 benmccann

I think eventually we wanna turn these into "deploy to prod, do playwright stuff against prod url" tests. Slower but more reliable and fully replicates the environment

dummdidumm avatar Jul 24 '25 18:07 dummdidumm

In the meantime I wonder if we could just invoke sirv since the only thing we do is call netlify serve. I'm not sure what all netlify serve does and it's probably not exactly the same, but it would be a lot lighter.

benmccann avatar Jul 24 '25 23:07 benmccann

In the meantime I wonder if we could just invoke sirv since the only thing we do is call netlify serve. I'm not sure what all netlify serve does and it's probably not exactly the same, but it would be a lot lighter.

I'm not sure how sirv would work with the Netlify build output. At least in https://github.com/sveltejs/kit/pull/12296 the CLI's serve is important for testing the split functions functionality with the Netlify middleware.

The Environment API integration is still far off, otherwise we could use the Netlify Vite plugin instead. I think Simon's idea might be a lot better and something we can implement sooner.

cc: @serhalp

teemingc avatar Jul 25 '25 03:07 teemingc

I shared another idea here: https://github.com/e18e/ecosystem-issues/issues/156#issuecomment-3160886873

benmccann avatar Aug 06 '25 16:08 benmccann