Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eight-cherries-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

Support invalid `Date` in `turbo-stream` v2 fork
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@
- rtzll
- rubeonline
- ruidi-huang
- rururux
- ryanflorence
- ryanhiebert
- saengmotmi
Expand Down
35 changes: 35 additions & 0 deletions integration/single-fetch-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,24 @@ const files = {
)
}
`,

"app/routes/invalid-date.tsx": js`
import { useLoaderData, data } from "react-router";

export function loader({ request }) {
return data({ invalidDate: new Date("invalid") });
}

export default function InvalidDate() {
let data = useLoaderData();
return (
<>
<h1 id="heading">Invalid Date</h1>
<p id="date">{data.invalidDate.toISOString()}</p>
</>
)
}
`
};

test.describe("single-fetch", () => {
Expand Down Expand Up @@ -216,6 +234,23 @@ test.describe("single-fetch", () => {
},
},
});

res = await fixture.requestSingleFetchData("/invalid-date.data");
expect(res.data).toEqual({
root: {
data: {
message: "ROOT",
},
},
"routes/invalid-date": {
data: {
invalidDate: expect.any(Date),
},
},
});

let date = (res.data as { ["routes/invalid-date"]: { data: { invalidDate: Date } } })["routes/invalid-date"].data.invalidDate;
expect(isNaN(date.getTime())).toBe(true);
});

test("loads proper errors on single fetch loader requests", async () => {
Expand Down
7 changes: 7 additions & 0 deletions packages/react-router/__tests__/vendor/turbo-stream-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ test("should encode and decode Date", async () => {
expect(output).toEqual(input);
});

test("should encode and decode invalid Date", async () => {
const input = new Date("invalid");
const output = await quickDecode(encode(input));
expect(isNaN(input.getTime())).toBe(true);
expect(isNaN(output.getTime())).toBe(true);
});

test("should encode and decode NaN", async () => {
const input = NaN;
const output = await quickDecode(encode(input));
Expand Down
3 changes: 2 additions & 1 deletion packages/react-router/vendor/turbo-stream-v2/flatten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ function stringify(this: ThisEncode, input: unknown, index: number) {
(i in input ? flatten.call(this, input[i]) : HOLE);
str[index] = `${result}]`;
} else if (input instanceof Date) {
str[index] = `["${TYPE_DATE}",${input.getTime()}]`;
const dateTime = input.getTime();
str[index] = `["${TYPE_DATE}",${Number.isNaN(dateTime)? JSON.stringify("invalid") : dateTime}]`;
} else if (input instanceof URL) {
str[index] = `["${TYPE_URL}",${JSON.stringify(input.href)}]`;
} else if (input instanceof RegExp) {
Expand Down