From b239de0445a1d15c0e30c3c210554334ab1e327d Mon Sep 17 00:00:00 2001 From: onmax Date: Sun, 25 Oct 2026 20:48:32 +0100 Subject: [PATCH] perf: skip the switch-locale-path html scan without a link --- src/runtime/components/SwitchLocalePathLink.ts | 4 ++++ src/runtime/plugins/switch-locale-path-ssr.ts | 4 +++- src/runtime/shared/switch-locale-path.ts | 12 ++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 src/runtime/shared/switch-locale-path.ts diff --git a/src/runtime/components/SwitchLocalePathLink.ts b/src/runtime/components/SwitchLocalePathLink.ts index 0bcad72ae..bf2eb503c 100644 --- a/src/runtime/components/SwitchLocalePathLink.ts +++ b/src/runtime/components/SwitchLocalePathLink.ts @@ -5,6 +5,7 @@ import { hasProtocol } from 'ufo' import { nuxtLinkDefaults } from '#build/nuxt.config.mjs' import { useComposableContext } from '../composable-context' import { useNuxtI18nContext } from '../context' +import { markSwitchLocalePathLink } from '../shared/switch-locale-path' import type { PropType } from 'vue' @@ -68,6 +69,9 @@ export default defineComponent({ }, inheritAttrs: false, setup(props, { slots, attrs }) { + if (import.meta.server) { + markSwitchLocalePathLink(useNuxtApp().ssrContext) + } return () => [ h(Comment, `${__SWITCH_LOCALE_PATH_LINK_IDENTIFIER__}-[${props.locale}]`), h(SlpComponent, { ...attrs, ...props }, slots.default), diff --git a/src/runtime/plugins/switch-locale-path-ssr.ts b/src/runtime/plugins/switch-locale-path-ssr.ts index 4462b9f8a..bd1a34b6e 100644 --- a/src/runtime/plugins/switch-locale-path-ssr.ts +++ b/src/runtime/plugins/switch-locale-path-ssr.ts @@ -1,6 +1,7 @@ import { defineNuxtPlugin, useNuxtApp } from '#imports' import { useSwitchLocalePath } from '#i18n' import { escapeHtmlAttr } from '../shared/utils' +import { hasSwitchLocalePathLink } from '../shared/switch-locale-path' const identifier = __SWITCH_LOCALE_PATH_LINK_IDENTIFIER__ const switchLocalePathLinkWrapperExpr = new RegExp( @@ -17,7 +18,8 @@ export default defineNuxtPlugin({ const nuxt = useNuxtApp(_nuxt._id) const switchLocalePath = useSwitchLocalePath(nuxt) nuxt.hook('app:rendered', (ctx) => { - if (ctx.renderResult?.html == null) { return } + // scanning the whole html costs time on every large page, so skip renders without a link + if (ctx.renderResult?.html == null || !hasSwitchLocalePathLink(ctx.ssrContext)) { return } ctx.renderResult.html = ctx.renderResult.html.replaceAll( switchLocalePathLinkWrapperExpr, diff --git a/src/runtime/shared/switch-locale-path.ts b/src/runtime/shared/switch-locale-path.ts new file mode 100644 index 000000000..ea4b02789 --- /dev/null +++ b/src/runtime/shared/switch-locale-path.ts @@ -0,0 +1,12 @@ +import type { NuxtSSRContext } from '#app' + +// server renders that contain a `SwitchLocalePathLink`, so only those pay for the html rewrite +const renders = new WeakSet() + +export function markSwitchLocalePathLink(ssrContext: NuxtSSRContext | undefined) { + if (ssrContext) { renders.add(ssrContext) } +} + +export function hasSwitchLocalePathLink(ssrContext: NuxtSSRContext | undefined) { + return !!ssrContext && renders.has(ssrContext) +}