difficulty hydrating client-side React code #2550
Unanswered
Nicholas1K
asked this question in
Help & Questions
Replies: 1 comment
-
Have you seen https://vike.dev/hydration-mismatch? I believe this is your issue? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone, I need your help because I'm unable to hydrate my client-side code correctly. To be clear, I can see what's rendered server-side (SSR), but what should be rendered client-side, meaning JavaScript functions, useState, useEffect, etc., I cannot see.
the structure of the code is as follows, for privacy reasons the main folder has a fictitious name:
Project-folder
├── package-lock.json
├── package.json
├── pages
│ ├── +client.jsx
│ ├── +config.js
│ ├── +onRenderHtml.jsx
│ └── index
│ └── +Page.jsx
├── public
│ ├── favicon.png
│ └── vite.svg
├── README.md
├── server
│ └── index.js
├── src
│ ├── App.css
│ ├── App.jsx
│ ├── components
│ │ ├── footer
│ │ ├── form
│ │ ├── navbar
│ ├── main.jsx
│ ├── pages
│ │ ├── auth
│ │ ├── cart
│ │ ├── contact
│ │ ├── error
│ │ ├── Home.jsx
│ │ ├── order
│ │ ├── profile
│ │ └── shop
│ ├── redux
│ │ ├── actionCreator
│ │ ├── actions
│ │ ├── reducers
│ │ └── store.js
│ ├── styles
│ │ ├── components
│ │ ├── pages
│ │ └── theme.css
│ └── utility
│ ├── Toolbox.jsx
│ └── Utils.jsx
└── vite.config.js
The contents of the file +client.jsx are as follows:import React from 'react'
import { createRoot, hydrateRoot } from 'react-dom/client'
import { Provider } from 'react-redux'
import { PersistGate } from 'redux-persist/es/integration/react.js'
import { createReduxStore } from '../src/redux/store'
export function render(pageContext) {
console.log('🚀 client render start')
const { Page, pageProps} = pageContext
const meta = pageContext.config.meta || {}
const container = document.getElementById('page-view')
if (!container) throw new Error('Elemento #page-view non trovato nel DOM')
const { store, persistor } = createReduxStore(false, pageContext.preloadedState); // client-side
const page = (
<PersistGate loading={
<Page {...pageProps} meta={meta} />
)
if (!window.__vike_hydrated) {
hydrateRoot(container, page)
window.__vike_hydrated = true
} else {
createRoot(container).render(page)
}
}
The contents of the file +config.js are as follows: import client from './+client'
export default {
clientRouting: true,
hydrationCanBeAborted: true,
client,
passToClient: ['Page', 'pageProps', 'meta', 'preloadedState']
}
The contents of the file +onRenderHtml.jsx are as follows: import { escapeInject, dangerouslySkipEscape } from 'vike';
import { renderToString } from 'react-dom/server';
import { Provider } from 'react-redux';
import { createReduxStore } from '../src/redux/store';
// Usa escapeInject per creare una stringa HTML sicura
export function onRenderHtml(pageContext) {
const {Page} = pageContext;
if (!Page) throw new Error('Page component is undefined')
const { store } = createReduxStore(true) // pass
true
per indicare che è lato serverconst preloadedState = store.getState()
//Renderizzo componente React in una stringa Html
const pageHtml = renderToString(
)
// Recuper valori meta per indicizzare la pagina x SEO
const meta = pageContext.config.meta || {}
const title = meta.title?.text || 'Default Title'
const description = meta.description?.text || 'Default Description'
// Usa escapeInject per creare l'HTML in modo sicuro
const documentHtml = escapeInject`
<title>${title}</title>
${dangerouslySkipEscape(pageHtml)}
`;
return {
documentHtml,
pageContext: {
meta,
store,
preloadedState
},
injectFilter: () => true
}
// Restituisci un oggetto con documentHtml
}
The contents of the file +Page.jsx are as follows: import React from 'react'
import Home from '../../src/pages/Home'
export default function Page() {
return (
)
}
export const meta = {
title: {
text: 'Home | Shop',
env: { server: true, client: true }
},
description: {
text: 'Benvenuto ',
env: { server: true, client: true }
}
}
inspecting the page looking at the index file in sources I see the +client.jsx file inside the script :
Assapora l’eccellenza
artigianale
Ogni assaggio è un momento da vivere:
gustalo da solo o condividilo con chi ami.
<script type="module" async>
import {injectIntoGlobalHook} from "/@react-refresh";
injectIntoGlobalHook(window);
window.$RefreshReg$ = () => {}
;
window.$RefreshSig$ = () => (type) => type;
import("/@vite/client");
import("/@fs/Users/nameusers/MyDesk/folder_work/name-project/pages/+client.jsx");
</script>
please help me I'm at a dead end where I can only see the server side rendered html but I can't see the client and I haven't found anything in the documentation that explains how to solve this problem, thanks everyone
Beta Was this translation helpful? Give feedback.
All reactions