diff --git a/client/package.json b/client/package.json index 4fde489..6bdffed 100644 --- a/client/package.json +++ b/client/package.json @@ -21,6 +21,7 @@ "itty-router": "^2.6.6", "react": "^18.3.1", "react-dom": "^18.3.1", + "react-router-dom": "^6.24.1", "serverless-cloudflare-workers": "^1.2.0" }, "devDependencies": { diff --git a/client/src/App.jsx b/client/src/App.jsx index 9ce847f..960d0fe 100644 --- a/client/src/App.jsx +++ b/client/src/App.jsx @@ -1,11 +1,19 @@ -import * as React from "react"; import { NextUIProvider } from "@nextui-org/react"; -import { Home } from "./pages/Home"; +import { Outlet } from 'react-router-dom'; +import { Header } from "@/components/Header/Header"; +import { Footer } from "@/components/Footer/Footer"; -export const App = () => { +const App = () => { return ( - - - +
+ +
+ +
+ ); -} \ No newline at end of file +}; + +export default App; \ No newline at end of file diff --git a/client/src/index.js b/client/src/index.js index a2d02fa..c5faf29 100644 --- a/client/src/index.js +++ b/client/src/index.js @@ -5,30 +5,42 @@ const router = Router(); const { base64Handler, healthHandler, postHandler, rootHandler, routesAndAssetsHandler } = routes; -/** The rootHandler will serve the React app accordingly - * Other routes can be defined as neeeded +/** + * Health route + * This route is used to check the health status of the application. + * When a GET request is made to /health, it will be handled by the healthHandler. */ -router.get('/', rootHandler); -// Health route router.get('/health', healthHandler); -// Test route + +/** + * Test route for base64 encoding + * This route takes a text parameter and returns its base64 encoded version. + * When a GET request is made to /base64/:text, it will be handled by the base64Handler. + */ router.get("/base64/:text", base64Handler); -// Test post route + +/** + * Test POST route + * This route is used to handle POST requests for testing purposes. + * When a POST request is made to /post, it will be handled by the postHandler. + */ router.post("/post", postHandler); /** - * This is the last route we define, it will match anything that hasn't hit a route we've defined - * above, therefore it's useful as a 404 (and avoids us hitting worker exceptions, so make sure - * to include it!). + * Catch-all route for serving the React app + * This is the last route we define. It will match any route that hasn't been + * defined above, making it useful as a catch-all route. + * This ensures that any route not explicitly defined will serve the React app, + * allowing the client-side routing of React Router to take over. */ -router.all("*", () => new Response("404, not found!", { status: 404 })); +router.all('*', rootHandler); /** - * All incoming requests to the worker are passed to the router - * where your routes are called and the response is sent. - * routesAndAssetsHandler will map assets and routes accordingly + * Event listener for incoming requests + * All incoming requests to the worker are passed to the router where your routes + * are called, and the response is sent. The routesAndAssetsHandler will map + * assets and routes accordingly. */ addEventListener('fetch', event => { event.respondWith(routesAndAssetsHandler(event, router)); }); - diff --git a/client/src/main.js b/client/src/main.js index 1bec7b3..6795e2a 100644 --- a/client/src/main.js +++ b/client/src/main.js @@ -1,7 +1,30 @@ -import { App } from './App'; import { createRoot } from 'react-dom/client'; +import { createBrowserRouter, RouterProvider } from 'react-router-dom' +import App from './App'; +import Error from './pages/Error'; +import Home from '@/pages/Home'; import './globals.css'; +const router = createBrowserRouter([ + { + path: '/', + element: , + errorElement: , + children: [ + { + index: true, + element: , + }, + { + path: '/test', + element:

Testing 1 2 3

, + }, + ], + }, +]); + // Render your React component instead const root = createRoot(document.getElementById('root')); -root.render(); +root.render( + +); diff --git a/client/src/pages/Error.jsx b/client/src/pages/Error.jsx new file mode 100644 index 0000000..2e043cd --- /dev/null +++ b/client/src/pages/Error.jsx @@ -0,0 +1,19 @@ +import { Header } from "@/components/Header/Header"; +import { Outlet } from "react-router-dom"; +import { Footer } from "@/components/Footer/Footer"; + +const Error = () => { + return ( +
+
+
+
+

Error!

+
+
+
+
+ ); +}; + +export default Error; \ No newline at end of file diff --git a/client/src/pages/Home.jsx b/client/src/pages/Home.jsx index 33ae3a3..701f95f 100644 --- a/client/src/pages/Home.jsx +++ b/client/src/pages/Home.jsx @@ -1,35 +1,31 @@ import { useState } from 'react'; -import { Header } from "@/components/Header/Header"; -import { Footer } from "@/components/Footer/Footer"; import { Button } from "@nextui-org/react"; -export const Home = () => { +const Home = () => { const [count, setCount] = useState(0); const tester = "Victor E"; return ( -
-
-
-
-

Hello, Cloudflare Workers!

-
-

This is a basic React page deployed on Cloudflare Workers.

-
-
Your name: { tester }
+
+
+

Hello, Cloudflare Workers!

+
+

This is a basic React page deployed on Cloudflare Workers.

+
+
Your name: { tester }
-

Count: { count }

-
- -
+

Count: { count }

+
+
-
); -}; \ No newline at end of file +}; + +export default Home; \ No newline at end of file diff --git a/client/webpack-dev.config.js b/client/webpack-dev.config.js index 477b0aa..ca7025b 100644 --- a/client/webpack-dev.config.js +++ b/client/webpack-dev.config.js @@ -6,6 +6,7 @@ module.exports = { devServer: { compress: true, // Enable gzip compression port: 3000, // Port to run dev server on - hot: true, // Enable hot module replacement + hot: true, // Enable hot module replacement, + historyApiFallback: true // This option is crucial for handling client-side routing }, }; diff --git a/client/webpack.config.js b/client/webpack.config.js index 61b7b24..a8f5076 100644 --- a/client/webpack.config.js +++ b/client/webpack.config.js @@ -8,7 +8,7 @@ module.exports = { mode: 'production', entry: './src/main.js', output: { - filename: 'bundle.[contenthash].js', // Use content hash for cache busting + filename: 'bundle.js', // Use content hash for cache busting path: path.resolve(__dirname, 'dist'), }, module: { @@ -45,7 +45,7 @@ module.exports = { { from: './public', to: '' }, // Copy all files from public directory to dist ], }), - new CompressionPlugin() + // new CompressionPlugin() ], resolve: { extensions: ['.js', '.jsx'], @@ -54,21 +54,21 @@ module.exports = { '@public': path.resolve(__dirname, 'public'), // Alias for public folder } }, - optimization: { - minimize: true, - minimizer: [ - new CssMinimizerPlugin(), - new TerserPlugin({ - terserOptions: { - format: { - comments: false, - }, - }, - extractComments: false, - }), - ], - splitChunks: { - chunks: 'all', - }, - } + // optimization: { + // minimize: true, + // minimizer: [ + // new CssMinimizerPlugin(), + // new TerserPlugin({ + // terserOptions: { + // format: { + // comments: false, + // }, + // }, + // extractComments: false, + // }), + // ], + // splitChunks: { + // chunks: 'all', + // }, + // } };