-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove single fetch response stub, add headers (#9769)
- Loading branch information
1 parent
e171af7
commit b163e06
Showing
23 changed files
with
587 additions
and
1,737 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
"@remix-run/cloudflare": minor | ||
"@remix-run/deno": minor | ||
"@remix-run/node": minor | ||
"@remix-run/react": minor | ||
"@remix-run/server-runtime": minor | ||
--- | ||
|
||
Add a new `unstable_data()` API for usage with Remix Single Fetch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
"@remix-run/server-runtime": minor | ||
"@remix-run/react": minor | ||
--- | ||
|
||
Single Fetch: Remove `responseStub` in favor of `headers` | ||
|
||
* Background | ||
* The original Single Fetch approach was based on an assumption that an eventual `middleware` implementation would require something like `ResponseStub` so users could mutate `status`/`headers` in `middleware` before/after handlers as well as during handlers | ||
* We wanted to align how `headers` got merged between document and data requests | ||
* So we made document requests also use `ResponseStub` and removed the usage of `headers` in Single Fetch | ||
* The realization/alignment between Michael and Ryan on the recent [roadmap planning](https://www.youtube.com/watch?v=f5z_axCofW0) made us realize that the original assumption was incorrect | ||
* `middleware` won't need a stub - users can just mutate the `Response` they get from `await next()` directly | ||
* With that gone, and still wanting to align how `headers` get merged, it makes more sense to stick with the current `headers` API and apply that to Single Fetch and avoid introducing a totally new thing in `RepsonseStub` (that always felt a bit awkward to work with anyway) | ||
|
||
* With this change: | ||
* You are encouraged to stop returning `Response` instances in favor of returning raw data from loaders and actions: | ||
* ~~`return json({ data: whatever });`~~ | ||
* `return { data: whatever };` | ||
* In most cases, you can remove your `json()` and `defer()` calls in favor of returning raw data if they weren't setting custom `status`/`headers` | ||
* We will be removing both `json` and `defer` in the next major version, but both _should_ still work in Single Fetch in v2 to allow for incremental adoption of the new behavior | ||
* If you need custom `status`/`headers`: | ||
* We've added a new `unstable_data({...}, responseInit)` utility that will let you send back `status`/`headers` alongside your raw data without having to encode it into a `Response` | ||
* The `headers()` function will let you control header merging for both document and data requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
title: unstable_data | ||
toc: false | ||
--- | ||
|
||
# `unstable_data` | ||
|
||
This is a utility for use with [Single Fetch][single-fetch] to return raw data accompanied with a status code or custom response headers. This avoids the need to serialize your data into a `Response` instance to provide custom status/headers. This is generally a replacement for `loader`/`action` functions that used [`json`][json] or [`defer`][defer] prior to Single Fetch. | ||
|
||
```tsx | ||
import { unstable_data as data } from "@remix-run/node"; // or cloudflare/deno | ||
|
||
export const loader = async () => { | ||
// So you can write this: | ||
return data( | ||
{ not: "coffee" }, | ||
{ | ||
status: 418, | ||
headers: { | ||
"Cache-Control": "no-store", | ||
}, | ||
} | ||
); | ||
}; | ||
``` | ||
|
||
You should _not_ be using this function if you don't need to return custom status/headers - in that case, just return the data directly: | ||
|
||
```tsx | ||
export const loader = async () => { | ||
// ❌ Bad | ||
return data({ not: "coffee" }); | ||
|
||
// ✅ Good | ||
return { not: "coffee" }; | ||
}; | ||
``` | ||
|
||
[single-fetch]: ../guides/single-fetch | ||
[json]: ./json | ||
[defer]: ./defer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.