Skip to content

Commit 616870e

Browse files
authored
refactor(React template): Add easier setup using HashRoute (#97)
1 parent 9519b7b commit 616870e

File tree

5 files changed

+26
-28
lines changed

5 files changed

+26
-28
lines changed

templates/react/{{cookiecutter.project_slug}}/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ build-frontend: # Build the React app
4343
@if [ ! -d "$(FRONTEND_FOLDER)/node_modules" ]; then \
4444
$(MAKE) install-frontend; \
4545
fi
46-
cd $(FRONTEND_FOLDER); rm -rf build && REACT_APP_DEVELOPMENT_ENVIRONMENT=false NODE_ENV=prod npm run build
46+
cd $(FRONTEND_FOLDER); rm -rf build && NODE_ENV=prod npm run build
4747

4848
start-frontend: ## Start the frontend in dev mode (hot reload)
49-
cd $(FRONTEND_FOLDER); REACT_APP_DEVELOPMENT_ENVIRONMENT=true yarn start
49+
cd $(FRONTEND_FOLDER); yarn start
5050

5151
install: venv install-backend install-frontend ## Install dependencies
5252

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
from localstack.http import route, Request, Response
1+
import logging
2+
3+
from localstack.http import Request, Response, route
24

35
from .. import static
46

7+
LOG = logging.getLogger(__name__)
8+
9+
510
class WebApp:
611
@route("/")
712
def index(self, request: Request, *args, **kwargs):
813
return Response.for_resource(static, "index.html")
9-
14+
1015
@route("/<path:path>")
1116
def index2(self, request: Request, path: str, **kwargs):
12-
return Response.for_resource(static, path)
17+
try:
18+
return Response.for_resource(static, path)
19+
except Exception:
20+
LOG.debug(f"File {path} not found, serving index.html")
21+
return Response.for_resource(static, "index.html")

templates/react/{{cookiecutter.project_slug}}/frontend/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
"@emotion/styled": "^11.11.5",
99
"@localstack/integrations": "^1.0.0",
1010
"@mui/material": "^5.15.20",
11-
"@testing-library/react": "^13.4.0",
12-
"@types/node": "^16.18.99",
13-
"@types/react-dom": "^17.0.11",
1411
"react": "^17.0.2",
1512
"react-dom": "^17.0.2",
1613
"react-router-dom": "^6.24.0",
@@ -19,6 +16,10 @@
1916
},
2017
"devDependencies": {
2118
"@esbuild-plugins/node-modules-polyfill": "^0.1.4",
19+
"@testing-library/react": "^13.4.0",
20+
"@types/node": "^16.18.99",
21+
"@types/react": "^19.1.6",
22+
"@types/react-dom": "^17.0.11",
2223
"concurrently": "^8.2.2",
2324
"esbuild": "^0.16.6",
2425
"esbuild-envfile-plugin": "^1.0.2",

templates/react/{{cookiecutter.project_slug}}/frontend/src/constants.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
1-
import ReactDOM from 'react-dom';
2-
import './index.css';
3-
import { CustomRoutes } from './CustomRoutes';
4-
import { BrowserRouter } from 'react-router-dom';
5-
import { LocalStackThemeProvider } from '@localstack/integrations'
6-
import { DEVELOPMENT_ENVIRONMENT } from './constants';
7-
8-
const EXTENSION_NAME = '{{cookiecutter.project_slug}}'
9-
10-
const getBaseName = () => {
11-
if (window.location.origin.includes(EXTENSION_NAME) || DEVELOPMENT_ENVIRONMENT) {
12-
return '';
13-
}
14-
15-
return `/_extension/${EXTENSION_NAME}`;
16-
};
1+
import ReactDOM from "react-dom";
2+
import "./index.css";
3+
import { CustomRoutes } from "./CustomRoutes";
4+
import { HashRouter } from "react-router-dom";
5+
import { LocalStackThemeProvider } from "@localstack/integrations";
176

187
ReactDOM.render(
198
<LocalStackThemeProvider useExtensionLayout>
20-
<BrowserRouter basename={getBaseName()}>
9+
<HashRouter>
2110
<CustomRoutes />
22-
</BrowserRouter >
11+
</HashRouter>
2312
</LocalStackThemeProvider>,
24-
document.getElementById('root'),
13+
document.getElementById("root")
2514
);

0 commit comments

Comments
 (0)