|
| 1 | +# What is this |
| 2 | + |
| 3 | +In this example project, we use [Wrangler](https://github.com/cloudflare/wrangler) and [Cloudflare Workers](https://developers.cloudflare.com/workers/get-started/guide) to deploy and serve a static site developed in React JS. |
| 4 | +Cloudflare Workers is a great platform to deploy static sites: the application will be distributed to hundrends of locations around the world, and served directly from Cloudflare’s CDN at a server incredibly close to your users. |
| 5 | + |
| 6 | +Note: This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). |
| 7 | + |
| 8 | +See video tutorial: https://www.youtube.com/watch?v=6YC3MgVwCGA |
| 9 | + |
| 10 | + |
| 11 | +# Steps to reproduce this project |
| 12 | + |
| 13 | +This assumes you have installed npm. It is recommended to install it with a Node version manager like `nvm`, which puts the global `node_modules` in your home directory to eliminate permissions issues with `npm install -g`. |
| 14 | +Next install the wrangler CLI tool to be able to use Workers: |
| 15 | + |
| 16 | +`npm i @cloudflare/wrangler -g` |
| 17 | + |
| 18 | +Then login: |
| 19 | + |
| 20 | +wrangler config |
| 21 | + |
| 22 | +## Create a static site |
| 23 | + |
| 24 | +`npx create-react-app cf-react` |
| 25 | + |
| 26 | +The create-react-app will create a new project (i.e. `cf-react`), and include all the relevant dependencies needed to build the project. |
| 27 | + |
| 28 | +## Generate a project |
| 29 | + |
| 30 | + Enter the newly-created React project folder, and use the following command to generate a Workers Sites configuration for your project: |
| 31 | + |
| 32 | + `wrangler init --site` |
| 33 | + |
| 34 | +The init --site command will provide the scaffolding necessary to deploy your React application. For the majority of static sites, you shouldn’t need to change the Workers script: by default, the script will look at an incoming request, and will serve a corresponding asset from [Workers KV](https://www.cloudflare.com/products/workers-kv/) based on that route. For instance, if my static site is deployed at mystaticsite.com, requesting mystaticsite.com/about.html will look for a file in KV called about.html, and serve it back to the client. In addition, if the asset being returned from KV is cacheable, it will automatically be cached with Cloudflare’s CDN, making subsequent requests even faster. |
| 35 | + |
| 36 | +To serve a single page application, update workers-site/index.js with the following code to so that all html requests are pointed at your root index.html file. |
| 37 | +` |
| 38 | +import { getAssetFromKV, serveSinglePageApp } from '@cloudflare/kv-asset-handler'; |
| 39 | +async function handleEvent(event) { |
| 40 | + ... |
| 41 | + const asset = await getAssetFromKV(event, { mapRequestToAsset: serveSinglePageApp }); |
| 42 | +} |
| 43 | +` |
| 44 | + |
| 45 | +## Configure and publish |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +# Getting Started with Create React App |
| 50 | + |
| 51 | +## Available Scripts |
| 52 | + |
| 53 | +In the project directory, you can run: |
| 54 | + |
| 55 | +### `yarn start` |
| 56 | + |
| 57 | +Runs the app in the development mode.\ |
| 58 | +Open [http://localhost:3000](http://localhost:3000) to view it in the browser. |
| 59 | + |
| 60 | +The page will reload if you make edits.\ |
| 61 | +You will also see any lint errors in the console. |
| 62 | + |
| 63 | +### `yarn test` |
| 64 | + |
| 65 | +Launches the test runner in the interactive watch mode.\ |
| 66 | +See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. |
| 67 | + |
| 68 | +### `yarn build` |
| 69 | + |
| 70 | +Builds the app for production to the `build` folder.\ |
| 71 | +It correctly bundles React in production mode and optimizes the build for the best performance. |
| 72 | + |
| 73 | +The build is minified and the filenames include the hashes.\ |
| 74 | +Your app is ready to be deployed! |
| 75 | + |
| 76 | +See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. |
| 77 | + |
| 78 | +### `yarn eject` |
| 79 | + |
| 80 | +**Note: this is a one-way operation. Once you `eject`, you can’t go back!** |
| 81 | + |
| 82 | +If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. |
| 83 | + |
| 84 | +Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. |
| 85 | + |
| 86 | +You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. |
| 87 | + |
| 88 | +## Learn More |
| 89 | + |
| 90 | +You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). |
| 91 | + |
| 92 | +To learn React, check out the [React documentation](https://reactjs.org/). |
| 93 | + |
| 94 | +### Code Splitting |
| 95 | + |
| 96 | +This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) |
| 97 | + |
| 98 | +### Analyzing the Bundle Size |
| 99 | + |
| 100 | +This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) |
| 101 | + |
| 102 | +### Making a Progressive Web App |
| 103 | + |
| 104 | +This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) |
| 105 | + |
| 106 | +### Advanced Configuration |
| 107 | + |
| 108 | +This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) |
| 109 | + |
| 110 | +### Deployment |
| 111 | + |
| 112 | +This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) |
| 113 | + |
| 114 | +### `yarn build` fails to minify |
| 115 | + |
| 116 | +This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) |
0 commit comments