Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/get-server-data #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"dev": "node server-dev.js",
"build:client": "vite build --outDir dist/client",
"build:server": "vite build --ssr src/entry-server.jsx --outDir dist/server",
"build": "npm run build:client && npm run build:server",
"build:function": "vite build --ssr src/function.js --outDir dist/function",
"build": "npm run build:client && npm run build:server && npm run build:function",
"serve": "node server-prod.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down
6 changes: 5 additions & 1 deletion server-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ app.use('*', async (req, res) => {
const template = await vite.transformIndexHtml(url, fs.readFileSync('index.html', 'utf-8'));
const { render } = await vite.ssrLoadModule('/src/entry-server.jsx');

const html = template.replace(`<!--outlet-->`, render);
const { getServerData } = await vite.ssrLoadModule('/src/function.js');
const data = await getServerData();
const script = `<script>window.__data__=${JSON.stringify(data)}</script>`;

const html = template.replace(`<!--outlet-->`, `${render(data)} ${script}`);
res.status(200).set({ 'Content-Type': 'text/html' }).end(html);
} catch (error) {
res.status(500).end(error);
Expand Down
6 changes: 5 additions & 1 deletion server-prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ app.use('*', async (_, res) => {
const template = fs.readFileSync('./dist/client/index.html', 'utf-8');
const { render } = await import('./dist/server/entry-server.js');

const html = template.replace(`<!--outlet-->`, render);
const { getServerData } = await import('./dist/function/function.js');
const data = await getServerData();
const script = `<script>window.__data__=${JSON.stringify(data)}</script>`;

const html = template.replace(`<!--outlet-->`, `${render(data)} ${script}`);
res.status(200).set({ 'Content-Type': 'text/html' }).end(html);
} catch (error) {
res.status(500).end(error);
Expand Down
3 changes: 2 additions & 1 deletion src/app.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState } from 'react';

const App = () => {
const App = ({ data }) => {
const [count, setCount] = useState(0);

return (
Expand All @@ -11,6 +11,7 @@ const App = () => {
<div>{count}</div>
<button onClick={() => setCount(count + 1)}>Count</button>
</div>
<pre>{JSON.stringify(data, null, 2)}</pre>
</main>
);
};
Expand Down
8 changes: 7 additions & 1 deletion src/entry-client.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ import { hydrateRoot } from 'react-dom/client';

import App from './app';

hydrateRoot(document.getElementById('app'), <App />);
let data;

if (typeof window !== 'undefined') {
data = window.__data__;
}

hydrateRoot(document.getElementById('app'), <App data={data} />);
4 changes: 2 additions & 2 deletions src/entry-server.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { renderToString } from 'react-dom/server';

import App from './app';

export const render = () => {
return renderToString(<App />);
export const render = (data) => {
return renderToString(<App data={data} />);
};
6 changes: 6 additions & 0 deletions src/function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const getServerData = async () => {
const response = await fetch('https://dummyjson.com/products/1');
const data = await response.json();

return data;
};