-
Notifications
You must be signed in to change notification settings - Fork 10
refactor: move SSE to devtools RPC #314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,91 @@ | ||
| <script setup lang="ts"> | ||
| import { useDevtoolsClient } from '@nuxt/devtools-kit/iframe-client' | ||
| import { useDevtoolsClient, onDevtoolsClientConnected } from '@nuxt/devtools-kit/iframe-client' | ||
| import type { HintsClientFunctions } from '../../src/runtime/core/rpc-types' | ||
| import { RPC_NAMESPACE } from '../../src/runtime/core/rpc-types' | ||
| import type { HydrationMismatchPayload, HydrationMismatchResponse, LocalHydrationMismatch } from '../../src/runtime/hydration/types' | ||
| import type { ComponentLazyLoadData } from '../../src/runtime/lazy-load/schema' | ||
| import { ComponentLazyLoadDataSchema } from '../../src/runtime/lazy-load/schema' | ||
| import type { HtmlValidateReport } from '../../src/runtime/html-validate/types' | ||
| import { parse } from 'valibot' | ||
| import { HYDRATION_ROUTE, LAZY_LOAD_ROUTE, HTML_VALIDATE_ROUTE } from './utils/routes' | ||
|
|
||
| const client = useDevtoolsClient() | ||
|
|
||
| const hydrationMismatches = ref<(HydrationMismatchPayload | LocalHydrationMismatch)[]>([]) | ||
| const lazyLoadHints = ref<ComponentLazyLoadData[]>([]) | ||
| const htmlValidateReports = ref<HtmlValidateReport[]>([]) | ||
|
|
||
| const nuxtApp = useNuxtApp() | ||
| nuxtApp.provide('hydrationMismatches', hydrationMismatches) | ||
| nuxtApp.provide('lazyLoadHints', lazyLoadHints) | ||
| nuxtApp.provide('htmlValidateReports', htmlValidateReports) | ||
|
|
||
| onDevtoolsClientConnected((client) => { | ||
| // Hydration: seed from host payload and fetch from server | ||
| if (useHintsFeature('hydration')) { | ||
| hydrationMismatches.value = [...client.host.nuxt.payload.__hints.hydration] | ||
| $fetch<HydrationMismatchResponse>(new URL(HYDRATION_ROUTE, window.location.origin).href).then((data) => { | ||
| hydrationMismatches.value = [ | ||
| ...hydrationMismatches.value, | ||
| ...data.mismatches.filter(m => !hydrationMismatches.value.some(existing => existing.id === m.id)), | ||
| ] | ||
| }) | ||
| } | ||
|
Comment on lines
+25
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add defensive check for payload hydration data. Line 23 spreads 🛡️ Proposed defensive access if (useHintsFeature('hydration')) {
- hydrationMismatches.value = [...client.host.nuxt.payload.__hints.hydration]
+ hydrationMismatches.value = [...(client.host.nuxt.payload.__hints?.hydration ?? [])]
$fetch<HydrationMismatchResponse>(new URL(HYDRATION_ROUTE, window.location.origin).href).then((data) => {🤖 Prompt for AI Agents |
||
|
|
||
| // Lazy load: fetch from server | ||
| if (useHintsFeature('lazyLoad')) { | ||
| $fetch<ComponentLazyLoadData[]>(new URL(LAZY_LOAD_ROUTE, window.location.origin).href).then((data) => { | ||
| lazyLoadHints.value = [ | ||
| ...lazyLoadHints.value, | ||
| ...(data ?? []).filter(d => !lazyLoadHints.value.some(existing => existing.id === d.id)), | ||
| ] | ||
| }) | ||
| } | ||
|
|
||
| // HTML validate: fetch from server | ||
| if (useHintsFeature('htmlValidate')) { | ||
| $fetch<HtmlValidateReport[]>(new URL(HTML_VALIDATE_ROUTE, window.location.origin).href).then((data) => { | ||
| htmlValidateReports.value = [ | ||
| ...htmlValidateReports.value, | ||
| ...(data ?? []).filter(d => !htmlValidateReports.value.some(existing => existing.id === d.id)), | ||
| ] | ||
| }) | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Register client RPC functions for real-time push notifications | ||
| client.devtools.extendClientRpc<Record<string, unknown>, HintsClientFunctions>(RPC_NAMESPACE, { | ||
| onHydrationMismatch(mismatch: HydrationMismatchPayload) { | ||
| if (!hydrationMismatches.value.some(existing => existing.id === mismatch.id)) { | ||
| hydrationMismatches.value.push(mismatch) | ||
| } | ||
| }, | ||
| onHydrationCleared(ids: string[]) { | ||
| hydrationMismatches.value = hydrationMismatches.value.filter(m => !ids.includes(m.id)) | ||
| }, | ||
| onLazyLoadReport(data: ComponentLazyLoadData) { | ||
| try { | ||
| const validated = parse(ComponentLazyLoadDataSchema, data) | ||
| if (!lazyLoadHints.value.some(existing => existing.id === validated.id)) { | ||
| lazyLoadHints.value.push(validated) | ||
| } | ||
| } | ||
| catch { | ||
| console.warn('[hints] Ignoring malformed lazy-load report', data) | ||
| } | ||
| }, | ||
| onLazyLoadCleared(id: string) { | ||
| lazyLoadHints.value = lazyLoadHints.value.filter(entry => entry.id !== id) | ||
| }, | ||
| onHtmlValidateReport(report: HtmlValidateReport) { | ||
| if (!htmlValidateReports.value.some(existing => existing.id === report.id)) { | ||
| htmlValidateReports.value = [...htmlValidateReports.value, report] | ||
| } | ||
| }, | ||
| onHtmlValidateDeleted(id: string) { | ||
| htmlValidateReports.value = htmlValidateReports.value.filter(report => report.id !== id) | ||
| }, | ||
| }) | ||
| }) | ||
| </script> | ||
|
|
||
| <template> | ||
|
|
||
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import type { HydrationMismatchPayload } from '../hydration/types' | ||
| import type { ComponentLazyLoadData } from '../lazy-load/schema' | ||
| import type { HtmlValidateReport } from '../html-validate/types' | ||
|
|
||
| export interface HintsClientFunctions { | ||
| onHydrationMismatch: (mismatch: HydrationMismatchPayload) => void | ||
| onHydrationCleared: (ids: string[]) => void | ||
| onLazyLoadReport: (data: ComponentLazyLoadData) => void | ||
| onLazyLoadCleared: (id: string) => void | ||
| onHtmlValidateReport: (report: HtmlValidateReport) => void | ||
| onHtmlValidateDeleted: (id: string) => void | ||
| } | ||
|
|
||
| export const RPC_NAMESPACE = 'nuxt-hints' | ||
|
|
||
| declare global { | ||
| var __nuxtHintsRpcBroadcast: HintsClientFunctions | undefined | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import type { NitroApp } from 'nitropack/types' | ||
| import type { HintsClientFunctions } from '../rpc-types' | ||
|
|
||
| function getRpcBroadcast(): HintsClientFunctions | undefined { | ||
| return globalThis.__nuxtHintsRpcBroadcast | ||
| } | ||
|
|
||
| export default function (nitroApp: NitroApp) { | ||
| nitroApp.hooks.hook('hints:hydration:mismatch', (mismatch) => { | ||
| getRpcBroadcast()?.onHydrationMismatch(mismatch) | ||
| }) | ||
|
|
||
| nitroApp.hooks.hook('hints:hydration:cleared', (payload) => { | ||
| getRpcBroadcast()?.onHydrationCleared(payload.id) | ||
| }) | ||
|
|
||
| nitroApp.hooks.hook('hints:lazy-load:report', (data) => { | ||
| getRpcBroadcast()?.onLazyLoadReport(data) | ||
| }) | ||
|
|
||
| nitroApp.hooks.hook('hints:lazy-load:cleared', (payload) => { | ||
| getRpcBroadcast()?.onLazyLoadCleared(payload.id) | ||
| }) | ||
|
|
||
| nitroApp.hooks.hook('hints:html-validate:report', (report) => { | ||
| getRpcBroadcast()?.onHtmlValidateReport(report) | ||
| }) | ||
|
|
||
| nitroApp.hooks.hook('hints:html-validate:deleted', (id) => { | ||
| getRpcBroadcast()?.onHtmlValidateDeleted(id) | ||
| }) | ||
| } |
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.