From 7952de520c8c1850d9da9a2643958035403570a9 Mon Sep 17 00:00:00 2001 From: Clark Du Date: Fri, 1 Dec 2017 18:22:25 +0800 Subject: [PATCH] refactor: unregister afterEach after route changed in guard --- lib/app/index.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/app/index.js b/lib/app/index.js index 65e72d451b..eeffda05f4 100644 --- a/lib/app/index.js +++ b/lib/app/index.js @@ -168,16 +168,13 @@ async function createApp (ssrContext) { if (process.server && ssrContext && ssrContext.url) { await new Promise((resolve, reject) => { router.push(ssrContext.url, resolve, () => { - let initSSR = true // navigated to a different route in router guard - router.afterEach(async (to, from, next) => { - if (initSSR) { - ssrContext.url = to.fullPath - app.context.route = await getRouteData(to) - app.context.params = to.params || {} - app.context.query = to.query || {} - } - initSSR = false + const unregister = router.afterEach(async (to, from, next) => { + ssrContext.url = to.fullPath + app.context.route = await getRouteData(to) + app.context.params = to.params || {} + app.context.query = to.query || {} + unregister() resolve() }) })