Skip to content

Commit

Permalink
fix(nuxt): don't refetch server components in initial html (#26089)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Mar 6, 2024
1 parent b90b4da commit b4bce57
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
5 changes: 3 additions & 2 deletions packages/nuxt/src/app/components/nuxt-island.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ export default defineComponent({
components: {}
}


if (nuxtApp.isHydrating) {
payloads.slots = toRaw(nuxtApp.payload.data[`${props.name}_${hashId.value}`])?.slots ?? {}
payloads.components = toRaw(nuxtApp.payload.data[`${props.name}_${hashId.value}`])?.components ?? {}
Expand All @@ -119,6 +118,8 @@ export default defineComponent({

if (import.meta.client && nuxtApp.isHydrating) {
ssrHTML.value = getFragmentHTML(instance.vnode?.el ?? null, true)?.join('') || ''
const key = `${props.name}_${hashId.value}`
nuxtApp.payload.data[key].html = ssrHTML.value
}

const uid = ref<string>(ssrHTML.value.match(SSR_UID_RE)?.[1] ?? getId())
Expand Down Expand Up @@ -149,7 +150,7 @@ export default defineComponent({
async function _fetchComponent (force = false) {
const key = `${props.name}_${hashId.value}`

if (nuxtApp.payload.data[key]?.html && !force) { return nuxtApp.payload.data[key] }
if (!force && nuxtApp.payload.data[key]?.html) { return nuxtApp.payload.data[key] }

const url = remoteComponentIslands && props.source ? new URL(`/__nuxt_island/${key}.json`, props.source).href : `/__nuxt_island/${key}.json`

Expand Down
12 changes: 6 additions & 6 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2134,22 +2134,22 @@ describe.skipIf(isDev() || isWindows || !isRenderingJson)('payload rendering', (
await gotoPath(page, '/random/a')

// We are manually prefetching other payloads
await page.waitForRequest(url('/random/c/_payload.json'))
await page.waitForRequest(request => request.url().includes('/random/c/_payload.json'))

// We are not triggering API requests in the payload
expect(requests).not.toContain(expect.stringContaining('/api/random'))
expect(requests).not.toContain(expect.stringContaining('/__nuxt_island'))
expect(requests).not.toContainEqual(expect.stringContaining('/api/random'))
expect(requests).not.toContainEqual(expect.stringContaining('/__nuxt_island'))
// requests.length = 0

await page.click('[href="/random/b"]')
await page.waitForLoadState('networkidle')

// We are not triggering API requests in the payload in client-side nav
expect(requests).not.toContain('/api/random')
expect(requests).not.toContain(expect.stringContaining('/__nuxt_island'))
expect(requests).not.toContainEqual(expect.stringContaining('/__nuxt_island'))

// We are fetching a payload we did not prefetch
expect(requests).toContain('/random/b/_payload.json')
expect(requests).toContainEqual(expect.stringContaining('/random/b/_payload.json'))

// We are not refetching payloads we've already prefetched
// expect(requests.filter(p => p.includes('_payload')).length).toBe(1)
Expand All @@ -2160,7 +2160,7 @@ describe.skipIf(isDev() || isWindows || !isRenderingJson)('payload rendering', (

// We are not triggering API requests in the payload in client-side nav
expect(requests).not.toContain('/api/random')
expect(requests).not.toContain(expect.stringContaining('/__nuxt_island'))
expect(requests).not.toContainEqual(expect.stringContaining('/__nuxt_island'))

// We are not refetching payloads we've already prefetched
// Note: we refetch on dev as urls differ between '' and '?import'
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/basic/pages/random/[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div>
<NuxtLink
to="/"
prefetched-class="prefetched"
no-prefetch
>
Home
</NuxtLink>
Expand Down

0 comments on commit b4bce57

Please sign in to comment.