Commit d8fe9417 authored by Luke Sandberg's avatar Luke Sandberg

Add next-intl JSON HMR regression test for #91768

Tests JSON HMR with next-intl's getRequestConfig pattern that uses dynamic
import() with template literals to load locale messages. Covers both server
components (getTranslations) and client components (useTranslations), patching
messages/en.json multiple times to catch the "second edit ignored" regression.
Co-Authored-By: 's avatarClaude <noreply@anthropic.com>
parent 56f6145f
'use client'
import { useTranslations } from 'next-intl'
export default function IntlClientPage() {
const t = useTranslations('HomePage')
return (
<div>
<h1 id="intl-client-title">{t('title')}</h1>
</div>
)
}
import { NextIntlClientProvider } from 'next-intl'
import { getMessages } from 'next-intl/server'
import { ReactNode } from 'react'
export default async function RootLayout({
children,
}: {
children: ReactNode
}) {
const messages = await getMessages()
return (
<html lang="en">
<body>
<NextIntlClientProvider messages={messages}>
{children}
</NextIntlClientProvider>
</body>
</html>
)
}
import { getTranslations } from 'next-intl/server'
export default async function HomePage() {
const t = await getTranslations('HomePage')
return (
<div>
<h1 id="intl-title">{t('title')}</h1>
</div>
)
}
import { getRequestConfig } from 'next-intl/server'
export default getRequestConfig(async () => {
const locale = 'en'
return {
locale,
messages: (await import(`../messages/${locale}.json`)).default,
}
})
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
describe('json-hmr-next-intl', () => {
const { next, isTurbopack, isNextDev } = nextTestSetup({
files: __dirname,
dependencies: {
'next-intl': '^4.8.3',
},
})
// This regression only affects Turbopack dev mode (Server Fast Refresh)
const itTurbopackDev = isTurbopack && isNextDev ? it : it.skip
describe('server component with getTranslations', () => {
itTurbopackDev(
'reflects multiple consecutive JSON message edits via HMR',
async () => {
const browser = await next.browser('/')
// Verify initial value
await retry(async () => {
const text = await browser.elementByCss('#intl-title').text()
expect(text).toBe('Hello world')
})
const originalContent = await next.readFile('messages/en.json')
try {
// First edit — should update
await next.patchFile(
'messages/en.json',
JSON.stringify({ HomePage: { title: 'First edit' } })
)
await retry(async () => {
const text = await browser.elementByCss('#intl-title').text()
expect(text).toBe('First edit')
})
// Second edit — this is the regression: subsequent edits are ignored
await next.patchFile(
'messages/en.json',
JSON.stringify({ HomePage: { title: 'Second edit' } })
)
await retry(async () => {
const text = await browser.elementByCss('#intl-title').text()
expect(text).toBe('Second edit')
})
// Third edit — ensure continued HMR functionality
await next.patchFile(
'messages/en.json',
JSON.stringify({ HomePage: { title: 'Third edit' } })
)
await retry(async () => {
const text = await browser.elementByCss('#intl-title').text()
expect(text).toBe('Third edit')
})
} finally {
await next.patchFile('messages/en.json', originalContent)
}
}
)
})
describe('client component with useTranslations', () => {
itTurbopackDev(
'reflects multiple consecutive JSON message edits via HMR',
async () => {
const browser = await next.browser('/intl')
// Verify initial value
await retry(async () => {
const text = await browser.elementByCss('#intl-client-title').text()
expect(text).toBe('Hello world')
})
const originalContent = await next.readFile('messages/en.json')
try {
// First edit
await next.patchFile(
'messages/en.json',
JSON.stringify({ HomePage: { title: 'Client first' } })
)
await retry(async () => {
const text = await browser.elementByCss('#intl-client-title').text()
expect(text).toBe('Client first')
})
// Second edit — regression: subsequent edits ignored
await next.patchFile(
'messages/en.json',
JSON.stringify({ HomePage: { title: 'Client second' } })
)
await retry(async () => {
const text = await browser.elementByCss('#intl-client-title').text()
expect(text).toBe('Client second')
})
} finally {
await next.patchFile('messages/en.json', originalContent)
}
}
)
})
})
{
"HomePage": {
"title": "Hello world"
}
}
const createNextIntlPlugin = require('next-intl/plugin')
const withNextIntl = createNextIntlPlugin()
/** @type {import('next').NextConfig} */
const nextConfig = {}
module.exports = withNextIntl(nextConfig)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment