Skip to content

Improve directive SSR #921

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

Merged
merged 4 commits into from
Feb 14, 2025
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
73 changes: 67 additions & 6 deletions __tests__/vue/ssr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import type { FluentVue } from '../../src'
import { FluentBundle, FluentResource } from '@fluent/bundle'

import ftl from '@fluent/dedent'
import { beforeEach, describe, expect, it } from 'vitest'
import { beforeEach, describe, expect, it, vi } from 'vitest'

import { isVue3 } from 'vue-demi'

import { createFluentVue } from '../../src'
import { renderSSR } from '../utils/ssr'

describe.skipIf(!isVue3)('sSR directive', () => {
describe.skipIf(!isVue3)('ssr directive', () => {
let fluent: FluentVue
let bundle: FluentBundle

Expand All @@ -34,15 +34,76 @@ describe.skipIf(!isVue3)('sSR directive', () => {
data: () => ({
name: 'John',
}),
template: '<a v-t:link href="/foo">Fallback text</a>',
template: '<a v-t:link href="/foo"></a>',
}

// Act
const rendered = await renderSSR(fluent, component)

// Assert
// This has fallback text because the textContent is not supported by Vue getSSRProps
// Text will be translated using directive transform
expect(rendered).toEqual('<a href="/foo" aria-label="Link aria label">Fallback text</a>')
expect(rendered).toEqual('<a href="/foo" aria-label="Link aria label">Link text</a>')
})

it('skips text content if not specified', async () => {
// Arrange
bundle.addResource(
new FluentResource(ftl`
link =
.aria-label = Link aria label
`),
)

const component = {
data: () => ({
name: 'John',
}),
template: '<a v-t:link href="/foo">Test</a>',
}

// Act
const rendered = await renderSSR(fluent, component)

// Assert
expect(rendered).toEqual('<a href="/foo" aria-label="Link aria label">Test</a>')
})

it('warns when missing translation key', async () => {
// Arrange
const warnSpy = vi.spyOn(console, 'warn')

const component = {
template: '<a v-t href="/foo"></a>',
}

// Act
const rendered = await renderSSR(fluent, component)

// Assert
expect(rendered).toEqual('<a href="/foo"></a>')
expect(warnSpy).toHaveBeenCalledWith('[fluent-vue] v-t directive is missing arg with translation key')
})

it('only allows certain attributes', async () => {
// Arrange
bundle.addResource(
new FluentResource(ftl`
message = Hello, { $name }!
.aria-label = Link aria label
.href = Link href
`),
)

const component = {
data: () => ({
name: 'John',
}),
template: '<a v-t:message="{ name }"></a>',
}

// Act
const rendered = await renderSSR(fluent, component)

// Assert
expect(rendered).toEqual('<a aria-label="Link aria label" href="Link href">Hello, \u{2068}John\u{2069}!</a>')
})
})
30 changes: 15 additions & 15 deletions src/vue/directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,26 @@ export function createVue3Directive(rootContext: TranslationContext): Vue3Direct

getSSRProps(binding) {
const context = getContext(rootContext, binding.instance)
const key = binding.arg
if (key === void 0) {
if (binding.arg === void 0) {
warn('v-t directive is missing arg with translation key')
return {}
}
const translation = context.formatWithAttrs(key, binding.value)
const allowedAttrs = Object.keys(binding.modifiers)
const attrs: Record<string, string> = {}
for (const [attr, attrValue] of Object.entries(translation.attributes)) {
// Vue 3 does not expose the element in the binding object
// so we can't check if the attribute is allowed
// we assume that all attributes are allowed
// this could lead to SSR hydration mismatches if translation
// contains attributes that are not allowed
// There is a runtime warning in the browser console in case translation contains not allowed attributes
if (isAttrNameLocalizable(attr, {} as HTMLElement, allowedAttrs))
attrs[attr] = attrValue

const translation = context.formatWithAttrs(binding.arg, binding.value)

// Vue 3 does not expose the element in the binding object during SSR.
// So we can't check if the attribute is allowed.
// We assume that all attributes are allowed.
// This could lead to SSR hydration mismatches if translation
// contains attributes that are not allowed.
// There is a runtime warning in the browser console in case translation
// contains not allowed attributes, this should catch this case.
const attrs = translation.attributes

if (translation.hasValue) {
attrs.textContent = translation.value
}

// TODO: Include textContent when https://github.com/vuejs/core/issues/8112 is resolved
return attrs
},
}
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy