-
-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: experimental feature
switchLocalePathLinkSSR
with `<SwitchLoc…
…alePathLink>` component (#2838) * feat: `switchLocalePath` ssr * feat: experimental `switchLocalePathLink` SSR component * refactor: use constant for `SwitchLocalePathLinkSSR` component retrieval * test: assert `switchLocalePathLinkSSR` functionality * docs: add documentation for `switchLocalePathLink` component and config * refactor: locale in wrapper comment to reduce regex matching
- Loading branch information
1 parent
3cf8538
commit df92c6a
Showing
18 changed files
with
211 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { test, expect } from 'vitest' | ||
import { fileURLToPath } from 'node:url' | ||
import { $fetch, setup } from '../utils' | ||
import { getDom, gotoPath, renderPage, waitForURL } from '../helper' | ||
|
||
await setup({ | ||
rootDir: fileURLToPath(new URL(`../fixtures/basic_usage`, import.meta.url)), | ||
browser: true, | ||
// prerender: true, | ||
// overrides | ||
nuxtConfig: { | ||
runtimeConfig: { | ||
public: { | ||
i18n: { | ||
baseUrl: '' | ||
} | ||
} | ||
}, | ||
i18n: { | ||
experimental: { | ||
switchLocalePathLinkSSR: true | ||
} | ||
} | ||
} | ||
}) | ||
|
||
describe('experimental.switchLocalePathLinkSSR', async () => { | ||
test('dynamic parameters render and update reactively client-side', async () => { | ||
const { page } = await renderPage('/products/big-chair') | ||
|
||
expect(await page.locator('#switch-locale-path-link-nl').getAttribute('href')).toEqual('/nl/products/grote-stoel') | ||
|
||
await gotoPath(page, '/nl/products/rode-mok') | ||
expect(await page.locator('#switch-locale-path-link-en').getAttribute('href')).toEqual('/products/red-mug') | ||
|
||
// Translated params are not lost on query changes | ||
await page.locator('#params-add-query').click() | ||
await waitForURL(page, '/nl/products/rode-mok?test=123') | ||
expect(await page.locator('#switch-locale-path-link-en').getAttribute('href')).toEqual('/products/red-mug?test=123') | ||
|
||
await page.locator('#params-remove-query').click() | ||
await waitForURL(page, '/nl/products/rode-mok') | ||
expect(await page.locator('#switch-locale-path-link-en').getAttribute('href')).toEqual('/products/red-mug') | ||
}) | ||
|
||
test('dynamic parameters rendered correctly during SSR', async () => { | ||
// head tags - alt links are updated server side | ||
const product1Html = await $fetch('/products/big-chair') | ||
const product1Dom = getDom(product1Html) | ||
expect(product1Dom.querySelector('#i18n-alt-nl').href).toEqual('/nl/products/grote-stoel') | ||
expect(product1Dom.querySelector('#switch-locale-path-link-nl').href).toEqual('/nl/products/grote-stoel') | ||
|
||
const product2Html = await $fetch('/nl/products/rode-mok') | ||
const product2dom = getDom(product2Html) | ||
expect(product2dom.querySelector('#i18n-alt-en').href).toEqual('/products/red-mug') | ||
expect(product2dom.querySelector('#switch-locale-path-link-en').href).toEqual('/products/red-mug') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { SWITCH_LOCALE_PATH_LINK_IDENTIFIER } from '#build/i18n.options.mjs' | ||
import { useSwitchLocalePath } from '#i18n' | ||
import { defineNuxtLink } from '#imports' | ||
import { Comment, defineComponent, h } from 'vue' | ||
|
||
import type { PropType } from 'vue' | ||
|
||
const NuxtLink = defineNuxtLink({ componentName: 'NuxtLink' }) | ||
|
||
export default defineComponent({ | ||
name: 'SwitchLocalePathLink', | ||
props: { | ||
locale: { | ||
type: String as PropType<string>, | ||
required: true | ||
} | ||
}, | ||
inheritAttrs: false, | ||
setup(props, { slots, attrs }) { | ||
const switchLocalePath = useSwitchLocalePath() | ||
|
||
return () => [ | ||
h(Comment, `${SWITCH_LOCALE_PATH_LINK_IDENTIFIER}-[${props.locale}]`), | ||
h(NuxtLink, { ...attrs, to: switchLocalePath(props.locale) }, slots.default), | ||
h(Comment, `/${SWITCH_LOCALE_PATH_LINK_IDENTIFIER}`) | ||
] | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.