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

refactor: separate switchLocalePath SSR feature into plugin #3021

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
1 change: 1 addition & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export default defineNuxtModule<NuxtI18nOptions>({

// for core plugin
addPlugin(resolve(runtimeDir, 'plugins/i18n'))
addPlugin(resolve(runtimeDir, 'plugins/switch-locale-path-ssr'))

// for composables
nuxt.options.alias['#i18n'] = resolve(distDir, 'runtime/composables/index.mjs')
Expand Down
27 changes: 1 addition & 26 deletions src/runtime/plugins/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import {
isSSG,
localeLoaders,
parallelPlugin,
normalizedLocales,
SWITCH_LOCALE_PATH_LINK_IDENTIFIER
normalizedLocales
} from '#build/i18n.options.mjs'
import { loadVueI18nOptions, loadInitialMessages, loadLocale } from '../messages'
import { useSwitchLocalePath } from '../composables'
import {
loadAndSetLocale,
detectLocale,
Expand Down Expand Up @@ -401,29 +399,6 @@ export default defineNuxtPlugin({
// inject for nuxt helpers
injectNuxtHelpers(nuxtContext, i18n)

// Replace `SwitchLocalePathLink` href in rendered html for SSR support
if (runtimeI18n.experimental.switchLocalePathLinkSSR === true) {
const switchLocalePath = useSwitchLocalePath()

const switchLocalePathLinkWrapperExpr = new RegExp(
[
`<!--${SWITCH_LOCALE_PATH_LINK_IDENTIFIER}-\\[(\\w+)\\]-->`,
`.+?`,
`<!--/${SWITCH_LOCALE_PATH_LINK_IDENTIFIER}-->`
].join(''),
'g'
)

nuxt.hook('app:rendered', ctx => {
if (ctx.renderResult?.html == null) return

ctx.renderResult.html = ctx.renderResult.html.replaceAll(
switchLocalePathLinkWrapperExpr,
(match: string, p1: string) => match.replace(/href="([^"]+)"/, `href="${switchLocalePath(p1 ?? '')}"`)
)
})
}

let routeChangeCount = 0

addRouteMiddleware(
Expand Down
32 changes: 32 additions & 0 deletions src/runtime/plugins/switch-locale-path-ssr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { defineNuxtPlugin } from '#imports'
import { useSwitchLocalePath } from '#i18n'
import { SWITCH_LOCALE_PATH_LINK_IDENTIFIER } from '#build/i18n.options.mjs'

// Replace `SwitchLocalePathLink` href in rendered html for SSR support
export default defineNuxtPlugin({
name: 'i18n:plugin:switch-locale-path-ssr',
dependsOn: ['i18n:plugin'],
setup(nuxt) {
if (nuxt.$config.public.i18n.experimental.switchLocalePathLinkSSR !== true) return

const switchLocalePath = useSwitchLocalePath()

const switchLocalePathLinkWrapperExpr = new RegExp(
[
`<!--${SWITCH_LOCALE_PATH_LINK_IDENTIFIER}-\\[(\\w+)\\]-->`,
`.+?`,
`<!--/${SWITCH_LOCALE_PATH_LINK_IDENTIFIER}-->`
].join(''),
'g'
)

nuxt.hook('app:rendered', ctx => {
if (ctx.renderResult?.html == null) return

ctx.renderResult.html = ctx.renderResult.html.replaceAll(
switchLocalePathLinkWrapperExpr,
(match: string, p1: string) => match.replace(/href="([^"]+)"/, `href="${switchLocalePath(p1 ?? '')}"`)
)
})
}
})