Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(core): add cache ssr hit (#2872)

authored by

Jovi De Croock and committed by
GitHub
bd59d34a 99e4ca92

+56 -6
+5
.changeset/big-melons-leave.md
··· 1 + --- 2 + '@urql/core': patch 3 + --- 4 + 5 + Correctly mark cache-hits from the ssr-exchange
+37 -3
packages/core/src/exchanges/ssr.test.ts
··· 196 196 const data = ssr.extractData(); 197 197 expect(Object.keys(data).length).toBe(1); 198 198 expect(output).not.toHaveBeenCalled(); 199 - expect(onPush).toHaveBeenCalledWith(queryResponse); 199 + expect(onPush).toHaveBeenCalledWith({ 200 + ...queryResponse, 201 + operation: { 202 + ...queryResponse.operation, 203 + context: { 204 + ...queryResponse.operation.context, 205 + meta: { 206 + cacheOutcome: 'hit', 207 + }, 208 + }, 209 + }, 210 + }); 200 211 }); 201 212 202 213 it('resolves deferred, cached query results correctly', () => { ··· 222 233 expect(Object.keys(data).length).toBe(1); 223 234 expect(output).toHaveBeenCalledTimes(1); 224 235 expect(onPush).toHaveBeenCalledTimes(2); 225 - expect(onPush.mock.calls[1][0]).toEqual({ hasNext: true, ...queryResponse }); 236 + expect(onPush.mock.calls[1][0]).toEqual({ 237 + hasNext: true, 238 + ...queryResponse, 239 + operation: { 240 + ...queryResponse.operation, 241 + context: { 242 + ...queryResponse.operation.context, 243 + meta: { 244 + cacheOutcome: 'hit', 245 + }, 246 + }, 247 + }, 248 + }); 226 249 }); 227 250 228 251 it('deletes cached results in non-suspense environments', async () => { ··· 242 265 await Promise.resolve(); 243 266 244 267 expect(Object.keys(ssr.extractData()).length).toBe(0); 245 - expect(onPush).toHaveBeenCalledWith(queryResponse); 268 + expect(onPush).toHaveBeenCalledWith({ 269 + ...queryResponse, 270 + operation: { 271 + ...queryResponse.operation, 272 + context: { 273 + ...queryResponse.operation.context, 274 + meta: { 275 + cacheOutcome: 'hit', 276 + }, 277 + }, 278 + }, 279 + }); 246 280 247 281 // NOTE: The operation should not be duplicated 248 282 expect(output).not.toHaveBeenCalled();
+14 -3
packages/core/src/exchanges/ssr.ts
··· 1 1 import { GraphQLError } from 'graphql'; 2 2 import { pipe, share, filter, merge, map, tap } from 'wonka'; 3 3 import { Exchange, OperationResult, Operation } from '../types'; 4 - import { CombinedError } from '../utils'; 4 + import { addMetadata, CombinedError } from '../utils'; 5 5 import { reexecuteOperation } from './cache'; 6 6 7 7 export interface SerializedResult { ··· 145 145 ), 146 146 map(op => { 147 147 const serialized = data[op.key]!; 148 - const result = deserializeResult(op, serialized, includeExtensions); 148 + const cachedResult = deserializeResult( 149 + op, 150 + serialized, 151 + includeExtensions 152 + ); 153 + 149 154 if (staleWhileRevalidate && !revalidated.has(op.key)) { 150 - result.stale = true; 155 + cachedResult.stale = true; 151 156 revalidated.add(op.key); 152 157 reexecuteOperation(client, op); 153 158 } 154 159 160 + const result: OperationResult = { 161 + ...cachedResult, 162 + operation: addMetadata(op, { 163 + cacheOutcome: 'hit', 164 + }), 165 + }; 155 166 return result; 156 167 }) 157 168 );