fix(nuxt): exclude data-v attrs from server component props (#23095)

This commit is contained in:
Julien Huang 2023-09-10 10:06:11 +02:00 committed by GitHub
parent dfdebf2919
commit 3f9fa008d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 5 deletions

View File

@ -7,11 +7,14 @@ export const createServerComponent = (name: string) => {
inheritAttrs: false, inheritAttrs: false,
props: { lazy: Boolean }, props: { lazy: Boolean },
setup (props, { attrs, slots }) { setup (props, { attrs, slots }) {
return () => h(NuxtIsland, { return () => {
return h(NuxtIsland, {
name, name,
lazy: props.lazy, lazy: props.lazy,
props: attrs // #23051 - remove data-v attributes
props: Object.fromEntries(Object.entries(attrs).filter(([key]) => !key.startsWith('data-v-')))
}, slots) }, slots)
} }
}
}) })
} }

View File

@ -0,0 +1,41 @@
import { describe, expect, it, vi } from 'vitest'
import { h } from 'vue'
import { createServerComponent } from '../../packages/nuxt/src/components/runtime/server-component'
vi.mock('vue', async (original) => {
const vue = await original<typeof import('vue')>()
return {
...vue,
h: vi.fn(vue.h)
}
})
describe('runtime server component', () => {
it('expect no data-v- attrbutes #23051', () => {
// @ts-expect-error mock
vi.mocked(h).mockImplementation(() => null)
// @ts-expect-error test setup
createServerComponent('DummyName').setup!({
lazy: false
}, {
attrs: {
'data-v-123': '',
test: 1
},
slots: {},
emit: vi.fn(),
expose: vi.fn()
})()
expect(h).toHaveBeenCalledOnce()
if (!vi.mocked(h).mock.lastCall) { throw new Error('no last call') }
expect(vi.mocked(h).mock.lastCall![1]?.props).toBeTypeOf('object')
expect(Object.keys(vi.mocked(h).mock.lastCall![1]?.props)).not.toContain('data-v-123')
expect(vi.mocked(h).mock.lastCall![1]?.props).toMatchInlineSnapshot(`
{
"test": 1,
}
`)
})
})