File tree Expand file tree Collapse file tree 3 files changed +49
-6
lines changed
packages/vue-app/template Expand file tree Collapse file tree 3 files changed +49
-6
lines changed Original file line number Diff line number Diff line change 1
1
import Vue from 'vue'
2
2
3
- const noopData = ( ) => ( { } )
4
-
5
3
// window.{{globals.loadedCallback}} hook
6
4
// Useful for jsdom testing or plugins (https://github.com/tmpvar/jsdom#dealing-with-asynchronous-script-loading)
7
5
if ( process . client ) {
@@ -24,19 +22,27 @@ export function interopDefault(promise) {
24
22
}
25
23
26
24
export function applyAsyncData ( Component , asyncData ) {
27
- const ComponentData = Component . options . data || noopData
28
- // Prevent calling this method for each request on SSR context
29
- if ( ! asyncData && Component . options . hasAsyncData ) {
25
+ if (
26
+ // For SSR, we once all this function without second param to just apply asyncData
27
+ // Prevent doing this for each SSR request
28
+ ! asyncData && Component . options . __hasNuxtData
29
+ ) {
30
30
return
31
31
}
32
- Component . options . hasAsyncData = true
32
+
33
+ const ComponentData = Component . options . _originDataFn || Component . options . data || function ( ) { return { } }
34
+ Component . options . _originDataFn = ComponentData
35
+
33
36
Component . options . data = function ( ) {
34
37
const data = ComponentData . call ( this )
35
38
if ( this . $ssrContext ) {
36
39
asyncData = this . $ssrContext . asyncData [ Component . cid ]
37
40
}
38
41
return { ...data , ...asyncData }
39
42
}
43
+
44
+ Component . options . __hasNuxtData = true
45
+
40
46
if ( Component . _Ctor && Component . _Ctor . options ) {
41
47
Component . _Ctor . options . data = Component . options . data
42
48
}
Original file line number Diff line number Diff line change
1
+ <template >
2
+ <pre >
3
+ {{ debug }}
4
+ </pre >
5
+ </template >
6
+
7
+ <script >
8
+ export default {
9
+ asyncData () {
10
+ return {
11
+ [Math .random ()]: true
12
+ }
13
+ },
14
+ computed: {
15
+ debug () {
16
+ return JSON .stringify (this .$data , null , 2 )
17
+ }
18
+ }
19
+ }
20
+ </script >
Original file line number Diff line number Diff line change @@ -82,6 +82,23 @@ describe('spa', () => {
82
82
expect ( consola . log ) . toHaveBeenCalledWith ( 'mounted' )
83
83
consola . log . mockClear ( )
84
84
} )
85
+
86
+ test ( '/async no asyncData leak' , async ( ) => {
87
+ const window = await nuxt . server . renderAndGetWindow ( url ( '/async' ) )
88
+
89
+ const navigate = url => new Promise ( ( resolve , reject ) => {
90
+ window . $nuxt . $router . push ( url , resolve , reject )
91
+ } )
92
+
93
+ for ( let i = 0 ; i < 3 ; i ++ ) {
94
+ await navigate ( '/' )
95
+ await navigate ( '/async' )
96
+ }
97
+
98
+ const { $data } = window . $nuxt . $route . matched [ 0 ] . instances . default
99
+ expect ( Object . keys ( $data ) . length ) . toBe ( 1 )
100
+ } )
101
+
85
102
// Close server and ask nuxt to stop listening to file changes
86
103
afterAll ( async ( ) => {
87
104
await nuxt . close ( )
You can’t perform that action at this time.
0 commit comments