diff --git a/examples/2.1-simple-express-only-client/build.js b/examples/2.1-simple-express-only-client/build.js new file mode 100644 index 0000000..26d9180 --- /dev/null +++ b/examples/2.1-simple-express-only-client/build.js @@ -0,0 +1,38 @@ +const { isomorphicCompiler, backendCompiler, frontendCompiler, getArgs } = require('@rockpack/compiler'); + +if (getArgs().client) { + frontendCompiler({ + src: 'src/client.jsx', + global: { + client: true + }, + dist: 'public', + babel: { + plugins: [ + '@issr/babel-plugin' + ] + }, + }); +} else { + isomorphicCompiler( + frontendCompiler({ + src: 'src/client.jsx', + dist: 'public', + babel: { + plugins: [ + '@issr/babel-plugin' + ] + }, + }), + backendCompiler({ + src: 'src/server.jsx', + dist: 'dist', + babel: { + plugins: [ + '@issr/babel-plugin' + ] + }, + }) + ); + +} diff --git a/examples/2.1-simple-express-only-client/package.json b/examples/2.1-simple-express-only-client/package.json new file mode 100644 index 0000000..e8cc711 --- /dev/null +++ b/examples/2.1-simple-express-only-client/package.json @@ -0,0 +1,20 @@ +{ + "name": "@issr/example-2.1", + "version": "1.0.0", + "scripts": { + "start": "cross-env NODE_ENV=development node build", + "start:client": "cross-env NODE_ENV=development node build --client", + "build": "cross-env NODE_ENV=production node build" + }, + "dependencies": { + "@issr/core": "1.0.0", + "express": "^4.17.1", + "react": "16.13.1", + "react-dom": "16.13.1", + "serialize-javascript": "3.0.0" + }, + "devDependencies": { + "@issr/babel-plugin": "1.0.0", + "@rockpack/compiler": "1.9.0" + } +} diff --git a/examples/2.1-simple-express-only-client/src/App.jsx b/examples/2.1-simple-express-only-client/src/App.jsx new file mode 100644 index 0000000..0894007 --- /dev/null +++ b/examples/2.1-simple-express-only-client/src/App.jsx @@ -0,0 +1,19 @@ +import React from 'react'; +import { useSsrState, useSsrEffect } from '@issr/core'; + +const asyncFn = () => new Promise((resolve) => setTimeout(() => resolve('Hello world'), 1000)); + +export const App = () => { + const [state, setState] = useSsrState('text here'); + + useSsrEffect(async () => { + const data = await asyncFn() + setState(data); + }); + + return ( +
+

{state}

+
+ ); +}; diff --git a/examples/2.1-simple-express-only-client/src/client.jsx b/examples/2.1-simple-express-only-client/src/client.jsx new file mode 100644 index 0000000..bbf9d4d --- /dev/null +++ b/examples/2.1-simple-express-only-client/src/client.jsx @@ -0,0 +1,26 @@ +import React from 'react'; +import { hydrate, render } from 'react-dom'; +import { App } from './App'; +import createSsr from '@issr/core'; + +const onlyClient = Boolean(process.env.client); + +const SSR = createSsr(window.SSR_DATA, { onlyClient }); + +const rootElement = document.getElementById("root"); + +if (rootElement.hasChildNodes()) { + hydrate( + + + , + rootElement + ); +} else { + render( + + + , + rootElement + ); +} diff --git a/examples/2.1-simple-express-only-client/src/server.jsx b/examples/2.1-simple-express-only-client/src/server.jsx new file mode 100644 index 0000000..db39cfd --- /dev/null +++ b/examples/2.1-simple-express-only-client/src/server.jsx @@ -0,0 +1,33 @@ +import React from 'react'; +import express from 'express'; +import serialize from 'serialize-javascript'; +import { App } from './App'; +import { serverRender } from '@issr/core'; + +const app = express(); + +app.use(express.static('public')); + +app.get('/*', async (req, res) => { + const { html, state } = await serverRender(() => ); + res.send(` + + + + + Title + + + +
${html}
+ + + +`); +}); + +app.listen(4000, () => { + console.log('Example app listening on port 4000!'); +});