Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(core) - Fix ssrExchange.restoreData adding invalidated results #1776

Merged
merged 3 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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/cuddly-readers-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/core': patch
---

Prevent `ssrExchange().restoreData()` from adding results to the exchange that have already been invalidated. This may happen when `restoreData()` is called repeatedly, e.g. per page. When a prior run has already invalidated an SSR result then the result is 'migrated' to the user's `cacheExchange`, which means that `restoreData()` should never attempt to re-add it again.
33 changes: 32 additions & 1 deletion packages/core/src/exchanges/ssr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,40 @@ it('deletes cached results in non-suspense environments', async () => {

await Promise.resolve();

expect(Object.keys(ssr.extractData()).length).toBe(0);
expect(ssr.extractData()[queryOperation.key]).toBe(null);
expect(onPush).toHaveBeenCalledWith(queryResponse);

// NOTE: The operation should not be duplicated
expect(output).not.toHaveBeenCalled();
});

it('never allows restoration of invalidated results', async () => {
client.suspense = false;

const onPush = jest.fn();
const initialState = { [queryOperation.key]: serializedQueryResponse as any };

const ssr = ssrExchange({
isClient: true,
initialState: { ...initialState },
});

const { source: ops$, next } = input;
const exchange = ssr(exchangeInput)(ops$);

pipe(exchange, forEach(onPush));
next(queryOperation);

await Promise.resolve();

expect(ssr.extractData()[queryOperation.key]).toBe(null);
expect(onPush).toHaveBeenCalledTimes(1);
expect(output).not.toHaveBeenCalled();

ssr.restoreData(initialState);
expect(ssr.extractData()[queryOperation.key]).toBe(null);

next(queryOperation);
expect(onPush).toHaveBeenCalledTimes(2);
expect(output).toHaveBeenCalledTimes(1);
});
20 changes: 15 additions & 5 deletions packages/core/src/exchanges/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface SerializedResult {
}

export interface SSRData {
[key: string]: SerializedResult;
[key: string]: SerializedResult | null;
}

export interface SSRExchangeParams {
Expand Down Expand Up @@ -101,13 +101,15 @@ export const ssrExchange = (params?: SSRExchangeParams): SSRExchange => {
if (invalidateQueue.length === 1) {
Promise.resolve().then(() => {
let key: number | void;
while ((key = invalidateQueue.shift())) delete data[key];
while ((key = invalidateQueue.shift())) {
data[key] = null;
}
});
}
};

const isCached = (operation: Operation) => {
return !shouldSkip(operation) && data[operation.key] !== undefined;
return !shouldSkip(operation) && data[operation.key] != null;
};

// The SSR Exchange is a temporary cache that can populate results into data for suspense
Expand All @@ -134,7 +136,7 @@ export const ssrExchange = (params?: SSRExchangeParams): SSRExchange => {
sharedOps$,
filter(op => isCached(op)),
map(op => {
const serialized = data[op.key];
const serialized = data[op.key]!;
return deserializeResult(op, serialized);
})
);
Expand All @@ -159,7 +161,15 @@ export const ssrExchange = (params?: SSRExchangeParams): SSRExchange => {
return merge([forwardedOps$, cachedOps$]);
};

ssr.restoreData = (restore: SSRData) => Object.assign(data, restore);
ssr.restoreData = (restore: SSRData) => {
for (const key in restore) {
// We only restore data that hasn't been previously invalidated
if (data[key] !== null) {
data[key] = restore[key];
}
}
};

ssr.extractData = () => Object.assign({}, data);

if (params && params.initialState) {
Expand Down