From 499b3c0c2f947b5a2cd194cbb6db40831e36c8ad Mon Sep 17 00:00:00 2001 From: Phil Demetriou Date: Wed, 27 Jan 2021 22:28:00 +0200 Subject: [PATCH 1/2] Support latest Nuxt in SSR utils --- src/utils/ssr-utils.ts | 122 +++++++++++++++++------------------------ 1 file changed, 51 insertions(+), 71 deletions(-) diff --git a/src/utils/ssr-utils.ts b/src/utils/ssr-utils.ts index aef504d..e8cdf9a 100644 --- a/src/utils/ssr-utils.ts +++ b/src/utils/ssr-utils.ts @@ -1,14 +1,42 @@ import { onServerPrefetch, getCurrentInstance, + onBeforeMount, computed, -} from "./api"; -import { TaskInstance } from "../TaskInstance"; -import { Task } from "../Task"; +} from "@nuxtjs/composition-api"; + +async function wrap (func, catcher, finaliser) { + try { + return await func(); + } catch (e) { + if (catcher) await catcher(e); + } finally { + if (finaliser) await finaliser(); + } +} + +function getNuxtTask (key) { + const ssrContext = process.client && window['__NUXT__']; + + if (!ssrContext) + throw Error(`Could not access window.__NUXT__ or not operating client-side`); + + if (!ssrContext.vueConcurrency || !ssrContext.vueConcurrency[key]) + return undefined; + + return ssrContext.vueConcurrency[key].value; +} -const isServer = () => typeof window === "undefined"; +function setNuxtTask (vm, key, task) { + const { nuxt: ssrContext } = vm.$ssrContext; -export function reviveTaskInstance(instance: TaskInstance) { + if (!ssrContext.vueConcurrency) + ssrContext.vueConcurrency = {}; + + ssrContext.vueConcurrency[key] = computed(() => task._instances); +} + +export function reviveTaskInstance (instance) { if (instance.isError) { instance._deferredObject.promise = Promise.reject(instance.error); } else { @@ -26,84 +54,36 @@ export function reviveTaskInstance(instance: TaskInstance) { instance._deferredObject.promise.finally(...params); } -export function useTaskPrefetch( - key: string, - task: Task -): TaskInstance { - /* Server */ - if (isServer()) { - // perform, add to prefetch, add to ssrContext - const taskInstance = task.perform(); - onServerPrefetch(async () => { - try { - await taskInstance; - saveTaskToNuxtState(key, task); - } catch (e) { - // no need for extra handling - } - }); - return taskInstance; - } +function reviveTaskInstances (key, task) { + const taskCache = getNuxtTask(key); - /* Client */ - const [last] = reviveTaskInstances(key, task).reverse(); - - if (last) { - return last; - } else { - return task.perform(); - } -} - -function saveTaskToNuxtState(key: string, task: Task) { - const { $root } = getCurrentInstance() as any; - const nuxtState = $root && $root.context && $root.context.nuxtState; - if (!nuxtState) { - throw new Error("Could not access $root.context.nuxtState"); - } - - if (!nuxtState.vueConcurrency) { - nuxtState.vueConcurrency = {}; - } - - nuxtState.vueConcurrency[key] = computed(() => ({ - instances: task._instances, - })); -} - -function reviveTaskInstances(key: string, task: Task) { - const taskCache = getTaskFromContext(key); if (taskCache) { - task._instances = taskCache.instances || []; + task._instances = taskCache || []; task._instances.forEach(reviveTaskInstance); - deleteTaskCache(key); } - - return task._instances; } -function getNuxtData() { - return (window as any).__NUXT__; -} +export function useTaskPrefetch (key, task) { + const vm = getCurrentInstance(); -function getTaskFromContext(key) { - if (!getNuxtData()) { - throw Error(`Could not access window.__NUXT__`); + onServerPrefetch(async () => { + await wrap(task.perform); + setNuxtTask(vm, key, task); + }); + + if (process.client) { + onBeforeMount(async () => !task._instances.length && task.perform()); + reviveTaskInstances(key, task); } - return getNuxtData().vueConcurrency[key].value; + return task; } -function deleteTaskCache(key) { - const nuxtData = getNuxtData(); - delete nuxtData.vueConcurrency[key]; -} +export function useSSRPersistance (key, task) { + const vm = getCurrentInstance(); -export function useSSRPersistance(key: string, task: Task) { - if (isServer()) { - saveTaskToNuxtState(key, task); - return; - } + if (process.server) + return setNuxtTask(vm, key, task); reviveTaskInstances(key, task); } From 51dfc8721295b8d421f35d221155b068a4a9dca7 Mon Sep 17 00:00:00 2001 From: Phil Demetriou Date: Wed, 27 Jan 2021 22:33:20 +0200 Subject: [PATCH 2/2] Update ssr-utils.ts --- src/utils/ssr-utils.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/utils/ssr-utils.ts b/src/utils/ssr-utils.ts index e8cdf9a..7b5736b 100644 --- a/src/utils/ssr-utils.ts +++ b/src/utils/ssr-utils.ts @@ -81,9 +81,6 @@ export function useTaskPrefetch (key, task) { export function useSSRPersistance (key, task) { const vm = getCurrentInstance(); - - if (process.server) - return setNuxtTask(vm, key, task); - - reviveTaskInstances(key, task); + if (process.server) setNuxtTask(vm, key, task); + else reviveTaskInstances(key, task); }