@graphcommerce/graphql 11.0.0-canary.58 → 11.0.0-canary.60
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +35 -0
- package/index.ts +1 -0
- package/package.json +7 -7
- package/utils/renewSignal.test.ts +31 -0
- package/utils/renewSignal.ts +7 -23
- package/utils/renewableSsrClient.test.ts +67 -0
- package/utils/renewableSsrClient.ts +30 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,40 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## 11.0.0-canary.60
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#2719](https://github.com/graphcommerce-org/graphcommerce/pull/2719) [`900688b`](https://github.com/graphcommerce-org/graphcommerce/commit/900688b06ebb4096212d8d906ba605ffe5dda19d) - Renew the SSR Apollo client at the first query after a publish.
|
|
8
|
+
|
|
9
|
+
`graphqlSsrClient()` decided with `renewSignal()` if its client was older than the renew signal. That function is synchronous and returned the value of the previous read, so the first `getStaticProps` after a publish kept the old client and rendered from the cache of before the publish. With on-demand revalidation, that render was often the revalidated page.
|
|
10
|
+
|
|
11
|
+
The new `renewableSsrClient()` makes the check in `query()`, which can wait for the signal. After a publish, the queries go to a new client. `refreshRenewSignal()` now gives callers that start during a read the result of that read. `renewSignal()` is removed.
|
|
12
|
+
|
|
13
|
+
Change `lib/graphql/graphqlSsrClient.ts` in your project:
|
|
14
|
+
|
|
15
|
+
```diff
|
|
16
|
+
-const ssrClient: {
|
|
17
|
+
- [locale: string]: { instancedAt: number; client: ApolloClient }
|
|
18
|
+
-} = {}
|
|
19
|
+
+const ssrClient: { [locale: string]: ApolloClient } = {}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
````diff
|
|
23
|
+
- const signal = renewSignal()
|
|
24
|
+
- const existing = ssrClient[locale]
|
|
25
|
+
- if (existing && signal !== undefined && existing.instancedAt < signal) delete ssrClient[locale]
|
|
26
|
+
-
|
|
27
|
+
- if (!ssrClient[locale])
|
|
28
|
+
- ssrClient[locale] = { instancedAt: Date.now(), client: client(context, 'no-cache') }
|
|
29
|
+
-
|
|
30
|
+
- return ssrClient[locale].client
|
|
31
|
+
+ ssrClient[locale] ??= renewableSsrClient(() => client(context, 'no-cache'))
|
|
32
|
+
+ return ssrClient[locale]
|
|
33
|
+
``` ([@Giovanni-Schroevers](https://github.com/Giovanni-Schroevers))
|
|
34
|
+
````
|
|
35
|
+
|
|
36
|
+
## 11.0.0-canary.59
|
|
37
|
+
|
|
3
38
|
## 11.0.0-canary.58
|
|
4
39
|
|
|
5
40
|
## 11.0.0-canary.57
|
package/index.ts
CHANGED
|
@@ -7,6 +7,7 @@ export * from './generated/types'
|
|
|
7
7
|
export * from './config'
|
|
8
8
|
export * from './utils/getPreviewData'
|
|
9
9
|
export * from './utils/cachePolicy'
|
|
10
|
+
export * from './utils/renewableSsrClient'
|
|
10
11
|
export * from './utils/renewSignal'
|
|
11
12
|
export * from './components/PrivateQueryMask/PrivateQueryMask'
|
|
12
13
|
export * from './hooks/usePrivateQueryContext'
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@graphcommerce/graphql",
|
|
3
3
|
"homepage": "https://www.graphcommerce.org/",
|
|
4
4
|
"repository": "github:graphcommerce-org/graphcommerce",
|
|
5
|
-
"version": "11.0.0-canary.
|
|
5
|
+
"version": "11.0.0-canary.60",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"main": "index.ts",
|
|
8
8
|
"prettier": "@graphcommerce/prettier-config-pwa",
|
|
@@ -28,12 +28,12 @@
|
|
|
28
28
|
"rxjs": "^7.8.2"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"@graphcommerce/eslint-config-pwa": "^11.0.0-canary.
|
|
32
|
-
"@graphcommerce/graphql-codegen-near-operation-file": "11.0.0-canary.
|
|
33
|
-
"@graphcommerce/graphql-codegen-relay-optimizer-plugin": "11.0.0-canary.
|
|
34
|
-
"@graphcommerce/next-config": "^11.0.0-canary.
|
|
35
|
-
"@graphcommerce/prettier-config-pwa": "^11.0.0-canary.
|
|
36
|
-
"@graphcommerce/typescript-config-pwa": "^11.0.0-canary.
|
|
31
|
+
"@graphcommerce/eslint-config-pwa": "^11.0.0-canary.60",
|
|
32
|
+
"@graphcommerce/graphql-codegen-near-operation-file": "11.0.0-canary.60",
|
|
33
|
+
"@graphcommerce/graphql-codegen-relay-optimizer-plugin": "11.0.0-canary.60",
|
|
34
|
+
"@graphcommerce/next-config": "^11.0.0-canary.60",
|
|
35
|
+
"@graphcommerce/prettier-config-pwa": "^11.0.0-canary.60",
|
|
36
|
+
"@graphcommerce/typescript-config-pwa": "^11.0.0-canary.60",
|
|
37
37
|
"@graphql-mesh/plugin-http-details-extensions": "*",
|
|
38
38
|
"react": "^19.2.0",
|
|
39
39
|
"react-dom": "^19.2.0"
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { beforeEach, expect, it, vi } from 'vitest'
|
|
2
|
+
|
|
3
|
+
let signal: typeof import('./renewSignal')
|
|
4
|
+
|
|
5
|
+
beforeEach(async () => {
|
|
6
|
+
vi.useFakeTimers()
|
|
7
|
+
vi.setSystemTime(new Date('2026-01-01T00:00:00Z'))
|
|
8
|
+
|
|
9
|
+
const entries = new Map<string, unknown>()
|
|
10
|
+
vi.stubGlobal('__incrementalCache', {
|
|
11
|
+
get: async (key: string) => (entries.has(key) ? { value: entries.get(key) } : null),
|
|
12
|
+
set: async (key: string, data: unknown) => {
|
|
13
|
+
entries.set(key, data)
|
|
14
|
+
},
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
vi.resetModules()
|
|
18
|
+
signal = await import('./renewSignal')
|
|
19
|
+
|
|
20
|
+
// The first read finds no signal. The next read is possible after the poll interval.
|
|
21
|
+
await signal.refreshRenewSignal()
|
|
22
|
+
vi.advanceTimersByTime(5000)
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('gives readers that start at the same moment the published value', async () => {
|
|
26
|
+
await signal.publishRenewSignal()
|
|
27
|
+
|
|
28
|
+
const values = await Promise.all([signal.refreshRenewSignal(), signal.refreshRenewSignal()])
|
|
29
|
+
|
|
30
|
+
expect(values).toEqual([Date.now(), Date.now()])
|
|
31
|
+
})
|
package/utils/renewSignal.ts
CHANGED
|
@@ -92,30 +92,14 @@ async function readRenewSignal(): Promise<number | undefined> {
|
|
|
92
92
|
* once, and they should not each open their own.
|
|
93
93
|
*/
|
|
94
94
|
export async function refreshRenewSignal(): Promise<number | undefined> {
|
|
95
|
+
if (inFlight) return inFlight
|
|
96
|
+
|
|
95
97
|
const now = Date.now()
|
|
96
98
|
if (signalValue !== undefined && now - signalReadAt < RENEW_SIGNAL_POLL_MS) return signalValue
|
|
97
|
-
if (!inFlight) {
|
|
98
|
-
signalReadAt = now
|
|
99
|
-
inFlight = readRenewSignal().finally(() => {
|
|
100
|
-
inFlight = undefined
|
|
101
|
-
})
|
|
102
|
-
}
|
|
103
|
-
return inFlight
|
|
104
|
-
}
|
|
105
99
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
* awaited would mean making every consumer async. It gets the value from the previous refresh and
|
|
112
|
-
* schedules the next one, bounding staleness at one poll interval plus one call.
|
|
113
|
-
*
|
|
114
|
-
* `undefined` must not be read as "nothing published" — on a fresh pod, which is every pod right
|
|
115
|
-
* after a deploy, that would silently skip the first invalidation. Callers treat it as unknown and
|
|
116
|
-
* fall back to their own failsafe.
|
|
117
|
-
*/
|
|
118
|
-
export function renewSignal(): number | undefined {
|
|
119
|
-
void refreshRenewSignal()
|
|
120
|
-
return signalValue
|
|
100
|
+
signalReadAt = now
|
|
101
|
+
inFlight = readRenewSignal().finally(() => {
|
|
102
|
+
inFlight = undefined
|
|
103
|
+
})
|
|
104
|
+
return inFlight
|
|
121
105
|
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { ApolloClient } from '@apollo/client'
|
|
2
|
+
import { beforeEach, expect, it, vi } from 'vitest'
|
|
3
|
+
|
|
4
|
+
const query = {} as ApolloClient.QueryOptions
|
|
5
|
+
|
|
6
|
+
let publishRenewSignal: typeof import('./renewSignal').publishRenewSignal
|
|
7
|
+
let client: ApolloClient
|
|
8
|
+
let created: ApolloClient[]
|
|
9
|
+
|
|
10
|
+
beforeEach(async () => {
|
|
11
|
+
vi.useFakeTimers()
|
|
12
|
+
vi.setSystemTime(new Date('2026-01-01T00:00:00Z'))
|
|
13
|
+
|
|
14
|
+
const entries = new Map<string, unknown>()
|
|
15
|
+
vi.stubGlobal('__incrementalCache', {
|
|
16
|
+
get: async (key: string) => (entries.has(key) ? { value: entries.get(key) } : null),
|
|
17
|
+
set: async (key: string, data: unknown) => {
|
|
18
|
+
entries.set(key, data)
|
|
19
|
+
},
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
vi.resetModules()
|
|
23
|
+
;({ publishRenewSignal } = await import('./renewSignal'))
|
|
24
|
+
const { renewableSsrClient } = await import('./renewableSsrClient')
|
|
25
|
+
|
|
26
|
+
created = []
|
|
27
|
+
client = renewableSsrClient(() => {
|
|
28
|
+
const next = { query: vi.fn(async () => ({ data: {} })), cache: {} } as unknown as ApolloClient
|
|
29
|
+
created.push(next)
|
|
30
|
+
return next
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
// The first query reads the signal. The next read is possible after the poll interval.
|
|
34
|
+
await client.query(query)
|
|
35
|
+
vi.advanceTimersByTime(5000)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('keeps the client when nothing is published', async () => {
|
|
39
|
+
await client.query(query)
|
|
40
|
+
|
|
41
|
+
expect(created).toHaveLength(1)
|
|
42
|
+
expect(created[0].query).toHaveBeenCalledTimes(2)
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('sends the first query after a publish to a new client', async () => {
|
|
46
|
+
await publishRenewSignal()
|
|
47
|
+
await client.query(query)
|
|
48
|
+
|
|
49
|
+
expect(created).toHaveLength(2)
|
|
50
|
+
expect(created[1].query).toHaveBeenCalledTimes(1)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('sends the queries that start at the same moment to the new client', async () => {
|
|
54
|
+
await publishRenewSignal()
|
|
55
|
+
await Promise.all([client.query(query), client.query(query)])
|
|
56
|
+
|
|
57
|
+
expect(created).toHaveLength(2)
|
|
58
|
+
expect(created[0].query).toHaveBeenCalledTimes(1)
|
|
59
|
+
expect(created[1].query).toHaveBeenCalledTimes(2)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it('gives the members of the newest client', async () => {
|
|
63
|
+
await publishRenewSignal()
|
|
64
|
+
await client.query(query)
|
|
65
|
+
|
|
66
|
+
expect(client.cache).toBe(created[1].cache)
|
|
67
|
+
})
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ApolloClient } from '@apollo/client'
|
|
2
|
+
import { refreshRenewSignal } from './renewSignal'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* An Apollo client for server side rendering that lives longer than one request.
|
|
6
|
+
*
|
|
7
|
+
* Each query waits for the renew signal. After a publish, `create` makes a new client and the
|
|
8
|
+
* queries go to that client. A query that is in flight completes on the client that it started on.
|
|
9
|
+
*/
|
|
10
|
+
export function renewableSsrClient(create: () => ApolloClient): ApolloClient {
|
|
11
|
+
let client = create()
|
|
12
|
+
let renewedAt = Date.now()
|
|
13
|
+
|
|
14
|
+
return new Proxy(client, {
|
|
15
|
+
get(_, key) {
|
|
16
|
+
if (key === 'query')
|
|
17
|
+
return async (...args: Parameters<ApolloClient['query']>) => {
|
|
18
|
+
const signal = await refreshRenewSignal()
|
|
19
|
+
if (signal && signal > renewedAt) {
|
|
20
|
+
renewedAt = signal
|
|
21
|
+
client = create()
|
|
22
|
+
}
|
|
23
|
+
return client.query(...args)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const value: unknown = Reflect.get(client, key)
|
|
27
|
+
return typeof value === 'function' ? value.bind(client) : value
|
|
28
|
+
},
|
|
29
|
+
})
|
|
30
|
+
}
|