Skip to content

Commit a70491a

Browse files
committed
support encoding and decoding of invalid Date in turbo-stream
1 parent 5c8ccd0 commit a70491a

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

integration/single-fetch-test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,24 @@ const files = {
170170
)
171171
}
172172
`,
173+
174+
"app/routes/invalid-date.tsx": js`
175+
import { useLoaderData, data } from "react-router";
176+
177+
export function loader({ request }) {
178+
return data({ invalidDate: new Date("invalid") });
179+
}
180+
181+
export default function InvalidDate() {
182+
let data = useLoaderData();
183+
return (
184+
<>
185+
<h1 id="heading">Invalid Date</h1>
186+
<p id="date">{data.invalidDate.toISOString()}</p>
187+
</>
188+
)
189+
}
190+
`
173191
};
174192

175193
test.describe("single-fetch", () => {
@@ -216,6 +234,23 @@ test.describe("single-fetch", () => {
216234
},
217235
},
218236
});
237+
238+
res = await fixture.requestSingleFetchData("/invalid-date.data");
239+
expect(res.data).toEqual({
240+
root: {
241+
data: {
242+
message: "ROOT",
243+
},
244+
},
245+
"routes/invalid-date": {
246+
data: {
247+
invalidDate: expect.any(Date),
248+
},
249+
},
250+
})
251+
252+
let date = (res.data as { ["routes/invalid-date"]: { data: { invalidDate: Date } } })["routes/invalid-date"].data.invalidDate
253+
expect(isNaN(date.getTime())).toBe(true)
219254
});
220255

221256
test("loads proper errors on single fetch loader requests", async () => {

packages/react-router/__tests__/vendor/turbo-stream-test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ test("should encode and decode Date", async () => {
5050
expect(output).toEqual(input);
5151
});
5252

53+
test("should encode and decode invalid Date", async () => {
54+
const input = new Date("invalid");
55+
const output = await quickDecode(encode(input));
56+
expect(isNaN(input.getTime())).toBe(true);
57+
expect(isNaN(output.getTime())).toBe(true);
58+
});
59+
5360
test("should encode and decode NaN", async () => {
5461
const input = NaN;
5562
const output = await quickDecode(encode(input));

packages/react-router/vendor/turbo-stream-v2/flatten.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ function stringify(this: ThisEncode, input: unknown, index: number) {
106106
(i in input ? flatten.call(this, input[i]) : HOLE);
107107
str[index] = `${result}]`;
108108
} else if (input instanceof Date) {
109-
str[index] = `["${TYPE_DATE}",${input.getTime()}]`;
109+
const dateTime = input.getTime();
110+
str[index] = `["${TYPE_DATE}",${Number.isNaN(dateTime)? JSON.stringify("invalid") : dateTime}]`;
110111
} else if (input instanceof URL) {
111112
str[index] = `["${TYPE_URL}",${JSON.stringify(input.href)}]`;
112113
} else if (input instanceof RegExp) {

0 commit comments

Comments
 (0)