frontend-hamroun 1.1.49 → 1.1.50
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/package.json +2 -1
- package/templates/ssr-template/package.json +7 -6
- package/templates/ssr-template/src/App.tsx +33 -17
- package/templates/ssr-template/src/client.tsx +16 -14
- package/templates/ssr-template/src/server.ts +35 -23
- package/templates/ssr-template/tsconfig.json +6 -4
- package/templates/ssr-template/vite.config.ts +9 -8
package/package.json
CHANGED
@@ -1,18 +1,19 @@
|
|
1
1
|
{
|
2
|
-
"name": "ssr-
|
2
|
+
"name": "ssr-template",
|
3
3
|
"private": true,
|
4
4
|
"version": "0.0.0",
|
5
5
|
"type": "module",
|
6
6
|
"scripts": {
|
7
7
|
"dev": "vite",
|
8
|
-
"build
|
9
|
-
"build:
|
10
|
-
"build": "
|
11
|
-
"start": "node dist/server.js"
|
8
|
+
"build": "vite build",
|
9
|
+
"build:client": "vite build --outDir dist/client",
|
10
|
+
"build:server": "vite build --ssr src/server.ts --outDir dist/server",
|
11
|
+
"start": "node dist/server/server.js",
|
12
|
+
"preview": "vite preview"
|
12
13
|
},
|
13
14
|
"dependencies": {
|
14
15
|
"express": "^4.18.2",
|
15
|
-
"frontend-hamroun": "
|
16
|
+
"frontend-hamroun": "link:../.."
|
16
17
|
},
|
17
18
|
"devDependencies": {
|
18
19
|
"@types/express": "^4.17.17",
|
@@ -1,35 +1,51 @@
|
|
1
|
-
import { useState, useEffect } from 'frontend-hamroun';
|
1
|
+
import { useState, useEffect, Link, useRouter } from 'frontend-hamroun';
|
2
2
|
|
3
3
|
interface AppProps {
|
4
|
-
|
5
|
-
|
4
|
+
url: string;
|
5
|
+
user: any | null;
|
6
|
+
settings: {
|
6
7
|
theme: string;
|
7
8
|
};
|
8
|
-
user?: any;
|
9
9
|
}
|
10
10
|
|
11
|
-
export function App({
|
12
|
-
|
13
|
-
|
14
|
-
user = null
|
15
|
-
}: AppProps) {
|
16
|
-
const [theme, setTheme] = useState(initialSettings.theme);
|
11
|
+
export function App({ url, user, settings }: AppProps) {
|
12
|
+
const [theme, setTheme] = useState(settings.theme);
|
13
|
+
const router = useRouter();
|
17
14
|
|
18
15
|
useEffect(() => {
|
19
|
-
document.
|
16
|
+
document.documentElement.dataset.theme = theme;
|
20
17
|
}, [theme]);
|
21
18
|
|
22
19
|
return (
|
23
20
|
<div className="app">
|
24
21
|
<header>
|
25
|
-
<
|
26
|
-
|
27
|
-
|
28
|
-
|
22
|
+
<nav>
|
23
|
+
<Link href="/">Home</Link>
|
24
|
+
<Link href="/about">About</Link>
|
25
|
+
<button onClick={() => setTheme(t => t === 'light' ? 'dark' : 'light')}>
|
26
|
+
{theme === 'light' ? '🌙' : '☀️'}
|
27
|
+
</button>
|
28
|
+
</nav>
|
29
29
|
</header>
|
30
|
+
|
30
31
|
<main>
|
31
|
-
|
32
|
-
|
32
|
+
{router.pathname === '/' && (
|
33
|
+
<section>
|
34
|
+
<h1>Welcome</h1>
|
35
|
+
{user ? (
|
36
|
+
<p>Hello, {user.name}!</p>
|
37
|
+
) : (
|
38
|
+
<p>Welcome, guest!</p>
|
39
|
+
)}
|
40
|
+
</section>
|
41
|
+
)}
|
42
|
+
|
43
|
+
{router.pathname === '/about' && (
|
44
|
+
<section>
|
45
|
+
<h1>About</h1>
|
46
|
+
<p>This is a server-side rendered app.</p>
|
47
|
+
</section>
|
48
|
+
)}
|
33
49
|
</main>
|
34
50
|
</div>
|
35
51
|
);
|
@@ -1,29 +1,31 @@
|
|
1
1
|
import { hydrate } from 'frontend-hamroun';
|
2
2
|
import { App } from './App';
|
3
3
|
|
4
|
-
// Type-safe initial state
|
4
|
+
// Type-safe initial state
|
5
|
+
interface InitialState {
|
6
|
+
url: string;
|
7
|
+
user: null | { id: number; name: string };
|
8
|
+
settings: {
|
9
|
+
theme: 'light' | 'dark';
|
10
|
+
};
|
11
|
+
}
|
12
|
+
|
5
13
|
declare global {
|
6
14
|
interface Window {
|
7
|
-
__INITIAL_STATE__:
|
8
|
-
url: string;
|
9
|
-
user: any;
|
10
|
-
settings: {
|
11
|
-
theme: string;
|
12
|
-
};
|
13
|
-
};
|
15
|
+
__INITIAL_STATE__: InitialState;
|
14
16
|
}
|
15
17
|
}
|
16
18
|
|
17
|
-
// Get state
|
19
|
+
// Get state injected by SSR
|
18
20
|
const initialState = window.__INITIAL_STATE__;
|
19
21
|
|
22
|
+
// Hydrate with the same props used in SSR
|
20
23
|
document.addEventListener('DOMContentLoaded', () => {
|
21
24
|
hydrate(
|
22
|
-
<App
|
23
|
-
initialUrl={initialState.url}
|
24
|
-
initialSettings={initialState.settings}
|
25
|
-
user={initialState.user}
|
26
|
-
/>,
|
25
|
+
<App {...initialState} />,
|
27
26
|
document.getElementById('root')!
|
28
27
|
);
|
28
|
+
|
29
|
+
// Clean up after hydration
|
30
|
+
delete window.__INITIAL_STATE__;
|
29
31
|
});
|
@@ -1,52 +1,64 @@
|
|
1
1
|
import express from 'express';
|
2
2
|
import path from 'path';
|
3
3
|
import { fileURLToPath } from 'url';
|
4
|
-
import { renderToString
|
4
|
+
import { renderToString } from 'frontend-hamroun';
|
5
5
|
import { App } from './App';
|
6
6
|
|
7
7
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
8
8
|
const app = express();
|
9
|
-
const port = 3000;
|
10
9
|
|
11
10
|
// Static files
|
12
|
-
app.use(
|
11
|
+
app.use(express.static(path.join(__dirname, 'dist')));
|
13
12
|
|
14
|
-
// All routes handler
|
15
13
|
app.get('*', async (req, res) => {
|
16
14
|
try {
|
17
|
-
//
|
15
|
+
// Create initial state
|
18
16
|
const initialState = {
|
19
17
|
url: req.url,
|
20
|
-
user: null,
|
18
|
+
user: null,
|
21
19
|
settings: { theme: 'light' }
|
22
20
|
};
|
23
21
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
styles: ['/assets/styles.css'],
|
22
|
+
// Use renderToString with proper options
|
23
|
+
const html = await renderToString(<App {...initialState} />, {
|
24
|
+
title: 'SSR Demo',
|
25
|
+
description: 'Server-side rendered app with Frontend Hamroun',
|
26
|
+
scripts: [
|
27
|
+
'/client.js' // Client entry point
|
28
|
+
],
|
29
|
+
styles: ['/styles.css'],
|
33
30
|
initialState,
|
34
31
|
meta: {
|
32
|
+
'viewport': 'width=device-width, initial-scale=1.0',
|
35
33
|
'theme-color': '#ffffff',
|
36
|
-
'og:title': '
|
34
|
+
'og:title': 'SSR Demo',
|
35
|
+
'og:description': 'A server-rendered application'
|
37
36
|
},
|
38
|
-
|
39
|
-
'
|
37
|
+
htmlAttrs: {
|
38
|
+
lang: 'en',
|
39
|
+
'data-theme': initialState.settings.theme
|
40
40
|
}
|
41
41
|
});
|
42
|
-
|
42
|
+
|
43
43
|
res.send(html);
|
44
44
|
} catch (error) {
|
45
|
-
console.error('
|
46
|
-
|
45
|
+
console.error('SSR Error:', error);
|
46
|
+
// Fallback error page
|
47
|
+
res.status(500).send(`
|
48
|
+
<!DOCTYPE html>
|
49
|
+
<html>
|
50
|
+
<head>
|
51
|
+
<title>Error</title>
|
52
|
+
</head>
|
53
|
+
<body>
|
54
|
+
<h1>500 - Server Error</h1>
|
55
|
+
<script>window.location.reload()</script>
|
56
|
+
</body>
|
57
|
+
</html>
|
58
|
+
`);
|
47
59
|
}
|
48
60
|
});
|
49
61
|
|
50
|
-
app.listen(
|
51
|
-
console.log(
|
62
|
+
app.listen(3000, () => {
|
63
|
+
console.log('✓ SSR Server running at http://localhost:3000');
|
52
64
|
});
|
@@ -1,9 +1,9 @@
|
|
1
1
|
{
|
2
2
|
"compilerOptions": {
|
3
|
-
"target": "
|
3
|
+
"target": "ESNext",
|
4
4
|
"module": "ESNext",
|
5
5
|
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
6
|
-
"moduleResolution": "
|
6
|
+
"moduleResolution": "Node",
|
7
7
|
"jsx": "preserve",
|
8
8
|
"jsxFactory": "jsx",
|
9
9
|
"jsxFragmentFactory": "Fragment",
|
@@ -15,9 +15,11 @@
|
|
15
15
|
"resolveJsonModule": true,
|
16
16
|
"isolatedModules": true,
|
17
17
|
"noEmit": true,
|
18
|
-
"types": ["
|
18
|
+
"types": ["vite/client"],
|
19
|
+
"outDir": "dist",
|
20
|
+
"baseUrl": ".",
|
19
21
|
"paths": {
|
20
|
-
"frontend-hamroun": ["
|
22
|
+
"frontend-hamroun": ["../../src/index.ts"]
|
21
23
|
}
|
22
24
|
},
|
23
25
|
"include": ["src"],
|
@@ -4,27 +4,28 @@ import path from 'path';
|
|
4
4
|
export default defineConfig({
|
5
5
|
resolve: {
|
6
6
|
alias: {
|
7
|
-
'frontend-hamroun': path.resolve(__dirname, '
|
7
|
+
'frontend-hamroun': path.resolve(__dirname, '../../src/index.ts')
|
8
8
|
}
|
9
9
|
},
|
10
10
|
build: {
|
11
|
+
ssr: true,
|
11
12
|
outDir: 'dist',
|
12
|
-
ssr: 'src/server.ts',
|
13
13
|
rollupOptions: {
|
14
14
|
input: {
|
15
15
|
client: './src/client.tsx',
|
16
16
|
server: './src/server.ts'
|
17
17
|
},
|
18
18
|
output: {
|
19
|
-
|
20
|
-
chunkFileNames: 'assets/[name]-[hash].js',
|
21
|
-
assetFileNames: 'assets/[name]-[hash].[ext]'
|
19
|
+
format: 'esm'
|
22
20
|
}
|
23
21
|
}
|
24
22
|
},
|
25
23
|
esbuild: {
|
26
|
-
jsxFactory: '
|
27
|
-
jsxFragment: '
|
28
|
-
jsxInject: `import {
|
24
|
+
jsxFactory: 'createElement',
|
25
|
+
jsxFragment: 'Fragment',
|
26
|
+
jsxInject: `import { createElement, Fragment } from 'frontend-hamroun'`
|
27
|
+
},
|
28
|
+
optimizeDeps: {
|
29
|
+
exclude: ['frontend-hamroun']
|
29
30
|
}
|
30
31
|
});
|