Skip to content

Commit

Permalink
refactor: use nuxt useState composable for SSR data
Browse files Browse the repository at this point in the history
  • Loading branch information
gbicou committed Dec 18, 2023
1 parent 2f45944 commit dc7fe0c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-masks-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bicou/nuxt-urql": patch
---

use nuxt useState composable for SSR data
11 changes: 7 additions & 4 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createClient, ssrExchange } from "@urql/core";
import { ref, defineNuxtPlugin, useRuntimeConfig } from "#imports";
import { createClient, type SSRData, ssrExchange } from "@urql/core";
import { ref, defineNuxtPlugin, useRuntimeConfig, useState } from "#imports";
import NuxtUrqlClient from "#urql-client";

export default defineNuxtPlugin(async (nuxtApp) => {
Expand All @@ -11,17 +11,20 @@ export default defineNuxtPlugin(async (nuxtApp) => {
...ssrParams,
});

// ssr data in nuxt state
const ssrData = useState<SSRData>(ssrParams.key);

// when app is created in browser, restore SSR state from nuxt payload
if (process.client) {
nuxtApp.hook("app:created", () => {
ssr.restoreData(nuxtApp.payload.data[ssrParams.key]);
ssr.restoreData(ssrData.value);
});
}

// when app has rendered in server, send SSR state to client
if (process.server) {
nuxtApp.hook("app:rendered", () => {
nuxtApp.payload.data[ssrParams.key] = ssr.extractData();
ssrData.value = ssr.extractData();
});
}

Expand Down

2 comments on commit dc7fe0c

@vercel
Copy link

@vercel vercel bot commented on dc7fe0c Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nuxt-urql-playground – ./playground

nuxt-urql-playground-bicou.vercel.app
nuxt-urql-playground.vercel.app
nuxt-urql-playground-git-main-bicou.vercel.app

@vercel
Copy link

@vercel vercel bot commented on dc7fe0c Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nuxt-urql-docs – ./docs

nuxt-urql-docs-bicou.vercel.app
nuxt-urql-docs-git-main-bicou.vercel.app
nuxt-urql-docs.vercel.app

Please sign in to comment.