Skip to content

Add support for binding SSR props in Vue 3 directive #869

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 2 commits into from
Apr 4, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions __tests__/utils/ssr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as TestUtils from '@vue/test-utils'
import type { ComponentOptions } from 'vue-3'
import { install } from 'vue-demi'

import type { FluentVue } from '../../src'

install()

export function renderSSR<T extends object>(
fluent: FluentVue | null,
component: ComponentOptions<T>,
): Promise<string> {
const { renderToString } = TestUtils

const plugins = fluent ? [fluent] : []

return renderToString(component, {
global: {
plugins,
},
})
}
6 changes: 6 additions & 0 deletions __tests__/vue/directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ describe('directive', () => {
`),
)

const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})

const component = {
data: () => ({
name: 'John',
Expand All @@ -153,6 +155,10 @@ describe('directive', () => {

// Assert
expect(mounted.html()).toEqual('<a aria-label="Hello \u{2068}John\u{2069}">Text</a>')
expect(warn).toHaveBeenCalledTimes(1)
expect(warn).toHaveBeenCalledWith(
'[fluent-vue] Attribute \'not-allowed\' on element <a> is not localizable. Remove it from the translation. Translation key: link',
)
})

it('works without fallbacks', () => {
Expand Down
48 changes: 48 additions & 0 deletions __tests__/vue/ssr.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { beforeEach, describe, expect, it } from 'vitest'
import { isVue3 } from 'vue-demi'

import { FluentBundle, FluentResource } from '@fluent/bundle'
import ftl from '@fluent/dedent'

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

import type { FluentVue } from '../../src'
import { createFluentVue } from '../../src'

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

beforeEach(() => {
bundle = new FluentBundle('en-US')

fluent = createFluentVue({
bundles: [bundle],
})
})

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

const component = {
data: () => ({
name: 'John',
}),
template: '<a v-t:link href="/foo">Fallback text</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>')
})
})
27 changes: 27 additions & 0 deletions src/vue/directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ function translate(el: HTMLElement, fluent: TranslationContext, binding: VueDire
for (const [attr, attrValue] of Object.entries(translation.attributes)) {
if (isAttrNameLocalizable(attr, el, allowedAttrs))
el.setAttribute(attr, attrValue)
else
warn(`Attribute '${attr}' on element <${el.tagName.toLowerCase()}> is not localizable. Remove it from the translation. Translation key: ${key}`)
}
}

Expand All @@ -39,6 +41,31 @@ export function createVue3Directive(rootContext: TranslationContext): Vue3Direct
const context = getContext(rootContext, binding.instance)
translate(el, context, binding)
},

getSSRProps(binding) {
const context = getContext(rootContext, binding.instance)
const key = binding.arg
if (key === 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
}

// 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