Performance: File uploads and streaming
This affects page load speed and interaction responsiveness by managing how large files are sent and processed without blocking the main thread.
export const action = async ({ request }) => { const formData = await request.formData(); const file = formData.get('file'); const stream = file.stream(); await saveFileStream(stream); return new Response('Upload complete'); };
export const action = async ({ request }) => { const formData = await request.formData(); const file = formData.get('file'); const buffer = await file.arrayBuffer(); // Process entire file buffer synchronously await saveFile(buffer); return new Response('Upload complete'); };
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous full file read | Minimal | 0 | 0 | [X] Bad |
| Streaming file upload | Minimal | 0 | 0 | [OK] Good |
request.formData() method to parse form submissions, including files.request.formData() is the correct method to access them.formData.get('file') returns the value of the field, which can be a File object for uploads.File using instanceof File.export async function action({ request }) {
const formData = await request.formData();
const file = formData.get('upload');
console.log(file.name);
return null;
}formData.get('upload') returns the uploaded File object with properties like name.file.name outputs the original filename, which is "photo.png".export async function action({ request }) {
const formData = request.formData();
const file = formData.get('file');
if (file instanceof File) {
// process file
}
return null;
}request.formData() method returns a promise and must be awaited.const formData = request.formData(); lacks await, so formData is a promise, not the data.request.body.getReader() method allows reading the request body in small chunks, enabling streaming.Pass the test and this pattern moves unknown