Skip to content

Commit f5d0cb0

Browse files
make next app work, add link to it on deploy, update gif (#1891)
1 parent 13f6daa commit f5d0cb0

File tree

2 files changed

+75
-41
lines changed

2 files changed

+75
-41
lines changed
Loading

examples/tutorials/next.md

Lines changed: 75 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,14 @@ oldUrl:
1010
server-side-rendered applications. It is built on top of React and provides a
1111
lot of features out of the box.
1212

13-
In this tutorial, we'll build a simple Next.js application and run it with Deno.
14-
The app will display a list of dinosaurs. When you click on one, it'll take you
15-
to a dinosaur page with more details.
13+
In this tutorial, we'll build a
14+
[simple Next.js application](https://tutorial-with-next.deno.deno.net/) and run
15+
it with Deno. The app will display a list of dinosaurs. When you click on one,
16+
it'll take you to a dinosaur page with more details. You can see the
17+
[complete app on GitHub](https://github.com/denoland/tutorial-with-next/tree/main).
1618

1719
![demo of the app](./images/how-to/next/dinoapp.gif)
1820

19-
Start by verifying that you have the latest version of Deno installed, you will
20-
need at least Deno 1.46.0:
21-
22-
```sh
23-
deno --version
24-
```
25-
2621
## Create a Next.js app with Deno
2722

2823
Next provides a CLI tool to quickly scaffold a new Next.js app. In your terminal
@@ -36,29 +31,60 @@ When prompted, select the default options to create a new Next.js app with
3631
TypeScript.
3732

3833
Then, `cd` into the newly created project folder and run the following command
39-
to install the dependencies
34+
to install the dependencies with script execution allowed:
4035

4136
```sh
42-
deno install
37+
deno install --allow-scripts
4338
```
4439

45-
Next.js has some dependencies that still rely on `Object.prototype.__proto__`,
46-
so you need to allow it. In a new `deno.json` file, add the following lines:
40+
Next.js has some dependencies that still rely on `Object.prototype.__proto__`
41+
and requires CommonJS module support. To configure Deno for Next.js
42+
compatibility, update your `deno.json` file with the following configuration:
4743

4844
```json deno.json
4945
{
50-
"unstable": ["unsafe-proto"]
46+
"nodeModulesDir": "auto",
47+
"unstable": [
48+
"unsafe-proto",
49+
"sloppy-imports"
50+
],
51+
"compilerOptions": {
52+
"lib": [
53+
"dom",
54+
"dom.iterable",
55+
"esnext"
56+
],
57+
"strict": true,
58+
"jsx": "preserve"
59+
},
60+
"tasks": {
61+
"dev": "deno run -A --unstable-detect-cjs npm:next@latest dev",
62+
"build": "deno run -A --unstable-detect-cjs npm:next@latest build",
63+
"start": "deno run -A --unstable-detect-cjs npm:next@latest start"
64+
}
5165
}
5266
```
5367

68+
This configuration includes:
69+
70+
- `nodeModulesDir: "auto"` - Enables npm package lifecycle scripts
71+
- `unstable: ["unsafe-proto", "sloppy-imports"]` - Required for Next.js
72+
compatibility
73+
- `--unstable-detect-cjs` flag - Enables CommonJS module detection for Next.js
74+
dependencies
75+
5476
Now you can serve your new Next.js app:
5577

5678
```sh
57-
deno task dev
79+
deno run dev
5880
```
5981

60-
This will start the Next.js server, click the output link to localhost to see
61-
your app in the browser.
82+
This will start the Next.js development server using Deno. The `deno task dev`
83+
command runs the Next.js development server with the necessary flags for
84+
CommonJS compatibility.
85+
86+
Visit [http://localhost:3000](http://localhost:3000) to see the app in the
87+
browser.
6288

6389
## Add a backend
6490

@@ -77,36 +103,37 @@ dinosaur based on the name in the URL.
77103

78104
### /api/
79105

80-
In the `app` folder of your new project, create an `api` folder. In that folder,
81-
create a `route.ts` file, which will handle requests to `/api/.
106+
In the `src/app` folder of your new project, create an `api` folder. In that
107+
folder, create a `route.ts` file, which will handle requests to `/api/.
82108

83109
Copy and paste the following code into the `api/route.ts` file:
84110

85111
```ts title="route.ts"
86-
export async function GET() {
112+
export function GET() {
87113
return Response.json("welcome to the dinosaur API");
88114
}
89115
```
90116

91117
This code defines a simple route handler that returns a JSON response with the
92118
string `welcome to the dinosaur API`.
93119

94-
### /api/dinosaurs
120+
### /api/data.json
95121

96-
In the `api` folder, create a folder called `dinosaurs`. In that folder, make a
97-
`data.json` file, which will contain the hard coded dinosaur data. Copy and
98-
paste
122+
In the `api` folder, create a `data.json` file, which will contain the hard
123+
coded dinosaur data. Copy and paste
99124
[this json file](https://raw.githubusercontent.com/denoland/deno-vue-example/main/api/data.json)
100125
into the `data.json` file.
101126

102-
Create a `route.ts` file in the `dinosaurs` directory, which will handle
103-
requests to `/api/dinosaurs`. In this route we'll read the `data.json` file and
104-
return the dinosaurs as JSON:
127+
### /api/dinosaurs
128+
129+
In the `api` folder, create a folder called `dinosaurs`, in that create a
130+
`route.ts` file which will handle requests to `/api/dinosaurs`. In this route
131+
we'll read the `data.json` file and return the dinosaurs as JSON:
105132

106133
```ts title="route.ts"
107134
import data from "./data.json" with { type: "json" };
108135

109-
export async function GET() {
136+
export function GET() {
110137
return Response.json(data);
111138
}
112139
```
@@ -119,12 +146,11 @@ file. In this file we'll read the `data.json` file, find the dinosaur with the
119146
name in the URL, and return it as JSON:
120147

121148
```ts title="route.ts"
122-
import { NextRequest } from "next/server";
123-
import data from "../data.json" with { type: "json" };
149+
import data from "../../data.json" with { type: "json" };
124150

125151
type RouteParams = { params: Promise<{ dinosaur: string }> };
126152

127-
export const GET = async (request: NextRequest, { params }: RouteParams) => {
153+
export const GET = async (_request: Request, { params }: RouteParams) => {
128154
const { dinosaur } = await params;
129155

130156
if (!dinosaur) {
@@ -269,20 +295,25 @@ return (
269295
);
270296
```
271297

298+
### Add some styles
299+
300+
Let's add some basic styles to make the app look nicer. Update your
301+
`app/globals.css` file with the
302+
[styles from this file](https://raw.githubusercontent.com/denoland/tutorial-with-next/refs/heads/main/src/app/globals.css).
303+
272304
## Run the app
273305

274-
Now you can run the app with `deno task dev` and visit `http://localhost:3000`
275-
in your browser to see the list of dinosaurs. Click on a dinosaur to see more
306+
Now you can run the app with `deno run dev` and visit `http://localhost:3000` in
307+
your browser to see the list of dinosaurs. Click on a dinosaur to see more
276308
details!
277309

278-
![demo of the app](./images/how-to/next/dinoapp.gif)
310+
## Deploy the app
279311

280-
## Deploy your app
312+
Now that you have your working Next.js app, you can deploy it to the web with
313+
Deno Deploy<sup>EA</sup>.
281314

282-
Now that you have a Next.js app, you can deploy it to the web with Deno
283-
Deploy<sup>EA</sup>. Deno Deploy requires your code to be hosted on GitHub to
284-
access and deploy it, so first you'll need to create a GitHub repository and
285-
push your app there.
315+
Deno Deploy requires your code to be hosted on GitHub to access and deploy it,
316+
so first you'll need to create a GitHub repository and push your app there.
286317

287318
[Create a new GitHub repository](https://github.com/new), then initialize and
288319
push your app to GitHub:
@@ -296,10 +327,13 @@ git push -u origin main
296327
```
297328

298329
Once your app is on GitHub, you can deploy it on the Deno Deploy<sup>EA</sup>
299-
dashboard:
330+
dashboard.
300331
<a href="https://app.deno.com/" class="docs-cta deploy-cta deploy-button">Deploy
301332
my app</a>
302333

334+
For a walkthrough of deploying your app, check out the
335+
[Deno Deploy tutorial](/examples/deno_deploy_tutorial/).
336+
303337
🦕 Now you can build and run a Next.js app with Deno! To build on your app you
304338
could consider [adding a database](/runtime/tutorials/connecting_to_databases/)
305339
to replace your `data.json` file, or consider

0 commit comments

Comments
 (0)