Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions client/app/composables/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function useHostThirdPartyScripts() {
}

return {
scripts: client.host.nuxt.__hints_tpc,
scripts: client.host.nuxt.payload.__hints.thirdPartyScripts,
isUsingNuxtScripts: Boolean(client.host.nuxt.$scripts),
}
}
Expand Down Expand Up @@ -47,7 +47,7 @@ export function useHostWebVitals() {
export function useHostHydration() {
const host = useHostNuxt()

return { hydration: host.__hints.hydration }
return { hydration: host.payload.__hints.hydration }
}

export function useHostNuxt() {
Expand Down
2 changes: 1 addition & 1 deletion client/app/plugins/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default defineNuxtPlugin(() => {
const nuxtApp = useNuxtApp()
const hydrationMismatches = ref<(HydrationMismatchPayload | LocalHydrationMismatch)[]>([])

hydrationMismatches.value = [...host.__hints.hydration]
hydrationMismatches.value = [...host.payload.__hints.hydration]

$fetch<HydrationMismatchResponse>(new URL(HYDRATION_ROUTE, window.location.origin).href).then((data: { mismatches: HydrationMismatchPayload[] }) => {
hydrationMismatches.value = [...hydrationMismatches.value, ...data.mismatches.filter(m => !hydrationMismatches.value.some(existing => existing.id === m.id))]
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/hydration/composables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function useHydrationCheck() {
method: 'POST',
body,
}).then((payload) => {
nuxtApp.__hints.hydration.push({
nuxtApp.payload.__hints.hydration.push({
...payload,
instance,
vnode: vnodePrehydration,
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/hydration/plugin.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default defineNuxtPlugin({
name: '@nuxt/hints:hydration',
setup() {
const nuxtApp = useNuxtApp()
nuxtApp.__hints = defu(nuxtApp.__hints, {
nuxtApp.payload.__hints = defu(nuxtApp.payload.__hints, {
hydration: [],
})
},
Expand Down
17 changes: 10 additions & 7 deletions src/runtime/lazy-load/composables.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import type { DefineComponent } from 'vue'
import { useNuxtApp } from '#imports'
import { defu } from 'defu'
import type { DirectImportInfo } from './schema'

export function useLazyComponentTracking(components: DirectImportInfo[] = []) {
const nuxtApp = useNuxtApp()
if (!nuxtApp.payload._lazyHydrationState) {
nuxtApp.payload._lazyHydrationState = {
directImports: new Map(),
hasReported: false,
pageLoaded: false,
}
if (!nuxtApp.payload.__hints?.lazyHydrationState) {
nuxtApp.payload.__hints = defu(nuxtApp.payload.__hints, {
lazyHydrationState: {
directImports: new Map(),
hasReported: false,
pageLoaded: false,
},
})
}

const state = nuxtApp.payload._lazyHydrationState
const state = nuxtApp.payload.__hints.lazyHydrationState

for (const comp of components) {
state.directImports.set(comp.componentName, comp)
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/lazy-load/plugin.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default defineNuxtPlugin({
setup() {
const nuxtApp = useNuxtApp()

nuxtApp.__hints = defu(nuxtApp.__hints, {
nuxtApp.payload.__hints = defu(nuxtApp.payload.__hints, {
lazyComponents: [],
})

Expand Down Expand Up @@ -52,7 +52,7 @@ function checkAndReport(state: ComponentLazyLoadState) {
function reportSuggestions(suggestions: DirectImportInfo[]) {
const route = useRoute()
const nuxtApp = useNuxtApp()
nuxtApp.__hints.lazyComponents = suggestions
nuxtApp.payload.__hints.lazyComponents = suggestions

logger.info(
`${suggestions.length} component has not been rendered in SSR nor rendered at hydration time. Consider lazy loading it:\n`,
Expand Down
6 changes: 5 additions & 1 deletion src/runtime/third-party-scripts/plugin.client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { defineNuxtPlugin, ref, useNuxtApp } from '#imports'
import { defu } from 'defu'
import { logger } from '../logger'

const EXTENSIONS_SCHEMES_RE = /^(chrome-extension|moz-extension|safari-extension|ms-browser-extension):/
Expand Down Expand Up @@ -32,7 +33,10 @@ export default defineNuxtPlugin({
setup() {
const nuxtApp = useNuxtApp()

const scripts = nuxtApp.__hints_tpc = ref<{ element: HTMLScriptElement, loaded: boolean }[]>([])
nuxtApp.payload.__hints = defu(nuxtApp.payload.__hints, {
thirdPartyScripts: ref<{ element: HTMLScriptElement, loaded: boolean }[]>([]),
})
const scripts = nuxtApp.payload.__hints.thirdPartyScripts

const isUsingNuxtScripts = !!nuxtApp.$scripts

Expand Down
19 changes: 11 additions & 8 deletions src/runtime/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,32 @@ declare module '#app' {
'hints:scripts:added': (script: HTMLScriptElement) => void
'hints:scripts:loaded': (script: HTMLScriptElement) => void

'hints:webvitals:sync': (webvitals: NuxtApp['__hints']['webvitals']) => void
'hints:webvitals:sync': (webvitals: NuxtPayload['__hints']['webvitals']) => void
'hints:webvitals:lcp': (metric: LCPMetricWithAttribution) => void
'hints:webvitals:inp': (metric: INPMetricWithAttribution) => void
'hints:webvitals:cls': (metric: CLSMetricWithAttribution) => void
}

interface NuxtApp {
__hints_tpc: Ref<{ element: HTMLScriptElement, loaded: boolean }[]>
__tracerOverlay: typeof import('vite-plugin-vue-tracer/client/overlay')
__tracerRecord: typeof import('vite-plugin-vue-tracer/client/record')
}

interface NuxtPayload {
__hints: {
lazyHydrationState?: LazyHydrationState
hydration: LocalHydrationMismatch[]
lazyComponents: DirectImportInfo[]
webvitals: {
lcp: Ref<LCPMetricWithAttribution[]>
inp: Ref<INPMetricWithAttribution[]>
cls: Ref<CLSMetricWithAttribution[]>
}
thirdPartyScripts: Ref<{
element: HTMLScriptElement
loaded: boolean
}[]>
}
__tracerOverlay: typeof import('vite-plugin-vue-tracer/client/overlay')
__tracerRecord: typeof import('vite-plugin-vue-tracer/client/record')
}

interface NuxtPayload {
_lazyHydrationState?: LazyHydrationState
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/runtime/web-vitals/plugin.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default defineNuxtPlugin({
name: 'nuxt-hints:performance',
setup() {
const nuxtApp = useNuxtApp()
nuxtApp.__hints = defu(nuxtApp.__hints, {
nuxtApp.payload.__hints = defu(nuxtApp.payload.__hints, {
webvitals: {
lcp: ref([]),
inp: ref([]),
Expand All @@ -37,9 +37,9 @@ export default defineNuxtPlugin({
})

nuxtApp.hook('hints:webvitals:sync', (webvitals) => {
webvitals.lcp.value = [...nuxtApp.__hints.webvitals.lcp.value]
webvitals.inp.value = [...nuxtApp.__hints.webvitals.inp.value]
webvitals.cls.value = [...nuxtApp.__hints.webvitals.cls.value]
webvitals.lcp.value = [...nuxtApp.payload.__hints.webvitals.lcp.value]
webvitals.inp.value = [...nuxtApp.payload.__hints.webvitals.inp.value]
webvitals.cls.value = [...nuxtApp.payload.__hints.webvitals.cls.value]
})

nuxtApp.hook('app:mounted', () => {
Expand All @@ -51,7 +51,7 @@ export default defineNuxtPlugin({
'[web-vitals] INP Metric: ',
metric,
)
nuxtApp.__hints.webvitals.inp.value.push(metric)
nuxtApp.payload.__hints.webvitals.inp.value.push(metric)
nuxtApp.callHook('hints:webvitals:inp', metric)
}, {
reportAllChanges: true,
Expand All @@ -65,7 +65,7 @@ export default defineNuxtPlugin({
`[web-vitals] LCP Metric: `,
metric,
)
nuxtApp.__hints.webvitals.lcp.value.push(metric)
nuxtApp.payload.__hints.webvitals.lcp.value.push(metric)
nuxtApp.callHook('hints:webvitals:lcp', metric)

for (const performanceEntry of metric.entries) {
Expand Down Expand Up @@ -130,7 +130,7 @@ export default defineNuxtPlugin({
metric,
)
// Push the metric as-is; components will access entries[0] directly for element
nuxtApp.__hints.webvitals.cls.value.push(metric)
nuxtApp.payload.__hints.webvitals.cls.value.push(metric)

for (const entry of metric.entries) {
const performanceEntry = entry
Expand Down
8 changes: 4 additions & 4 deletions test/runtime/lazy-load/composables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('lazy-load composables', () => {
beforeEach(() => {
const nuxtApp = useNuxtApp()
// Reset lazy hydration state between tests
delete nuxtApp.payload._lazyHydrationState
delete nuxtApp.payload.__hints?.lazyHydrationState
})

describe('useLazyComponentTracking', () => {
Expand Down Expand Up @@ -90,7 +90,7 @@ describe('lazy-load composables', () => {
wrapped.setup!({}, { attrs: {}, slots: {}, emit: () => {}, expose: () => {} })

const nuxtApp = useNuxtApp()
const state = nuxtApp.payload._lazyHydrationState
const state = nuxtApp.payload.__hints?.lazyHydrationState
expect(state).toBeDefined()
expect(state.directImports.get('Child')).toEqual(imports[0])
})
Comment on lines 92 to 96
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟑 Minor

state is typed T | undefined after ?. β€” unguarded property accesses may cause TypeScript errors.

nuxtApp.payload.__hints?.lazyHydrationState infers LazyHydrationState | undefined. expect(state).toBeDefined() guards the runtime path but TypeScript's control-flow analysis does not narrow through Vitest assertions, so state.directImports (line 95) is a potential strict-mode type error. The same pattern repeats at lines 124 and 145 (line 145 lacks even a toBeDefined() guard before property access at 147–149).

Since setup!() is called before each read, __hints.lazyHydrationState is guaranteed to be initialized. Drop the optional chain to get a non-nullable type:

πŸ›‘οΈ Proposed fix (same pattern applies to lines 124 and 145)
-     const state = nuxtApp.payload.__hints?.lazyHydrationState
+     const state = nuxtApp.payload.__hints!.lazyHydrationState
      expect(state).toBeDefined()
      expect(state.directImports.get('Child')).toEqual(imports[0])
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const nuxtApp = useNuxtApp()
const state = nuxtApp.payload._lazyHydrationState
const state = nuxtApp.payload.__hints?.lazyHydrationState
expect(state).toBeDefined()
expect(state.directImports.get('Child')).toEqual(imports[0])
})
const nuxtApp = useNuxtApp()
const state = nuxtApp.payload.__hints!.lazyHydrationState
expect(state).toBeDefined()
expect(state.directImports.get('Child')).toEqual(imports[0])
})
πŸ€– Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@test/runtime/lazy-load/composables.test.ts` around lines 92 - 96, The test
reads nuxtApp.payload.__hints?.lazyHydrationState which types state as possibly
undefined, causing TypeScript errors when accessing properties even though
setup!() guarantees initialization; remove the optional chaining so you read
nuxtApp.payload.__hints.lazyHydrationState (and similarly at the other
occurrences around the reads at lines referencing state) so state is
non-nullable, then keep the runtime expect(state).toBeDefined() assertions for
safety β€” update the accesses in this file (the variable named state and the
expression nuxtApp.payload.__hints?.lazyHydrationState) to use the non-optional
form.

Expand Down Expand Up @@ -121,7 +121,7 @@ describe('lazy-load composables', () => {
wrapped.setup!({}, { attrs: {}, slots: {}, emit: () => {}, expose: () => {} })

const nuxtApp = useNuxtApp()
const state = nuxtApp.payload._lazyHydrationState
const state = nuxtApp.payload.__hints?.lazyHydrationState
expect(state).toBeDefined()
expect(state.directImports.get('MyComp')).toEqual({
componentName: 'MyComp',
Expand All @@ -142,7 +142,7 @@ describe('lazy-load composables', () => {
wrapped.setup!({}, { attrs: {}, slots: {}, emit: () => {}, expose: () => {} })

const nuxtApp = useNuxtApp()
const state = nuxtApp.payload._lazyHydrationState
const state = nuxtApp.payload.__hints?.lazyHydrationState
// Should keep original import source since entry already existed
expect(state.directImports.get('MyComp')!.importSource).toBe('./Original.vue')
// But rendered should be true
Expand Down
Loading