-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: de885af The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
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 |
There was a problem hiding this comment.
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';
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
?
kit/packages/kit/src/exports/vite/dev/index.js
Lines 550 to 556 in 6261a87
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)); | |
}, |
kit/packages/kit/src/exports/vite/preview/index.js
Lines 203 to 209 in 6261a87
read: (file) => { | |
if (file in manifest._.server_assets) { | |
return fs.readFileSync(join(dir, file)); | |
} | |
return fs.readFileSync(join(svelte_config.kit.files.assets, file)); | |
}, |
kit/packages/adapter-node/src/handler.js
Line 39 in 6261a87
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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
I re-ran the test in CI and it passes. Probably just a flaky test that failed. |
Ah yes. Sorry I missed Rich's PR in my search. Will look into it. Thanks for the input! |
dc18769
to
6da55ed
Compare
@eltigerchino extracted into a
Also, I haven't added 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 |
…t util for server read
Thank you!
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. |
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:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. Changesets that add features should beminor
and those that fix bugs should bepatch
. Please prefix changeset messages withfeat:
,fix:
, orchore:
.Edits