Skip to content

Commit 1ddacbf

Browse files
committed
feat: attempt to fix error 27
1 parent 0393f17 commit 1ddacbf

File tree

5 files changed

+30
-10
lines changed

5 files changed

+30
-10
lines changed

apps/frontend/nuxt.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ export default defineNuxtConfig({
320320
compatibilityDate: '2025-01-01',
321321
telemetry: false,
322322
experimental: {
323-
asyncContext: isProduction(),
323+
asyncContext: false,
324324
},
325325
sourcemap: { client: 'hidden' },
326326
sentry: {

apps/frontend/src/utils/analytics.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import dayjs from 'dayjs'
22
import { computed, ref, watch } from 'vue'
3-
import { useI18n } from 'vue-i18n'
43

54
// note: build step can miss unix import for some reason, so
65
// we have to import it like this
76

87
const { unix } = dayjs
98

109
export function useCountryNames(style = 'long') {
11-
const { locale } = useI18n()
10+
const { $i18n } = useNuxtApp()
11+
const locale = $i18n.locale
1212
const displayNames = computed(
1313
() => new Intl.DisplayNames([locale.value], { type: 'region', style }),
1414
)

packages/ui/src/components/base/IntlFormatted.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
<script setup lang="ts">
22
import IntlMessageFormat, { type FormatXMLElementFn, type PrimitiveType } from 'intl-messageformat'
33
import { computed, useSlots, type VNode } from 'vue'
4-
import { useI18n } from 'vue-i18n'
54
6-
import type { MessageDescriptor } from '../../composables/i18n'
5+
import { getSafeI18n, type MessageDescriptor } from '../../composables/i18n'
76
87
const props = defineProps<{
98
messageId: MessageDescriptor
109
values?: Record<string, PrimitiveType>
1110
}>()
1211
1312
const slots = useSlots()
14-
const { t, locale } = useI18n()
13+
const { t, locale } = getSafeI18n()
1514
1615
const formattedParts = computed(() => {
1716
const key = props.messageId.id

packages/ui/src/composables/how-ago.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { computed, type ComputedRef } from 'vue'
2-
import { useI18n } from 'vue-i18n'
2+
3+
import { getSafeI18n } from './i18n'
34

45
export type Formatter = (value: Date | number, options?: FormatOptions) => string
56

@@ -10,7 +11,7 @@ export interface FormatOptions {
1011
const formatters = new Map<string, ComputedRef<Intl.RelativeTimeFormat>>()
1112

1213
export function useRelativeTime(): Formatter {
13-
const { locale } = useI18n()
14+
const { locale } = getSafeI18n()
1415

1516
const formatterRef = computed(
1617
() =>

packages/ui/src/composables/i18n.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
import IntlMessageFormat from 'intl-messageformat'
22
import type { Ref } from 'vue'
3-
import type { CompileError, MessageCompiler, MessageContext } from 'vue-i18n'
3+
import type { CompileError, Composer, MessageCompiler, MessageContext } from 'vue-i18n'
44
import { useI18n } from 'vue-i18n'
55

6+
/**
7+
* Get i18n instance, preferring Nuxt's $i18n to avoid vue-i18n's
8+
* getCurrentInstance() issues on edge runtimes with concurrent SSR requests.
9+
*/
10+
export function getSafeI18n(): Pick<Composer, 't' | 'locale'> {
11+
// Try Nuxt's $i18n first (avoids Error 27 on Cloudflare Workers)
12+
if (typeof useNuxtApp !== 'undefined') {
13+
try {
14+
const { $i18n } = useNuxtApp()
15+
if ($i18n) {
16+
return { t: $i18n.t, locale: $i18n.locale }
17+
}
18+
} catch {
19+
// Not in Nuxt context, fall through
20+
}
21+
}
22+
// Fallback to vue-i18n's useI18n
23+
return useI18n()
24+
}
25+
626
export interface MessageDescriptor {
727
id: string
828
defaultMessage?: string
@@ -174,7 +194,7 @@ export interface VIntlFormatters {
174194
* Uses vue-i18n's useI18n() under the hood.
175195
*/
176196
export function useVIntl(): VIntlFormatters & { locale: Ref<string> } {
177-
const { t, locale } = useI18n()
197+
const { t, locale } = getSafeI18n()
178198

179199
function formatMessage(descriptor: MessageDescriptor, values?: Record<string, unknown>): string {
180200
const key = descriptor.id

0 commit comments

Comments
 (0)