Skip to content

Commit

Permalink
refactor: separate switchLocalePath SSR into plugin (nuxt-modules#3021)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieGoede committed Jul 26, 2024
1 parent 856ba4f commit 6b69f75
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,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 ?? '')}"`)
)
})
}
})

0 comments on commit 6b69f75

Please sign in to comment.