next.js
76f99765 - docs: add file uploads and file serving to backend-for-frontend guide (#100362)

Commit
1 day ago
docs: add file uploads and file serving to backend-for-frontend guide (#100362) ### What? - Adds a new `## Handling files` section to `docs/01-app/02-guides/backend-for-frontend.mdx` covering storage considerations, direct uploads to storage, streaming in Route Handlers, small uploads in Server Actions, and streaming file downloads. - Adds a note in the existing `## Proxy` section regarding the 10MB request body buffering limit and how to exclude routes using `matcher`. ### Why? - Consolidates file handling and storage patterns into a dedicated section within the backend guide as suggested by @icyJoseph. - Clarifies where files should live: serverless instances do not share disk state, and files placed in `public/` at runtime are not served until the server restarts. - Clarifies how Proxy body buffering interacts with large payloads. ### How? Tested on Windows, Node v26.11.0, in production mode with Turbopack: - **File serving:** Streamed a 5MB file with `Readable.toWeb()` and verified byte-for-byte parity. Path traversal attempts such as `..%2fpackage.json` return 404 because files are looked up by a stored upload ID and the disk path is built from that ID. - **Runtime `public/` check:** Verified in `next start` that files created in `public/` after server startup return 404, while pre-existing files return 200. - **Interrupted uploads:** Aborting an upload midway removes the partial file. - **Memory:** For a 300MB file, streaming increased RSS by 69MB, while `formData()` increased it by 895MB. - **Proxy:** With a passthrough `proxy.ts`, sending 300MB resulted in the Route Handler receiving only 10,485,760 bytes (status 200). - **Matcher:** After excluding the upload route with `matcher`, the full 300MB arrived without truncation. - **Server Action:** A 1KB file succeeded, and a 2MB file failed with "Body exceeded 1 MB limit". - **Checks:** Checked all links in the guide against the docs tree, and the TypeScript check passes for all code blocks. - **Authorization & ownership:** Tested that unauthenticated requests get 401, accessing another user's file returns 404, and guessed IDs return 404. - **Direct-to-storage:** Verified the presigned URL flow against a mock storage service (signature check, expiry, and CORS). **Note for reviewers:** The direct-to-storage section has no provider SDK code, to stay provider-agnostic. I verified the flow against a small mock storage service (signature check, expiry, and CORS), not against a real provider. --------- Co-authored-by: Joseph <sephxd1234@gmail.com> Co-authored-by: Joseph <joseph.chamochumbi@vercel.com>
Author
Parents
Loading