Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
alanko0511 committed Sep 13, 2024
1 parent 981af30 commit f9dda49
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/api-client-core/src/GadgetConnection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import type { DefinitionNode, DirectiveNode, OperationDefinitionNode } from "@0no-co/graphql.web";
import type { ClientOptions, RequestPolicy } from "@urql/core";
import { Client, cacheExchange, fetchExchange, subscriptionExchange } from "@urql/core";
import type { ClientOptions, RequestPolicy, SSRExchange } from "@urql/core";
import { Client, cacheExchange, fetchExchange, ssrExchange, subscriptionExchange } from "@urql/core";
import type { ExecutionResult } from "graphql";
import type { Sink, Client as SubscriptionClient, ClientOptions as SubscriptionClientOptions } from "graphql-ws";
import { CloseCode, createClient as createSubscriptionClient } from "graphql-ws";
Expand Down Expand Up @@ -108,6 +108,8 @@ export class GadgetConnection {
private requestPolicy: RequestPolicy;
createSubscriptionClient: typeof createSubscriptionClient;

ssrExchange?: SSRExchange;

constructor(readonly options: GadgetConnectionOptions) {
if (!options.endpoint) throw new Error("Must provide an `endpoint` option for a GadgetConnection to connect to");
this.endpoint = options.endpoint;
Expand Down Expand Up @@ -363,7 +365,14 @@ export class GadgetConnection {
}

private newBaseClient() {
const exchanges = [...this.exchanges.beforeAll, operationNameExchange, urlParamExchange];
this.ssrExchange = ssrExchange({
isClient: typeof window !== "undefined",
initialState: typeof window !== "undefined" ? (window as any).__URQL_DATA__ : undefined,
staleWhileRevalidate: true,
});
this.ssrExchange.extractData();

const exchanges = [...this.exchanges.beforeAll, operationNameExchange, urlParamExchange, this.ssrExchange];

// apply urql's default caching behaviour when client side (but skip it server side)
if (typeof window != "undefined") {
Expand Down

0 comments on commit f9dda49

Please sign in to comment.