mirror of
https://github.com/aleleba/create-react-go-ssr.git
synced 2025-07-27 01:58:13 -06:00
Adding Redux to project, updating to version 0.1.0
This commit is contained in:
27
src/__mocks__/ProviderMock.tsx
Normal file
27
src/__mocks__/ProviderMock.tsx
Normal file
@ -0,0 +1,27 @@
|
||||
import React from 'react';
|
||||
import { Provider } from 'react-redux';
|
||||
import { Router } from 'react-router-dom';
|
||||
import { createMemoryHistory } from 'history';
|
||||
import initialStateReducer from '../frontend/reducers/initialState';
|
||||
import setStore from '../frontend/setStore';
|
||||
|
||||
export const ProviderMock = ({ children, initialState }: { children: unknown, initialState?: unknown}) => {
|
||||
let initialStateMock = initialStateReducer;
|
||||
|
||||
if(initialState !== undefined){
|
||||
initialStateMock = initialState as unknown as typeof initialStateReducer;
|
||||
}
|
||||
|
||||
const history = createMemoryHistory();
|
||||
const store = setStore({ initialState: initialStateMock });
|
||||
|
||||
return(
|
||||
<Provider store={store}>
|
||||
<Router location={history.location} navigator={history}>
|
||||
{children as JSX.Element}
|
||||
</Router>
|
||||
</Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export default ProviderMock;
|
2
src/__mocks__/fileMock.ts
Normal file
2
src/__mocks__/fileMock.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export const fileMock = '';
|
||||
module.exports = fileMock;
|
1
src/__mocks__/index.ts
Normal file
1
src/__mocks__/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './ProviderMock';
|
@ -3,7 +3,7 @@ import PrincipalRoutes from './PrincipalRoutes';
|
||||
|
||||
const App = () => {
|
||||
useEffect(() => {
|
||||
const ws = new WebSocket('wss://xs70kvlc-3000.use.devtunnels.ms/ws');
|
||||
const ws = new WebSocket('wss://nmr4jbx8-3000.use.devtunnels.ms/ws');
|
||||
ws.onmessage = (event) => {
|
||||
if (event.data === 'reload') {
|
||||
window.location.reload();
|
||||
|
@ -1,25 +1,36 @@
|
||||
import React from 'react';
|
||||
import './InitialComponent.scss';
|
||||
import { connect } from 'react-redux';
|
||||
import { Link } from 'react-router-dom';
|
||||
|
||||
const InitialComponent = () => (
|
||||
<div className="App">
|
||||
<header className="App-header">
|
||||
<img src="assets/img/logo.svg" className="App-logo" alt="logo" />
|
||||
<p>
|
||||
Edit <code>src/frontend/InitialComponent.jsx</code> and save to reload.
|
||||
</p>
|
||||
<a
|
||||
className="App-link"
|
||||
href="https://reactjs.org"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
Learn React
|
||||
</a>
|
||||
<Link className="App-link" to="/other-component">Other Component</Link>
|
||||
</header>
|
||||
</div>
|
||||
);
|
||||
const InitialComponent = ({ hello }: { hello: string }) => {
|
||||
|
||||
export default InitialComponent;
|
||||
return(
|
||||
<div className="App">
|
||||
<header className="App-header">
|
||||
<img src="assets/img/logo.svg" className="App-logo" alt="logo" />
|
||||
<p>This is the text from the store of redux: <strong>{hello}</strong></p>
|
||||
<p>
|
||||
Edit <code>src/frontend/InitialComponent.jsx</code> and save to reload.
|
||||
</p>
|
||||
<a
|
||||
className="App-link"
|
||||
href="https://reactjs.org"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
Learn React
|
||||
</a>
|
||||
<Link className="App-link" to="/other-component">Other Component</Link>
|
||||
</header>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const mapStateToProps = (state) => {
|
||||
return {
|
||||
hello: state.testReducer.hello
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps)(InitialComponent);
|
||||
|
@ -1,15 +1,23 @@
|
||||
import React from 'react';
|
||||
//Redux
|
||||
import { Provider } from 'react-redux';
|
||||
import setStore from '../setStore';
|
||||
import initialState from '../reducers/initialState';
|
||||
import { renderToString } from 'react-dom/server';
|
||||
import { StaticRouter } from 'react-router-dom/server';
|
||||
import App from '../components/App';
|
||||
|
||||
const url = process.argv[2];
|
||||
|
||||
const store = setStore({ initialState });
|
||||
|
||||
const render = () => {
|
||||
return renderToString(
|
||||
<StaticRouter location={`${url}`} >
|
||||
<App />
|
||||
</StaticRouter>
|
||||
<Provider store={store}>
|
||||
<StaticRouter location={`${url}`} >
|
||||
<App />
|
||||
</StaticRouter>
|
||||
</Provider>
|
||||
);
|
||||
};
|
||||
|
||||
|
5
src/frontend/getPreloadedState/getPreloadedState.ts
Normal file
5
src/frontend/getPreloadedState/getPreloadedState.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import setStore from '../setStore';
|
||||
import initialState from '../reducers/initialState';
|
||||
const store = setStore({ initialState });
|
||||
const preloadedState = store.getState();
|
||||
console.log(preloadedState);
|
@ -10,7 +10,7 @@ import { config } from '../../config';
|
||||
|
||||
import './styles/global.scss';
|
||||
import App from './components/App';
|
||||
// import serviceWorkerRegistration from '../../serviceWorkerRegistration';
|
||||
import serviceWorkerRegistration from '../../serviceWorkerRegistration';
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
@ -72,10 +72,10 @@ ENV === 'production' && hydrateRoot(container,
|
||||
</Provider>
|
||||
); */
|
||||
|
||||
/* if((ENV) && (ENV === 'production')){
|
||||
if((ENV) && (ENV === 'production')){
|
||||
serviceWorkerRegistration();
|
||||
} */
|
||||
}
|
||||
|
||||
/* if(module.hot){
|
||||
/*if(module.hot){
|
||||
module.hot.accept();
|
||||
} */
|
||||
}*/
|
||||
|
@ -1,2 +1,3 @@
|
||||
const initialState = {};
|
||||
import { IInitialState } from './';
|
||||
const initialState: IInitialState = {};
|
||||
export default initialState;
|
||||
|
@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
func LoadEnv() {
|
||||
err := godotenv.Load(".env")
|
||||
err := godotenv.Load("../../.env")
|
||||
if err != nil {
|
||||
log.Fatal("Error loading .env file")
|
||||
}
|
||||
|
37
src/server/utils/getPreloadedState.go
Normal file
37
src/server/utils/getPreloadedState.go
Normal file
@ -0,0 +1,37 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func GetPreloadedState() string {
|
||||
// Set the path to the jsxToString.js file
|
||||
jsFilePath := "./web/getstate/getPreloadedState.js"
|
||||
|
||||
// Create the command to run Node.js with the jsxToString.js file
|
||||
cmd := exec.Command("node", jsFilePath)
|
||||
|
||||
// Get a pipe to the standard input of the Node.js process
|
||||
stdin, err := cmd.StdinPipe()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return ""
|
||||
}
|
||||
|
||||
// Write the URL parameter to the standard input of the Node.js process
|
||||
//fmt.Fprintf(stdin, "%s\n", url)
|
||||
|
||||
// Close the standard input pipe
|
||||
stdin.Close()
|
||||
|
||||
// Get the output and error of the Node.js process
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return string(output)
|
||||
}
|
||||
|
||||
// Return the output of the Node.js process
|
||||
return string(output)
|
||||
}
|
@ -7,7 +7,7 @@ import (
|
||||
|
||||
func JsxToString(url string) string {
|
||||
// Set the path to the jsxToString.js file
|
||||
jsFilePath := "./web/utils/tsxToString.js"
|
||||
jsFilePath := "./web/conversion/tsxToString.js"
|
||||
|
||||
// Create the command to run Node.js with the jsxToString.js file
|
||||
cmd := exec.Command("node", jsFilePath, url)
|
||||
|
@ -32,6 +32,7 @@ func RegisterHandlers(e *echo.Echo, paths []string) {
|
||||
//return c.File(filePath)
|
||||
url := c.Request().URL.String()
|
||||
component := utils.JsxToString(url)
|
||||
preloadedState := utils.GetPreloadedState();
|
||||
html := `<!DOCTYPE html>
|
||||
<html lang="es">
|
||||
<head>
|
||||
@ -47,9 +48,9 @@ func RegisterHandlers(e *echo.Echo, paths []string) {
|
||||
</head>
|
||||
<body>
|
||||
<div id="app">`+ component +`</div>
|
||||
<!-- <script>
|
||||
window.__PRELOADED_STATE__ = ${JSON.stringify(preloadedState).replace(/</g, '\\u003c')}
|
||||
</script> -->
|
||||
<script>
|
||||
window.__PRELOADED_STATE__ = JSON.stringify(`+ preloadedState+`).replace(/</g, '\\u003c')
|
||||
</script>
|
||||
<script src="assets/app-frontend.js" type="text/javascript"></script>
|
||||
<script src="assets/vendor-vendors.js" type="text/javascript"></script>
|
||||
</body>
|
||||
|
Reference in New Issue
Block a user