frontend-hamroun 1.2.80 → 1.2.82
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/cli.js +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/templates/basic-app/src/App.jsx +16 -0
- package/templates/basic-app/src/client.jsx +5 -0
- package/templates/basic-app/src/components/Counter.jsx +13 -0
- package/templates/basic-app/src/jsx-shim.js +3 -0
- package/templates/basic-app/src/jsx-shim.ts +7 -0
- package/templates/basic-app/src/main.jsx +98 -0
- package/templates/basic-app/src/server.js +47 -0
- package/templates/complete-app/api/hello.js +0 -0
- package/templates/complete-app/lib/frontend-hamroun.js +182 -0
- package/templates/complete-app/package.json +18 -0
- package/templates/complete-app/pages/about.js +119 -0
- package/templates/complete-app/pages/about.jsx +0 -0
- package/templates/complete-app/pages/index.js +157 -0
- package/templates/complete-app/pages/index.jsx +0 -0
- package/templates/complete-app/pages/wasm-demo.js +290 -0
- package/templates/complete-app/pages/wasm-demo.jsx +0 -0
- package/templates/complete-app/public/client.js +89 -0
- package/templates/complete-app/public/index.html +118 -0
- package/templates/complete-app/public/styles.css +76 -0
- package/templates/complete-app/server.js +226 -0
- package/templates/complete-app/src/App.tsx +59 -0
- package/templates/complete-app/src/client.tsx +18 -0
- package/templates/complete-app/src/server.ts +218 -0
- package/templates/complete-app/tsconfig.json +22 -0
- package/templates/complete-app/tsconfig.server.json +19 -0
- package/templates/{ssr-template → complete-app}/vite.config.js +16 -5
- package/templates/complete-app/vite.config.ts +30 -0
- package/templates/complete-app/wasm/build.bat +0 -0
- package/templates/complete-app/wasm/build.sh +0 -0
- package/templates/complete-app/wasm/example.go +0 -0
- package/templates/fullstack-app/build/main.css +874 -874
- package/templates/fullstack-app/build/main.css.map +7 -7
- package/templates/fullstack-app/build/main.js +996 -967
- package/templates/fullstack-app/build/main.js.map +7 -7
- package/templates/fullstack-app/package-lock.json +6301 -0
- package/templates/fullstack-app/public/styles.css +768 -768
- package/templates/go/example.go +154 -99
- package/templates/ssr-template/esbuild.config.js +33 -0
- package/templates/ssr-template/jsx-shim.js +1 -0
- package/templates/ssr-template/package.json +22 -16
- package/templates/ssr-template/src/App.tsx +12 -52
- package/templates/ssr-template/src/client.tsx +3 -17
- package/templates/ssr-template/src/server.ts +21 -204
- package/templates/ssr-template/tsconfig.json +10 -13
- package/templates/ssr-template/tsconfig.server.json +6 -14
- package/templates/wasm/build-wasm.js +228 -0
- package/templates/wasm/esbuild.config.js +63 -0
- package/templates/wasm/go/main.go +256 -0
- package/templates/wasm/go/wasm_exec.js +0 -0
- package/templates/wasm/index.html +97 -0
- package/templates/wasm/jsx-shim.js +9 -0
- package/templates/wasm/package-lock.json +5307 -0
- package/templates/wasm/package.json +42 -0
- package/templates/wasm/public/example.wasm +0 -0
- package/templates/{go-wasm-app/public/wasm → wasm/public}/wasm_exec.js +561 -561
- package/templates/wasm/src/App.tsx +564 -0
- package/templates/wasm/src/client.tsx +220 -0
- package/templates/wasm/src/index.tsx +21 -0
- package/templates/wasm/src/server.ts +145 -0
- package/templates/wasm/tsconfig.json +21 -0
- package/templates/wasm/tsconfig.node.json +13 -0
- package/templates/wasm/tsconfig.server.json +23 -0
- package/templates/wasm/vite.config.ts +56 -0
- package/templates/wasm/wasm-loader.js +103 -0
- package/templates/basic-app/bun.lock +0 -196
- package/templates/basic-app/docs/rapport_pfe.aux +0 -27
- package/templates/basic-app/docs/rapport_pfe.out +0 -10
- package/templates/basic-app/docs/rapport_pfe.pdf +0 -0
- package/templates/basic-app/docs/rapport_pfe.tex +0 -68
- package/templates/basic-app/docs/rapport_pfe.toc +0 -14
- package/templates/basic-app/package-lock.json +0 -4185
- package/templates/go-wasm-app/README.md +0 -38
- package/templates/go-wasm-app/babel.config.js +0 -15
- package/templates/go-wasm-app/build-client.js +0 -49
- package/templates/go-wasm-app/build-wasm.js +0 -237
- package/templates/go-wasm-app/package.json +0 -23
- package/templates/go-wasm-app/public/index.html +0 -128
- package/templates/go-wasm-app/public/styles.css +0 -197
- package/templates/go-wasm-app/public/wasm/example.wasm +0 -0
- package/templates/go-wasm-app/public/wasm/wasm_exec_node.js +0 -39
- package/templates/go-wasm-app/server.js +0 -521
- package/templates/go-wasm-app/src/App.jsx +0 -38
- package/templates/go-wasm-app/src/app.js +0 -153
- package/templates/go-wasm-app/src/client.js +0 -57
- package/templates/go-wasm-app/src/components/Footer.jsx +0 -13
- package/templates/go-wasm-app/src/components/Header.jsx +0 -19
- package/templates/go-wasm-app/src/components/WasmDemo.jsx +0 -120
- package/templates/go-wasm-app/src/main.jsx +0 -12
- package/templates/go-wasm-app/src/wasm/example.go +0 -75
- package/templates/go-wasm-app/tsconfig.server.json +0 -18
- package/templates/go-wasm-app/vite.config.js +0 -34
- package/templates/ssr-template/package-lock.json +0 -2478
- package/templates/ssr-template/public/index.html +0 -47
- package/templates/ssr-template/server.js +0 -369
- /package/templates/{ssr-template → complete-app}/client.js +0 -0
- /package/templates/{ssr-template → complete-app}/readme.md +0 -0
- /package/templates/{ssr-template → complete-app}/server.ts +0 -0
- /package/templates/{ssr-template → complete-app}/src/client.ts +0 -0
- /package/templates/{ssr-template → complete-app}/src/pages/index.tsx +0 -0
@@ -0,0 +1,118 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html lang="en">
|
3
|
+
<head>
|
4
|
+
<meta charset="UTF-8">
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6
|
+
<title>Static Fallback - Frontend Hamroun</title>
|
7
|
+
<link href="/styles.css" rel="stylesheet" type="text/css">
|
8
|
+
<style>
|
9
|
+
body {
|
10
|
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
11
|
+
background-color: #f8f9fa;
|
12
|
+
color: #212529;
|
13
|
+
line-height: 1.5;
|
14
|
+
margin: 0;
|
15
|
+
padding: 0;
|
16
|
+
display: flex;
|
17
|
+
flex-direction: column;
|
18
|
+
min-height: 100vh;
|
19
|
+
}
|
20
|
+
.container {
|
21
|
+
max-width: 800px;
|
22
|
+
margin: 0 auto;
|
23
|
+
padding: 2rem;
|
24
|
+
flex: 1;
|
25
|
+
display: flex;
|
26
|
+
flex-direction: column;
|
27
|
+
justify-content: center;
|
28
|
+
}
|
29
|
+
.alert {
|
30
|
+
background-color: #f8d7da;
|
31
|
+
color: #721c24;
|
32
|
+
border: 1px solid #f5c6cb;
|
33
|
+
border-radius: 4px;
|
34
|
+
padding: 1.5rem;
|
35
|
+
margin-bottom: 2rem;
|
36
|
+
}
|
37
|
+
.warning {
|
38
|
+
background-color: #fff3cd;
|
39
|
+
color: #856404;
|
40
|
+
border: 1px solid #ffeeba;
|
41
|
+
border-radius: 4px;
|
42
|
+
padding: 1.5rem;
|
43
|
+
margin-bottom: 2rem;
|
44
|
+
}
|
45
|
+
.steps {
|
46
|
+
background-color: #e9ecef;
|
47
|
+
border-radius: 4px;
|
48
|
+
padding: 1.5rem;
|
49
|
+
}
|
50
|
+
h1 {
|
51
|
+
margin-top: 0;
|
52
|
+
color: #0066cc;
|
53
|
+
}
|
54
|
+
footer {
|
55
|
+
text-align: center;
|
56
|
+
padding: 1rem;
|
57
|
+
background-color: #f1f1f1;
|
58
|
+
font-size: 0.875rem;
|
59
|
+
color: #6c757d;
|
60
|
+
}
|
61
|
+
code {
|
62
|
+
background-color: #f1f1f1;
|
63
|
+
padding: 0.2rem 0.4rem;
|
64
|
+
border-radius: 3px;
|
65
|
+
}
|
66
|
+
.logo {
|
67
|
+
font-size: 2rem;
|
68
|
+
margin-bottom: 2rem;
|
69
|
+
color: #0066cc;
|
70
|
+
font-weight: bold;
|
71
|
+
text-align: center;
|
72
|
+
}
|
73
|
+
ul {
|
74
|
+
margin-bottom: 0;
|
75
|
+
}
|
76
|
+
li {
|
77
|
+
margin-bottom: 0.5rem;
|
78
|
+
}
|
79
|
+
li:last-child {
|
80
|
+
margin-bottom: 0;
|
81
|
+
}
|
82
|
+
</style>
|
83
|
+
</head>
|
84
|
+
<body>
|
85
|
+
<div class="container">
|
86
|
+
<div class="logo">Frontend Hamroun</div>
|
87
|
+
|
88
|
+
<div class="alert">
|
89
|
+
<h1>STATIC INDEX.HTML FILE</h1>
|
90
|
+
<p><strong>You should NOT be seeing this page if the server is running correctly.</strong></p>
|
91
|
+
<p>This is a static file that should only be used as a fallback when the server is not running.</p>
|
92
|
+
</div>
|
93
|
+
|
94
|
+
<div class="warning">
|
95
|
+
<p>If you're seeing this while running <code>npm run dev</code>, there's likely an issue with the Express route handling.</p>
|
96
|
+
<p>Try clearing your browser cache and refreshing the page.</p>
|
97
|
+
</div>
|
98
|
+
|
99
|
+
<div class="steps">
|
100
|
+
<h2>Troubleshooting Steps:</h2>
|
101
|
+
<ul>
|
102
|
+
<li>Check your terminal for any error messages</li>
|
103
|
+
<li>Verify that you've installed all dependencies with <code>npm install</code></li>
|
104
|
+
<li>Check that the server port (default: 3000) is not already in use</li>
|
105
|
+
<li>Make sure your Express routes are configured correctly</li>
|
106
|
+
<li>Try restarting the server with <code>npm run dev</code></li>
|
107
|
+
<li>Check that all required environment variables are set</li>
|
108
|
+
</ul>
|
109
|
+
</div>
|
110
|
+
</div>
|
111
|
+
|
112
|
+
<footer class="footer">
|
113
|
+
<div class="container">
|
114
|
+
<p>© 2025 Frontend Hamroun - A lightweight full-stack JavaScript framework</p>
|
115
|
+
</div>
|
116
|
+
</footer>
|
117
|
+
</body>
|
118
|
+
</html>
|
@@ -0,0 +1,76 @@
|
|
1
|
+
/* Color scheme */
|
2
|
+
:root {
|
3
|
+
--primary-color: #0066cc;
|
4
|
+
--primary-hover: #004c99;
|
5
|
+
--secondary-color: #6c757d;
|
6
|
+
--light-bg: #f8f9fa;
|
7
|
+
--light-border: #dee2e6;
|
8
|
+
--dark-text: #212529;
|
9
|
+
}
|
10
|
+
|
11
|
+
body {
|
12
|
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
|
13
|
+
background-color: var(--light-bg);
|
14
|
+
color: var(--dark-text);
|
15
|
+
line-height: 1.5;
|
16
|
+
margin: 0;
|
17
|
+
padding: 0;
|
18
|
+
}
|
19
|
+
|
20
|
+
.container {
|
21
|
+
max-width: 1140px;
|
22
|
+
margin: 0 auto;
|
23
|
+
padding: 2rem 1rem;
|
24
|
+
}
|
25
|
+
|
26
|
+
.logo {
|
27
|
+
font-size: 2rem;
|
28
|
+
font-weight: bold;
|
29
|
+
color: var(--primary-color);
|
30
|
+
margin-bottom: 1.5rem;
|
31
|
+
}
|
32
|
+
|
33
|
+
h1, h2, h3 {
|
34
|
+
margin-top: 0;
|
35
|
+
margin-bottom: 1rem;
|
36
|
+
}
|
37
|
+
|
38
|
+
ul {
|
39
|
+
padding-left: 1.5rem;
|
40
|
+
}
|
41
|
+
|
42
|
+
li {
|
43
|
+
margin-bottom: 0.5rem;
|
44
|
+
}
|
45
|
+
|
46
|
+
.button, button {
|
47
|
+
display: inline-block;
|
48
|
+
font-weight: 400;
|
49
|
+
color: #fff;
|
50
|
+
text-align: center;
|
51
|
+
vertical-align: middle;
|
52
|
+
background-color: var(--primary-color);
|
53
|
+
border: 1px solid var(--primary-color);
|
54
|
+
padding: 0.375rem 0.75rem;
|
55
|
+
font-size: 1rem;
|
56
|
+
line-height: 1.5;
|
57
|
+
border-radius: 0.25rem;
|
58
|
+
cursor: pointer;
|
59
|
+
text-decoration: none;
|
60
|
+
transition: background-color 0.15s ease-in-out;
|
61
|
+
}
|
62
|
+
|
63
|
+
.button:hover, button:hover {
|
64
|
+
background-color: var(--primary-hover);
|
65
|
+
border-color: var(--primary-hover);
|
66
|
+
text-decoration: none;
|
67
|
+
}
|
68
|
+
|
69
|
+
.footer {
|
70
|
+
padding: 1.5rem 0;
|
71
|
+
margin-top: 3rem;
|
72
|
+
background-color: #f1f1f1;
|
73
|
+
color: var(--secondary-color);
|
74
|
+
text-align: center;
|
75
|
+
font-size: 0.875rem;
|
76
|
+
}
|
@@ -0,0 +1,226 @@
|
|
1
|
+
import express from 'express';
|
2
|
+
import path from 'path';
|
3
|
+
import { fileURLToPath } from 'url';
|
4
|
+
import fs from 'fs';
|
5
|
+
// Fix imports by using only the exports that exist
|
6
|
+
import {
|
7
|
+
renderToString,
|
8
|
+
jsx,
|
9
|
+
requestLogger,
|
10
|
+
errorHandler,
|
11
|
+
notFoundHandler,
|
12
|
+
rateLimit
|
13
|
+
} from './lib/frontend-hamroun.js';
|
14
|
+
import dotenv from 'dotenv';
|
15
|
+
import compression from 'compression';
|
16
|
+
import cors from 'cors';
|
17
|
+
|
18
|
+
// Load environment variables
|
19
|
+
dotenv.config();
|
20
|
+
|
21
|
+
// Get __dirname equivalent in ESM
|
22
|
+
const __filename = fileURLToPath(import.meta.url);
|
23
|
+
const __dirname = path.dirname(__filename);
|
24
|
+
|
25
|
+
// Create Express app
|
26
|
+
const app = express();
|
27
|
+
const PORT = process.env.PORT || 3000;
|
28
|
+
|
29
|
+
// Middleware
|
30
|
+
app.use(compression()); // Compress responses
|
31
|
+
app.use(express.json()); // Parse JSON requests
|
32
|
+
app.use(express.urlencoded({ extended: true })); // Parse URL-encoded requests
|
33
|
+
app.use(cors()); // Enable CORS
|
34
|
+
app.use(requestLogger); // Log all requests
|
35
|
+
|
36
|
+
// Add rate limiting to prevent abuse
|
37
|
+
app.use(rateLimit({
|
38
|
+
windowMs: 15 * 60 * 1000, // 15 minutes
|
39
|
+
max: 100 // limit each IP to 100 requests per windowMs
|
40
|
+
}));
|
41
|
+
|
42
|
+
// Serve static files from the public directory
|
43
|
+
// Important: This middleware should be defined BEFORE your catch-all route
|
44
|
+
app.use(express.static(path.join(__dirname, 'public')));
|
45
|
+
|
46
|
+
// Add correct MIME type for client.js module
|
47
|
+
app.get('/client.js', (req, res) => {
|
48
|
+
res.type('application/javascript').sendFile(path.join(__dirname, 'public', 'client.js'));
|
49
|
+
});
|
50
|
+
|
51
|
+
// Serve WebAssembly files with correct MIME type
|
52
|
+
app.get('*.wasm', (req, res, next) => {
|
53
|
+
res.type('application/wasm');
|
54
|
+
next();
|
55
|
+
});
|
56
|
+
|
57
|
+
// Add API routes example
|
58
|
+
app.get('/api/hello', (req, res) => {
|
59
|
+
res.json({
|
60
|
+
message: 'Hello from the API!',
|
61
|
+
serverTime: new Date().toISOString()
|
62
|
+
});
|
63
|
+
});
|
64
|
+
|
65
|
+
// Find the pages directory
|
66
|
+
const getPagesDirectory = () => {
|
67
|
+
return path.join(__dirname, 'pages');
|
68
|
+
};
|
69
|
+
|
70
|
+
// Helper to check if a file exists
|
71
|
+
const fileExists = async (filePath) => {
|
72
|
+
try {
|
73
|
+
await fs.promises.access(filePath);
|
74
|
+
return true;
|
75
|
+
} catch {
|
76
|
+
return false;
|
77
|
+
}
|
78
|
+
};
|
79
|
+
|
80
|
+
// Map URL path to component path
|
81
|
+
const getComponentPath = async (urlPath) => {
|
82
|
+
const pagesDir = getPagesDirectory();
|
83
|
+
|
84
|
+
// Handle root path
|
85
|
+
if (urlPath === '/') {
|
86
|
+
const indexPath = path.join(pagesDir, 'index.js');
|
87
|
+
if (await fileExists(indexPath)) {
|
88
|
+
return {
|
89
|
+
componentPath: indexPath,
|
90
|
+
params: {}
|
91
|
+
};
|
92
|
+
}
|
93
|
+
}
|
94
|
+
|
95
|
+
// Try direct match (e.g., /about -> /pages/about.js)
|
96
|
+
const possibleExtensions = ['.js'];
|
97
|
+
|
98
|
+
for (const ext of possibleExtensions) {
|
99
|
+
const directPath = path.join(pagesDir, `${urlPath.slice(1)}${ext}`);
|
100
|
+
if (await fileExists(directPath)) {
|
101
|
+
return {
|
102
|
+
componentPath: directPath,
|
103
|
+
params: {}
|
104
|
+
};
|
105
|
+
}
|
106
|
+
}
|
107
|
+
|
108
|
+
// Try directory index (e.g., /about -> /pages/about/index.js)
|
109
|
+
for (const ext of possibleExtensions) {
|
110
|
+
const dirIndexPath = path.join(pagesDir, urlPath.slice(1), `index${ext}`);
|
111
|
+
if (await fileExists(dirIndexPath)) {
|
112
|
+
return {
|
113
|
+
componentPath: dirIndexPath,
|
114
|
+
params: {}
|
115
|
+
};
|
116
|
+
}
|
117
|
+
}
|
118
|
+
|
119
|
+
// No match found
|
120
|
+
return null;
|
121
|
+
};
|
122
|
+
|
123
|
+
// Handle all GET routes with SSR - explicitly exclude /public/ paths
|
124
|
+
app.get('*', async (req, res, next) => {
|
125
|
+
try {
|
126
|
+
// Skip API routes
|
127
|
+
if (req.path.startsWith('/api/')) {
|
128
|
+
return next();
|
129
|
+
}
|
130
|
+
|
131
|
+
// Skip static assets - check file extensions that should be handled as static
|
132
|
+
if (req.path.match(/\.(js|css|ico|png|jpg|jpeg|gif|svg|wasm|txt|pdf|json|map)$/)) {
|
133
|
+
return next();
|
134
|
+
}
|
135
|
+
|
136
|
+
// Try to find the matching component
|
137
|
+
const routeResult = await getComponentPath(req.path);
|
138
|
+
|
139
|
+
if (!routeResult) {
|
140
|
+
return res.status(404).send(`
|
141
|
+
<!DOCTYPE html>
|
142
|
+
<html>
|
143
|
+
<head>
|
144
|
+
<title>404 - Page Not Found</title>
|
145
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
146
|
+
<link href="/styles.css" rel="stylesheet" type="text/css">
|
147
|
+
</head>
|
148
|
+
<body>
|
149
|
+
<div id="app">
|
150
|
+
<div class="container">
|
151
|
+
<h1>404 - Page Not Found</h1>
|
152
|
+
<p>The page you requested could not be found.</p>
|
153
|
+
<a href="/" class="button">Go to Home</a>
|
154
|
+
</div>
|
155
|
+
</div>
|
156
|
+
<script id="__APP_DATA__" type="application/json">${JSON.stringify({
|
157
|
+
path: req.path,
|
158
|
+
error: 'not_found'
|
159
|
+
})}</script>
|
160
|
+
<script type="module" src="/client.js"></script>
|
161
|
+
</body>
|
162
|
+
</html>
|
163
|
+
`);
|
164
|
+
}
|
165
|
+
|
166
|
+
// Import the component
|
167
|
+
try {
|
168
|
+
const componentUrl = `file://${routeResult.componentPath}`;
|
169
|
+
const moduleImport = await import(componentUrl);
|
170
|
+
const PageComponent = moduleImport.default || moduleImport;
|
171
|
+
|
172
|
+
if (!PageComponent || typeof PageComponent !== 'function') {
|
173
|
+
throw new Error(`Invalid component in ${routeResult.componentPath}`);
|
174
|
+
}
|
175
|
+
|
176
|
+
// Create props with route data
|
177
|
+
const initialProps = {
|
178
|
+
params: routeResult.params,
|
179
|
+
path: req.path,
|
180
|
+
query: req.query,
|
181
|
+
api: {
|
182
|
+
serverTime: new Date().toISOString()
|
183
|
+
}
|
184
|
+
};
|
185
|
+
|
186
|
+
// Render the component
|
187
|
+
const result = PageComponent(initialProps);
|
188
|
+
let html = renderToString(result);
|
189
|
+
|
190
|
+
// Send complete HTML with hydration data
|
191
|
+
res.send(`
|
192
|
+
<!DOCTYPE html>
|
193
|
+
<html lang="en">
|
194
|
+
<head>
|
195
|
+
<meta charset="UTF-8">
|
196
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
197
|
+
<title>Frontend Hamroun App</title>
|
198
|
+
<link href="/styles.css" rel="stylesheet" type="text/css">
|
199
|
+
</head>
|
200
|
+
<body>
|
201
|
+
<div id="app">${html}</div>
|
202
|
+
<script id="__APP_DATA__" type="application/json">${JSON.stringify(initialProps)}</script>
|
203
|
+
<script type="module" src="/client.js"></script>
|
204
|
+
</body>
|
205
|
+
</html>
|
206
|
+
`);
|
207
|
+
} catch (error) {
|
208
|
+
console.error('Error rendering component:', error);
|
209
|
+
next(error);
|
210
|
+
}
|
211
|
+
} catch (error) {
|
212
|
+
next(error);
|
213
|
+
}
|
214
|
+
});
|
215
|
+
|
216
|
+
// Add error handler middleware
|
217
|
+
app.use(errorHandler);
|
218
|
+
|
219
|
+
// Add 404 handler for API routes
|
220
|
+
app.use(notFoundHandler);
|
221
|
+
|
222
|
+
// Start the server
|
223
|
+
app.listen(PORT, () => {
|
224
|
+
console.log(`Server running at http://localhost:${PORT}`);
|
225
|
+
console.log(`Mode: ${process.env.NODE_ENV || 'development'}`);
|
226
|
+
});
|
@@ -0,0 +1,59 @@
|
|
1
|
+
import { useState, useEffect, useMemo, useRef, createContext } from 'frontend-hamroun';
|
2
|
+
|
3
|
+
// Create a theme context
|
4
|
+
const ThemeContext = createContext('light');
|
5
|
+
|
6
|
+
export function App() {
|
7
|
+
// Initialize with a default state that works on both server and client
|
8
|
+
const [count, setCount] = useState(0);
|
9
|
+
const [theme, setTheme] = useState<'light' | 'dark'>('light');
|
10
|
+
const renderCount = useRef(0);
|
11
|
+
|
12
|
+
// Client-side only effect
|
13
|
+
useEffect(() => {
|
14
|
+
if (typeof window !== 'undefined') {
|
15
|
+
renderCount.current += 1;
|
16
|
+
console.log('Component rendered', renderCount.current, 'times');
|
17
|
+
}
|
18
|
+
return () => console.log('Component unmounting');
|
19
|
+
}, [count]);
|
20
|
+
|
21
|
+
// Memoized value
|
22
|
+
const doubled = useMemo(() => count * 2, [count]);
|
23
|
+
|
24
|
+
return (
|
25
|
+
<ThemeContext.Provider value={theme}>
|
26
|
+
<div style={{
|
27
|
+
padding: '20px',
|
28
|
+
backgroundColor: theme === 'dark' ? '#333' : '#fff',
|
29
|
+
color: theme === 'dark' ? '#fff' : '#333'
|
30
|
+
}}>
|
31
|
+
<h1>Server-Side Rendered App</h1>
|
32
|
+
<div>
|
33
|
+
<button
|
34
|
+
onClick={() => setCount(count - 1)}
|
35
|
+
data-action="decrement"
|
36
|
+
>-</button>
|
37
|
+
<span style={{ margin: '0 10px' }}>{count}</span>
|
38
|
+
<button
|
39
|
+
onClick={() => setCount(count + 1)}
|
40
|
+
data-action="increment"
|
41
|
+
>+</button>
|
42
|
+
</div>
|
43
|
+
<p>Doubled value: {doubled}</p>
|
44
|
+
{typeof window !== 'undefined' && (
|
45
|
+
<p>Render count: {renderCount.current}</p>
|
46
|
+
)}
|
47
|
+
<button
|
48
|
+
onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}
|
49
|
+
style={{ marginTop: '10px' }}
|
50
|
+
>
|
51
|
+
Toggle Theme ({theme})
|
52
|
+
</button>
|
53
|
+
<script dangerouslySetInnerHTML={{
|
54
|
+
__html: `window.__INITIAL_STATE__ = ${JSON.stringify({ count: 0, theme: 'light' })};`
|
55
|
+
}} />
|
56
|
+
</div>
|
57
|
+
</ThemeContext.Provider>
|
58
|
+
);
|
59
|
+
}
|
@@ -0,0 +1,18 @@
|
|
1
|
+
import { hydrate, createElement } from 'frontend-hamroun';
|
2
|
+
|
3
|
+
// For simplicity in this example, we just hydrate the root component
|
4
|
+
// In a more complex app, you might use a router
|
5
|
+
import HomePage from './pages/index';
|
6
|
+
|
7
|
+
// When the DOM is ready, hydrate the server-rendered HTML
|
8
|
+
document.addEventListener('DOMContentLoaded', () => {
|
9
|
+
const rootElement = document.getElementById('app');
|
10
|
+
|
11
|
+
if (rootElement) {
|
12
|
+
// Hydrate the app with the same component that was rendered on the server
|
13
|
+
hydrate(<HomePage />, rootElement);
|
14
|
+
console.log('Hydration complete');
|
15
|
+
} else {
|
16
|
+
console.error('Could not find root element with id "app"');
|
17
|
+
}
|
18
|
+
});
|