-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af90862
commit 57516db
Showing
6 changed files
with
115 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React, { useState } from 'react'; | ||
|
||
function App() { | ||
const [count, setCount] = useState(0); | ||
|
||
return ( | ||
<div> | ||
<h1>Hello, Cloudflare Workers!</h1> | ||
<p>This is a basic React page deployed on Cloudflare Workers.</p> | ||
<p>Count: {count}</p> | ||
<button type="button" onClick={() => setCount(count + 1)}>Increase</button> | ||
</div> | ||
); | ||
} | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,56 @@ | ||
import { Router } from 'itty-router' | ||
import { base64Handler, postHandler, rootHandler } from './routers' | ||
import { registerHBHelper } from './utils/hbsAsyncHelper.js' | ||
import '../assets/pages.js'; | ||
import '../assets/partials.js'; | ||
import { Router } from 'itty-router'; | ||
import { getAssetFromKV } from '@cloudflare/kv-asset-handler'; | ||
|
||
// Register HB | ||
registerHBHelper(); | ||
|
||
// Create a new router | ||
const router = Router(); | ||
router.get("/", rootHandler); | ||
router.get("/base64/:text", base64Handler); | ||
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!). | ||
*/ | ||
router.all("*", () => new Response("404, not found!", { status: 404 })); | ||
// Serve the React app | ||
router.get('/', async () => { | ||
// const html = ReactDOMServer.renderToString(<App />); | ||
return new Response(` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>React App on Cloudflare Workers</title> | ||
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script> | ||
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script src="bundle.js"></script> | ||
</body> | ||
</html> | ||
`, { | ||
headers: { | ||
'Content-Type': 'text/html', | ||
}, | ||
}); | ||
}); | ||
|
||
/** | ||
* This snippet ties our worker to the router we defined above, all incoming requests | ||
* are passed to the router where your routes are called and the response is sent. | ||
*/ | ||
addEventListener('fetch', (e) => { | ||
e.respondWith(router.handle(e.request)) | ||
// Respond to fetch events with the router | ||
addEventListener('fetch', event => { | ||
// Pass the entire event object to handleAssetRequest function | ||
event.respondWith(handleAssetRequest(event)); | ||
}); | ||
|
||
async function handleAssetRequest(event) { | ||
// Extract the request from the event | ||
const request = event.request; | ||
|
||
// Check if the request is for bundle.js | ||
if (request.url.endsWith('/bundle.js') || request.url.endsWith('/favicon.ico')) { | ||
// Serve the bundle.js file from KV storage | ||
try { | ||
// Pass the entire event object to getAssetFromKV | ||
return await getAssetFromKV(event); | ||
} catch (e) { | ||
return new Response(`Bundle not found: ${e.message}`, { | ||
status: 404, | ||
statusText: 'Not Found', | ||
}); | ||
} | ||
} | ||
|
||
// For any other requests, handle them with the router | ||
return router.handle(request); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import App from './App'; | ||
import { createRoot } from 'react-dom/client'; | ||
|
||
// Render your React component instead | ||
const root = createRoot(document.getElementById('root')); | ||
root.render(<App />); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const path = require('path'); | ||
|
||
module.exports = { | ||
mode: 'production', | ||
entry: './src/main.js', | ||
output: { | ||
filename: 'bundle.js', | ||
path: path.resolve(__dirname, 'dist'), | ||
}, | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.js$/, | ||
exclude: /node_modules/, | ||
use: { | ||
loader: 'babel-loader', | ||
options: { | ||
presets: ['@babel/preset-env', '@babel/preset-react'], | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters