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

Added React Router example configuration #16

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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export const serve = async (event: APIGatewayEvent, context: Context): Promise<A
headers: {
"Content-Type": "text/html",
},
body: await render(),
body: await render(event),
};
};
127 changes: 124 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"dependencies": {
"react": "^16.14.0",
"react-dom": "^16.14.0",
"react-router-dom": "^5.2.0",
"source-map-support": "^0.5.19"
},
"devDependencies": {
Expand All @@ -39,6 +40,7 @@
"@types/prettier": "^2.1.6",
"@types/react": "^16.14.2",
"@types/react-dom": "^16.9.10",
"@types/react-router-dom": "^5.1.7",
"@types/serverless": "^1.78.20",
"@types/source-map-support": "^0.5.3",
"@types/webpack": "^4.41.26",
Expand Down
23 changes: 12 additions & 11 deletions src/browser/App.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import "./App.css";

import React from "react";
import { Route, Switch } from "react-router-dom";

import useConfig from "../components/useConfig";
import logo from "./logo.svg";
import MainPage from "./pages/MainPage";
import ErrorPage from "./pages/ErrorPage";

/**
* Our Web Application
*/
export default function App() {
const config = useConfig();
const { app } = useConfig();
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Welcome to {config.app.TITLE}</h1>
</header>
<p className="App-intro">
To get started, edit <code>src/browser/App.jsx</code> and save to reload.
</p>
</div>
<Switch>
<Route exact path="/">
<MainPage />
</Route>
<Route>
<ErrorPage />
</Route>
</Switch>
);
}
7 changes: 6 additions & 1 deletion src/browser/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "./index.css";
*/
import React from "react";
import { hydrate } from "react-dom";
import { BrowserRouter } from "react-router-dom";

import ConfigContext from "../components/ConfigContext";
import { Config } from "../server/config";
Expand All @@ -15,9 +16,13 @@ const config = (window as any).__CONFIG__ as Config;
delete (window as any).__CONFIG__;
/* eslint-enable @typescript-eslint/no-unsafe-member-access */

const basename = config.app.URL.match(/^(?:https?:\/\/)?[^\/]+(\/?.+)?$/i)?.[1];

hydrate(
<ConfigContext.Provider value={config}>
<App />
<BrowserRouter basename={basename}>
<App />
</BrowserRouter>
</ConfigContext.Provider>,
document.querySelector("#root"),
);
5 changes: 5 additions & 0 deletions src/browser/pages/ErrorPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from "react";

export default function ErrorPage() {
return <div>404</div>;
}
19 changes: 19 additions & 0 deletions src/browser/pages/MainPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from "react";

import useConfig from "../../components/useConfig";
import logo from "../logo.svg";

export default function MainPage() {
const { app } = useConfig();
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Welcome to {app.TITLE}</h1>
</header>
<p className="App-intro">
To get started, edit <code>src/browser/App.jsx</code> and save to reload.
</p>
</div>
);
}
8 changes: 6 additions & 2 deletions src/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
* Server Side Rendering
*/
import React from "react";
import { APIGatewayEvent } from "aws-lambda";
import { renderToString } from "react-dom/server";
import { StaticRouter } from "react-router-dom";

import App from "../browser/App";
import ConfigContext from "../components/ConfigContext";
Expand All @@ -16,7 +18,7 @@ const isLocal = process.env.IS_LOCAL || process.env.IS_OFFLINE;
/**
* Server-side rendering
*/
export default async function render(): Promise<string> {
export default async function render(event: APIGatewayEvent): Promise<string> {
let stats: Stats = { main: "index.js", css: "index.css" };
if (!isLocal) {
try {
Expand All @@ -28,7 +30,9 @@ export default async function render(): Promise<string> {

const content = renderToString(
<ConfigContext.Provider value={config}>
<App />
<StaticRouter basename={config.app.URL} location={event.path}>
<App />
</StaticRouter>
</ConfigContext.Provider>,
);
return html({ stats, content, config });
Expand Down