Skip to content

Commit 9e08b37

Browse files
fix(overmind): improve ssr bug fix
1 parent 82f8f93 commit 9e08b37

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed

packages/node_modules/proxy-state-tree/src/Proxyfier.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ const shouldProxy = (value: any) => {
5353
export class Proxifier {
5454
CACHED_PROXY = Symbol('CACHED_PROXY')
5555
delimiter: string
56+
ssr: boolean
5657
constructor(private tree: TTree) {
5758
this.delimiter = tree.master.options.delimiter
59+
this.ssr = Boolean(tree.master.options.ssr)
5860
}
5961
private concat(path, prop) {
6062
return path ? path + this.delimiter + prop : prop
@@ -141,7 +143,7 @@ export class Proxifier {
141143
)
142144
}
143145
private createArrayProxy(value, path) {
144-
if (this.isProxyCached(value, path)) {
146+
if (!this.ssr && this.isProxyCached(value, path)) {
145147
return value[this.CACHED_PROXY]
146148
}
147149

@@ -234,16 +236,18 @@ export class Proxifier {
234236
},
235237
})
236238

237-
Object.defineProperty(value, this.CACHED_PROXY, {
238-
value: proxy,
239-
configurable: true,
240-
})
239+
if (!this.ssr) {
240+
Object.defineProperty(value, this.CACHED_PROXY, {
241+
value: proxy,
242+
configurable: true,
243+
})
244+
}
241245

242246
return proxy
243247
}
244248

245249
private createObjectProxy(object, path) {
246-
if (this.isProxyCached(object, path)) {
250+
if (!this.ssr && this.isProxyCached(object, path)) {
247251
return object[this.CACHED_PROXY]
248252
}
249253

@@ -386,10 +390,12 @@ export class Proxifier {
386390
},
387391
})
388392

389-
Object.defineProperty(object, this.CACHED_PROXY, {
390-
value: proxy,
391-
configurable: true,
392-
})
393+
if (!this.ssr) {
394+
Object.defineProperty(object, this.CACHED_PROXY, {
395+
value: proxy,
396+
configurable: true,
397+
})
398+
}
393399

394400
return proxy
395401
}

packages/node_modules/proxy-state-tree/src/index.test.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,23 @@ describe('TrackStateTree', () => {
1616
tree.getTrackStateTree().track(() => {}).state[IS_PROXY]
1717
).toBeTruthy()
1818
})
19-
test('should not create proxies when SSR', () => {
20-
const state = {}
19+
test('should not cache proxies in SSR', () => {
20+
const state = {
21+
foo: {},
22+
}
2123
const tree = new ProxyStateTree(state, {
2224
ssr: true,
2325
})
2426

25-
expect(tree.getTrackStateTree().track(() => {}).state[IS_PROXY]).toBeFalsy()
27+
const trackStateTree = tree.getTrackStateTree()
28+
trackStateTree.track(() => {
29+
trackStateTree.state.foo
30+
})
31+
32+
expect(
33+
// @ts-ignore
34+
trackStateTree.state.foo[trackStateTree.proxifier.CACHED_PROXY]
35+
).toBeFalsy()
2636
})
2737

2838
test('should not create nested proxies when initialized', () => {

packages/node_modules/proxy-state-tree/src/index.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,12 @@ export class ProxyStateTree<T extends object> implements IProxyStateTree<T> {
7373
private createTrackStateProxifier() {
7474
const trackStateTree = new TrackStateTree(this)
7575

76-
if (this.options.ssr) {
77-
this.state = this.sourceState
78-
} else {
79-
this.proxifier = trackStateTree.proxifier = new Proxifier(trackStateTree)
76+
this.proxifier = trackStateTree.proxifier = new Proxifier(trackStateTree)
8077

81-
this.state = trackStateTree.state = this.proxifier.proxify(
82-
this.sourceState,
83-
''
84-
)
85-
}
78+
this.state = trackStateTree.state = this.proxifier.proxify(
79+
this.sourceState,
80+
''
81+
)
8682
}
8783
getMutationTree(): IMutationTree<T> {
8884
if (!this.options.devmode) {

0 commit comments

Comments
 (0)