EugeneMeles / laravel-react-i18n

Allows to connect your `Laravel` Framework translation files with `React`.
MIT License
75 stars 9 forks source link

Content showing twice using Inertia SSR with hydrateRoot and laravel-react-i18n #16

Closed TheCoati closed 1 month ago

TheCoati commented 11 months ago

When using laravel-react-i18n with Inertia SSR, the page is rendered twice using hydrateRoot.

ssr.tsx

void createServer(page => createInertiaApp({
    page,
    render: ReactDOMServer.renderToString,
    resolve: name => {
        const pages = import.meta.glob('./pages/**/*.tsx', { eager: true })
        return pages[`./pages/${name}.tsx`]
    },
    setup: ({App, props}) => {
        return (
            <LaravelReactI18nProvider
                    locale={'en'}
                    fallbackLocale={'en'}
                    files={import.meta.glob('/lang/*.json', { eager: true })}>
                <App {...props} />
            </LaravelReactI18nProvider>
        )
    }
}))

app.tsx

void createInertiaApp({
    resolve: name => {
        const pages = import.meta.glob('./pages/**/*.tsx', { eager: true })
        return pages[`./pages/${name}.tsx`]
    },
    setup({ el, App, props }) {
        hydrateRoot(el, <LaravelReactI18nProvider
                locale={'en'}
                fallbackLocale={'en'}
                files={import.meta.glob('/lang/*.json')}>
            <App {...props} />
        </LaravelReactI18nProvider>)
    },
})

home.tsx

export default function HomePage(): ReactElement {
    return (
        <div>
            Test
        </div>
    )
}

Restults in: image


After removing the <LaravelReactI18nProvider>, the text Test correctly renders only once, as intended.

saullo commented 5 months ago

Any ideias? Same issue here

image

wescopeland commented 2 months ago

I resorted to forking the project to resolve this issue.

The bug is in provider.ts. If you want to fix your own project, copy the changes in this file: https://github.com/RetroAchievements/RAWeb/pull/2723/files/b9644c05d1f193032d15f37277b1d88535047e50#diff-f7d458b3d1d0014c6c7114fd9fe84f22424ecd60dca8404f506ad0bc218c6ad7

EugeneMeles commented 1 month ago

fixed at 2.0.5