-
Notifications
You must be signed in to change notification settings - Fork 702
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
C3: next templates refactoring (#4999)
* add variant selection to c3 templates' `copyFiles` * add possibility to edit destination dir for c3's `copyFiles` * C3: refactor Next.js template code Align the Next.js template code with the rest of the C3 templates by removing its `templates.ts` file and including templates files directly in the next template directory * add new copyFile utility * fix: make sure not to wrongly ask users if they want to use typescript
- Loading branch information
1 parent
246512c
commit ce6d4bc
Showing
20 changed files
with
472 additions
and
395 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
"create-cloudflare": patch | ||
--- | ||
|
||
fix: make sure not to wrongly ask users if they want to use typescript | ||
|
||
currently if a CLI invoked by C3 asks the user if they want to use | ||
typescript and the user opted out of it, C3 could actually again offer | ||
typescript to the user afterwords, make sure that this does not happen |
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
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
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,57 @@ | ||
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`c3`](https://developers.cloudflare.com/pages/get-started/c3). | ||
|
||
## Getting Started | ||
|
||
First, run the development server: | ||
|
||
```bash | ||
npm run dev | ||
# or | ||
yarn dev | ||
# or | ||
pnpm dev | ||
# or | ||
bun dev | ||
``` | ||
|
||
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. | ||
|
||
## Cloudflare integration | ||
|
||
Besides the `dev` script mentioned above `c3` has added a few extra scripts that allow you to integrate the application with the [Cloudflare Pages](https://pages.cloudflare.com/) environment, these are: | ||
- `pages:build` to build the application for Pages using the [`@cloudflare/next-on-pages`](https://github.com/cloudflare/next-on-pages) CLI | ||
- `pages:preview` to locally preview your Pages application using the [Wrangler](https://developers.cloudflare.com/workers/wrangler/) CLI | ||
- `pages:deploy` to deploy your Pages application using the [Wrangler](https://developers.cloudflare.com/workers/wrangler/) CLI | ||
|
||
> __Note:__ while the `dev` script is optimal for local development you should preview your Pages application as well (periodically or before deployments) in order to make sure that it can properly work in the Pages environment (for more details see the [`@cloudflare/next-on-pages` recommended workflow](https://github.com/cloudflare/next-on-pages/blob/05b6256/internal-packages/next-dev/README.md#recommended-workflow)) | ||
### Bindings | ||
|
||
Cloudflare [Bindings](https://developers.cloudflare.com/pages/functions/bindings/) are what allows you to interact with resources available in the Cloudflare Platform. | ||
|
||
You can use bindings during development, when previewing locally your application and of course in the deployed application: | ||
|
||
- To use bindings in dev mode you need to define them in the `next.config.js` file under `setupDevBindings`, this mode uses the `next-dev` `@cloudflare/next-on-pages` submodule. For more details see its [documentation](https://github.com/cloudflare/next-on-pages/blob/05b6256/internal-packages/next-dev/README.md). | ||
|
||
- To use bindings in the preview mode you need to add them to the `pages:preview` script accordingly to the `wrangler pages dev` command. For more details see its [documentation](https://developers.cloudflare.com/workers/wrangler/commands/#dev-1) or the [Pages Bindings documentation](https://developers.cloudflare.com/pages/functions/bindings/). | ||
|
||
- To use bindings in the deployed application you will need to configure them in the Cloudflare [dashboard](https://dash.cloudflare.com/). For more details see the [Pages Bindings documentation](https://developers.cloudflare.com/pages/functions/bindings/). | ||
|
||
#### KV Example | ||
|
||
`c3` has added for you an example showing how you can use a KV binding, in order to enable the example, search for lines containing the following comment: | ||
```ts | ||
// KV Example: | ||
``` | ||
|
||
and uncomment the commented lines below it. | ||
|
||
After doing this you can run the `dev` script and visit the `/api/hello` route to see the example in action. | ||
|
||
To then enable such example also in preview mode add a `kv` in the `pages:preview` script like so: | ||
```diff | ||
- "pages:preview": "npm run pages:build && wrangler pages dev .vercel/output/static --compatibility-date=2023-12-18 --compatibility-flag=nodejs_compat", | ||
+ "pages:preview": "npm run pages:build && wrangler pages dev .vercel/output/static --compatibility-date=2023-12-18 --compatibility-flag=nodejs_compat --kv MY_KV", | ||
``` | ||
|
||
Finally, if you also want to see the example work in the deployed application make sure to add a `MY_KV` binding to your Pages application in its [dashboard kv bindings settings section](https://dash.cloudflare.com/?to=/:account/pages/view/:pages-project/settings/functions#kv_namespace_bindings_section). After having configured it make sure to re-deploy your application. |
19 changes: 19 additions & 0 deletions
19
packages/create-cloudflare/templates/next/app/js/app/api/hello/route.js
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,19 @@ | ||
export const runtime = 'edge' | ||
|
||
export async function GET(request) { | ||
let responseText = 'Hello World' | ||
|
||
// In the edge runtime you can use Bindings that are available in your application | ||
// (for more details see: | ||
// - https://developers.cloudflare.com/pages/framework-guides/deploy-a-nextjs-site/#use-bindings-in-your-nextjs-application | ||
// - https://developers.cloudflare.com/pages/functions/bindings/ | ||
// ) | ||
// | ||
// KV Example: | ||
// const myKv = process.env.MY_KV | ||
// await myKv.put('suffix', ' from a KV store!') | ||
// const suffix = await myKv.get('suffix') | ||
// responseText += suffix | ||
|
||
return new Response(responseText) | ||
} |
58 changes: 58 additions & 0 deletions
58
packages/create-cloudflare/templates/next/app/js/app/not-found.js
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,58 @@ | ||
export const runtime = "edge"; | ||
|
||
export default function NotFound() { | ||
return ( | ||
<> | ||
<title>404: This page could not be found.</title> | ||
<div style={styles.error}> | ||
<div> | ||
<style | ||
dangerouslySetInnerHTML={{ | ||
__html: `body{color:#000;background:#fff;margin:0}.next-error-h1{border-right:1px solid rgba(0,0,0,.3)}@media (prefers-color-scheme:dark){body{color:#fff;background:#000}.next-error-h1{border-right:1px solid rgba(255,255,255,.3)}}`, | ||
}} | ||
/> | ||
<h1 className="next-error-h1" style={styles.h1}> | ||
404 | ||
</h1> | ||
<div style={styles.desc}> | ||
<h2 style={styles.h2}>This page could not be found.</h2> | ||
</div> | ||
</div> | ||
</div> | ||
</> | ||
); | ||
} | ||
|
||
const styles = { | ||
error: { | ||
fontFamily: | ||
'system-ui,"Segoe UI",Roboto,Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji"', | ||
height: "100vh", | ||
textAlign: "center", | ||
display: "flex", | ||
flexDirection: "column", | ||
alignItems: "center", | ||
justifyContent: "center", | ||
}, | ||
|
||
desc: { | ||
display: "inline-block", | ||
}, | ||
|
||
h1: { | ||
display: "inline-block", | ||
margin: "0 20px 0 0", | ||
padding: "0 23px 0 0", | ||
fontSize: 24, | ||
fontWeight: 500, | ||
verticalAlign: "top", | ||
lineHeight: "49px", | ||
}, | ||
|
||
h2: { | ||
fontSize: 14, | ||
fontWeight: 400, | ||
lineHeight: "49px", | ||
margin: 0, | ||
}, | ||
}; |
Oops, something went wrong.