@@ -2,8 +2,7 @@ import Vue from 'vue'
2
2
import { createRenderer } from 'vue-server-renderer'
3
3
import { WebApp } from 'meteor/webapp'
4
4
import cookieParser from 'cookie-parser'
5
- import { onPageLoad } from 'meteor/server-render'
6
- import { FastRender } from 'meteor/staringatlights:fast-render'
5
+ import { FastRender } from 'meteor/communitypackages:fast-render'
7
6
8
7
import SsrContext from './context'
9
8
import patchSubscribeData from './data'
@@ -70,98 +69,59 @@ function writeServerError (sink) {
70
69
71
70
WebApp . rawConnectHandlers . use ( cookieParser ( ) )
72
71
73
- onPageLoad ( sink => new Promise ( ( resolve , reject ) => {
72
+ FastRender . onPageLoad ( sink => new Promise ( async ( resolve , reject ) => {
74
73
const req = sink . request
75
74
76
- // Fast render
77
- const loginToken = req . cookies [ 'meteor_login_token' ]
78
- const headers = req . headers
79
- const frLoginContext = new FastRender . _Context ( loginToken , { headers } )
80
-
81
- FastRender . frContext . withValue ( frLoginContext , function ( ) {
82
- // we're stealing all the code from FlowRouter SSR
83
- // https://github.com/kadirahq/flow-router/blob/ssr/server/route.js#L61
84
- const ssrContext = new SsrContext ( )
85
-
86
- VueSSR . ssrContext . withValue ( ssrContext , ( ) => {
87
- try {
88
- // const frData = InjectData.getData(res, 'fast-render-data')
89
- // if (frData) {
90
- // ssrContext.addData(frData.collectionData)
91
- // }
92
-
93
- // Vue
94
- const context = { url : req . url }
95
- let asyncResult
96
- const result = VueSSR . createApp ( context )
97
- if ( result && typeof result . then === 'function' ) {
98
- asyncResult = result
99
- } else {
100
- asyncResult = Promise . resolve ( result )
101
- }
102
-
103
- asyncResult . then ( app => {
104
- renderer . renderToString (
105
- app ,
106
- context ,
107
- ( error , html ) => {
108
- if ( error ) {
109
- console . error ( error )
110
- writeServerError ( sink )
111
- return
112
- }
113
-
114
- // const frContext = FastRender.frContext.get()
115
- // const data = frContext.getData()
116
- // // InjectData.pushData(res, 'fast-render-data', data)
117
- // const injectData = EJSON.stringify({
118
- // 'fast-render-data': data,
119
- // })
120
- // // sink.appendToHead(`<script type="text/inject-data">${encodeURIComponent(injectData)}</script>`)
121
-
122
- let appendHtml
123
- if ( typeof context . appendHtml === 'function' ) appendHtml = context . appendHtml ( )
124
-
125
- const head = ( ( appendHtml && appendHtml . head ) || context . head ) || ''
126
- const body = ( ( appendHtml && appendHtml . body ) || context . body ) || ''
127
- const js = ( ( appendHtml && appendHtml . js ) || context . js ) || ''
128
-
129
- const script = js && `<script type="text/javascript">${ js } </script>`
130
-
131
- sink . renderIntoElementById ( VueSSR . outlet , html )
132
- sink . appendToHead ( head )
133
- sink . appendToBody ( [ body , script ] )
134
-
135
- resolve ( )
136
- } ,
137
- )
138
- } ) . catch ( e => {
139
- console . error ( e )
140
- writeServerError ( sink )
141
- resolve ( )
142
- } )
143
- } catch ( error ) {
144
- console . error ( error )
145
- writeServerError ( sink )
146
- resolve ( )
75
+ const ssrContext = new SsrContext ( )
76
+
77
+ await VueSSR . ssrContext . withValue ( ssrContext , ( ) => {
78
+ try {
79
+ // Vue
80
+ const context = { url : req . url }
81
+ let asyncResult
82
+ const result = VueSSR . createApp ( context )
83
+ if ( result && typeof result . then === 'function' ) {
84
+ asyncResult = result
85
+ } else {
86
+ asyncResult = Promise . resolve ( result )
147
87
}
148
- } )
149
- } )
150
- } ) )
151
-
152
- return
153
-
154
- /* eslint-disable */
155
88
156
- Meteor . bindEnvironment ( function ( ) {
157
- WebApp . rawConnectHandlers . use ( cookieParser ( ) )
158
-
159
- WebApp . connectHandlers . use ( ( req , res , next ) => {
160
- if ( ! IsAppUrl ( req ) ) {
161
- next ( )
162
- return
89
+ asyncResult . then ( app => {
90
+ renderer . renderToString (
91
+ app ,
92
+ context ,
93
+ ( error , html ) => {
94
+ if ( error ) {
95
+ console . error ( error )
96
+ writeServerError ( sink )
97
+ return
98
+ }
99
+
100
+ let appendHtml
101
+ if ( typeof context . appendHtml === 'function' ) appendHtml = context . appendHtml ( )
102
+
103
+ const head = ( ( appendHtml && appendHtml . head ) || context . head ) || ''
104
+ const body = ( ( appendHtml && appendHtml . body ) || context . body ) || ''
105
+ const js = ( ( appendHtml && appendHtml . js ) || context . js ) || ''
106
+
107
+ const script = js && `<script type="text/javascript">${ js } </script>`
108
+
109
+ sink . renderIntoElementById ( VueSSR . outlet , html )
110
+ sink . appendToHead ( head )
111
+ sink . appendToBody ( [ body , script ] )
112
+
113
+ resolve ( )
114
+ } ,
115
+ )
116
+ } ) . catch ( e => {
117
+ console . error ( e )
118
+ writeServerError ( sink )
119
+ resolve ( )
120
+ } )
121
+ } catch ( error ) {
122
+ console . error ( error )
123
+ writeServerError ( sink )
124
+ resolve ( )
163
125
}
164
-
165
-
166
126
} )
167
- } ) ( )
127
+ } ) )
0 commit comments