0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-03-22 08:58:11 +00:00

trying to inject props

This commit is contained in:
Víctor Losada Hernández
2026-02-01 21:06:39 +01:00
parent b5598ac423
commit b4ec26a29f
2 changed files with 16 additions and 12 deletions

View File

@@ -2,6 +2,6 @@ import { createRoot } from "react-dom/client";
import Homebrew from "./homebrew.jsx"; import Homebrew from "./homebrew.jsx";
const props = window.__INITIAL_PROPS__ || {}; const props = window.__INITIAL_PROPS__ || {};
console.log('props: ', window.__INITIAL_PROPS__); window.onload = ()=> {console.log('props: ', window.__INITIAL_PROPS__)};
createRoot(document.getElementById("reactRoot")).render(<Homebrew {...props} />); createRoot(document.getElementById("reactRoot")).render(<Homebrew {...props} />);

View File

@@ -542,10 +542,15 @@ export default async function createApp(vite) {
return next(); return next();
})); }));
app.use((req, res, next) => {
console.log('Before SPA middleware:', req.originalUrl);
next();
});
//Send rendered page //Send rendered page
app.use(asyncHandler(async (req, res, next)=>{ app.use(asyncHandler(async (req, res, next)=>{
console.log(req.route); if(!req.route) return res.redirect('/'); // Catch-all for invalid routes
//if(!req.route) return res.redirect('/'); // Catch-all for invalid routes
const page = await renderPage(req, res); const page = await renderPage(req, res);
if(!page) return; if(!page) return;
@@ -555,7 +560,7 @@ export default async function createApp(vite) {
//Render the page //Render the page
const renderPage = async (req, res)=>{ const renderPage = async (req, res)=>{
console.log('renderpage'); console.log('renderpage');
// Create configuration object // Create configuration object
const configuration = { const configuration = {
local : isLocalEnvironment, local : isLocalEnvironment,
publicUrl : config.get('publicUrl') ?? '', publicUrl : config.get('publicUrl') ?? '',
@@ -574,24 +579,23 @@ export default async function createApp(vite) {
ogMeta : req.ogMeta, ogMeta : req.ogMeta,
userThemes : req.userThemes userThemes : req.userThemes
}; };
console.log('props: ',props); console.log('props: ',!!props);
return await renderSPA(req, props); return await renderSPA(req, props);
}; };
const renderSPA = async (req, props)=>{ const renderSPA = async (req, props)=>{
const htmlPath = isProd ? path.resolve('build', 'index.html') : path.resolve('index.html'); const htmlPath = isProd ? path.resolve('build', 'index.html') : path.resolve('index.html');
let html = fs.readFileSync(htmlPath, 'utf-8'); let html = fs.readFileSync(htmlPath, 'utf-8');
console.log('index.html snippet:', html.slice(0, 200)); // see the first 200 chars
html = html.replace(
'</head>',
`<script>window.__INITIAL_PROPS__ = ${JSON.stringify(props)}</script>\n</head>`
);
if(!isProd && vite?.transformIndexHtml) { if(!isProd && vite?.transformIndexHtml) {
console.log('transforming'); html = await vite.transformIndexHtml(req.originalUrl, html);
return await vite.transformIndexHtml(req.originalUrl, html);
} }
html = html.replace(
'<head>',
`<head><script>window.__INITIAL_PROPS__ = ${JSON.stringify(props)}</script>`
);
console.log('html', html);
return html; return html;
}; };