Skip to content

feat: add support for read in edge environments #13859

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

vnphanquang
Copy link
Contributor

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:

  • 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
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • 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

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

Copy link

changeset-bot bot commented Jun 5, 2025

🦋 Changeset detected

Latest commit: de885af

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

This PR includes changesets to release 2 packages
Name Type
@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

@svelte-docs-bot
Copy link

@vnphanquang
Copy link
Contributor Author

vnphanquang commented Jun 5, 2025

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

Copy link
Member

Choose a reason for hiding this comment

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

We could save some space by testing with a .txt file instead. The .txt file has to be imported with the ?url query so that Vite doesn't inline it. For example:

import textFile from './myFile.txt?url';

Copy link
Contributor Author

@vnphanquang vnphanquang Jun 6, 2025

Choose a reason for hiding this comment

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

Thanks, happy to adjust. I also wanted to test the content-type header in the response. My expectation was that read should automatically return the correct mime type given the asset, without the need to do so explicitly in the caller. Do you know if that's always the case or worth testing here? I guess with a txt we can still test with "text/plain".

Let me know

Copy link
Member

@eltigerchino eltigerchino Jun 6, 2025

Choose a reason for hiding this comment

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

Oh I'm not actually sure. I would think that it doesn't automatically return the correct mime type but I'd have to check if that's something Cloudflare is doing or if it happens in Node as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just gave it a quick test with adapter-node. content-type is correctly returned from the built server, but not during development. Here's a test repo if you want to take a look.

Anyway, let's simplify to txt for now. Having correct mime is nice but it seems out of scope for this particular PR.

Copy link
Member

@eltigerchino eltigerchino Jun 6, 2025

Choose a reason for hiding this comment

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

It's probably because during development, read uses a different implementation (it assumes we're always running in a Node.js environment). I could be wrong about this though and maybe it's something else. Are you using vite preview or node build?

read: (file) => {
if (file in manifest._.server_assets) {
return fs.readFileSync(from_fs(file));
}
return fs.readFileSync(path.join(svelte_config.kit.files.assets, file));
},

read: (file) => {
if (file in manifest._.server_assets) {
return fs.readFileSync(join(dir, file));
}
return fs.readFileSync(join(svelte_config.kit.files.assets, file));
},

read: (file) => createReadableStream(`${asset_dir}/${file}`)

This could be an issue once dev runs things in a Cloudflare or Netlify environment etc. based on the adapter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I ran pnpm build followed by node build/index.js

Copy link
Member

@eltigerchino eltigerchino Jun 6, 2025

Choose a reason for hiding this comment

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

Ah alright. That definitely uses a different implementation, although I don't see how createReadableStream and readFileSync would differ much. Maybe it's how files are served in the Node adapter itself (which is different from vite dev and vite preview).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I have no clue either. Something to investigate more if I can find some time. For now I'll try to get this PR done first. I guess users can always explicitly set content-type if they find it inconsistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved to txt via 9a525d6.

@eltigerchino
Copy link
Member

eltigerchino commented Jun 6, 2025

Thanks for this PR! Is there any chance you could continue the work from #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.

@vnphanquang
Copy link
Contributor Author

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

@vnphanquang vnphanquang force-pushed the 12446 branch 2 times, most recently from dc18769 to 6da55ed Compare June 6, 2025 06:20
@vnphanquang
Copy link
Contributor Author

vnphanquang commented Jun 6, 2025

@eltigerchino extracted into a streamFileContent function via 6da55ed. 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 vnphanquang requested a review from eltigerchino June 6, 2025 06:36
@eltigerchino
Copy link
Member

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.

@eltigerchino eltigerchino changed the title feat: add support for server read in cloudflare adapter feat: add support for read in edge environments Jun 6, 2025
@eltigerchino eltigerchino added adapters - general Support for functionality general to all adapters and removed pkg:adapter-cloudflare labels Jun 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
adapters - general Support for functionality general to all adapters
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants