Skip to content
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

fix(nuxt): don't refetch server components in initial html #26089

Merged
merged 3 commits into from
Mar 6, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/nuxt/src/app/components/nuxt-island.ts
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
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
Expand Up @@ -2,7 +2,7 @@
<div>
<NuxtLink
to="/"
prefetched-class="prefetched"
no-prefetch
>
Home
</NuxtLink>
Expand Down