Sfoglia il codice sorgente

serialize entity store

Jason Gorst 4 giorni fa
parent
commit
6863cd59f4
1 ha cambiato i file con 21 aggiunte e 0 eliminazioni
  1. 21 0
      app/plugins/normalizePlugin.js

+ 21 - 0
app/plugins/normalizePlugin.js

@@ -0,0 +1,21 @@
+import { useEntityStore } from "pinia-colada-plugin-normalizer"
+
+export default defineNuxtPlugin({
+  name: "normalize-plugin",
+
+  setup: (nuxtApp) => {
+    const entityStore = useEntityStore()
+
+    if (import.meta.server) {
+      nuxtApp.hook("app:rendered", ({ ssrContext }) => {
+        ssrContext.payload.normalizer = entityStore.toJSON()
+
+        // console.log("[normalize-plugin] [entityStore.toJSON]")
+      })
+    } else if (nuxtApp.payload && nuxtApp.payload.normalizer) {
+      entityStore.hydrate(nuxtApp.payload.normalizer)
+
+      // console.log("[normalize-plugin] [entityStore.hydrate]")
+    }
+  }
+})