Skip to content

Commit

Permalink
close on unsubscribe, catch errors
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Nov 14, 2024
1 parent 09c1be0 commit 1554899
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
17 changes: 17 additions & 0 deletions integration-test/nextjs/src/app/rsc/dynamic/PreloadQuery/error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use client"; // Error boundaries must be Client Components

export default function Error({
error,
reset,
}: {
error: Error & { digest?: string };
reset: () => void;
}) {
return (
<>
<h2>Encountered an error:</h2>
<pre>{error.message}</pre>
<button onClick={() => reset()}>Try again</button>
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { useQueryRefHandlers, useReadQuery } from "@apollo/client";
import { DynamicProductResult } from "../shared";
import { TransportedQueryRef } from "@apollo/experimental-nextjs-app-support/ssr";
import { TransportedQueryRef } from "@apollo/experimental-nextjs-app-support";

export function ClientChild({
queryRef,
Expand Down
1 change: 1 addition & 0 deletions packages/client-react-streaming/src/ReadableStreamLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export const TeeToReadableStreamLink = new ApolloLink((operation, forward) => {
);

return () => {
controller.close();
subscription.unsubscribe();
};
});
Expand Down
30 changes: 17 additions & 13 deletions packages/client-react-streaming/src/transportedQueryRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,23 @@ export function createTransportedQueryPreloader(

// Instead of creating the queryRef, we kick off a query that will feed the network response
// into our custom event stream.
client.query({
query,
...options,
// ensure that this query makes it to the network
fetchPolicy: "network-only",
context: skipDataTransport(
teeToReadableStream(__injectIntoStream!, {
...options?.context,
// we want to do this even if the query is already running for another reason
queryDeduplication: false,
})
),
});
client
.query({
query,
...options,
// ensure that this query makes it to the network
fetchPolicy: "network-only",
context: skipDataTransport(
teeToReadableStream(__injectIntoStream!, {
...options?.context,
// we want to do this even if the query is already running for another reason
queryDeduplication: false,
})
),
})
.catch(() => {
/* we want to avoid any floating promise rejections */
});

return createTransportedQueryRef<any, any>(
query,
Expand Down

0 comments on commit 1554899

Please sign in to comment.