@@ -10,19 +10,14 @@ oldUrl:
10
10
server-side-rendered applications. It is built on top of React and provides a
11
11
lot of features out of the box.
12
12
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 ) .
16
18
17
19
![ demo of the app] ( ./images/how-to/next/dinoapp.gif )
18
20
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
-
26
21
## Create a Next.js app with Deno
27
22
28
23
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
36
31
TypeScript.
37
32
38
33
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:
40
35
41
36
``` sh
42
- deno install
37
+ deno install --allow-scripts
43
38
```
44
39
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:
47
43
48
44
``` json deno.json
49
45
{
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
+ }
51
65
}
52
66
```
53
67
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
+
54
76
Now you can serve your new Next.js app:
55
77
56
78
``` sh
57
- deno task dev
79
+ deno run dev
58
80
```
59
81
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.
62
88
63
89
## Add a backend
64
90
@@ -77,36 +103,37 @@ dinosaur based on the name in the URL.
77
103
78
104
### /api/
79
105
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/.
82
108
83
109
Copy and paste the following code into the ` api/route.ts ` file:
84
110
85
111
``` ts title="route.ts"
86
- export async function GET() {
112
+ export function GET() {
87
113
return Response .json (" welcome to the dinosaur API" );
88
114
}
89
115
```
90
116
91
117
This code defines a simple route handler that returns a JSON response with the
92
118
string ` welcome to the dinosaur API ` .
93
119
94
- ### /api/dinosaurs
120
+ ### /api/data.json
95
121
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
99
124
[ this json file] ( https://raw.githubusercontent.com/denoland/deno-vue-example/main/api/data.json )
100
125
into the ` data.json ` file.
101
126
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:
105
132
106
133
``` ts title="route.ts"
107
134
import data from " ./data.json" with { type: " json" };
108
135
109
- export async function GET() {
136
+ export function GET() {
110
137
return Response .json (data );
111
138
}
112
139
```
@@ -119,12 +146,11 @@ file. In this file we'll read the `data.json` file, find the dinosaur with the
119
146
name in the URL, and return it as JSON:
120
147
121
148
``` 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" };
124
150
125
151
type RouteParams = { params: Promise <{ dinosaur: string }> };
126
152
127
- export const GET = async (request : NextRequest , { params }: RouteParams ) => {
153
+ export const GET = async (_request : Request , { params }: RouteParams ) => {
128
154
const { dinosaur } = await params ;
129
155
130
156
if (! dinosaur ) {
@@ -269,20 +295,25 @@ return (
269
295
);
270
296
```
271
297
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
+
272
304
## Run the app
273
305
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
276
308
details!
277
309
278
- ![ demo of the app] ( ./images/how-to/next/dinoapp.gif )
310
+ ## Deploy the app
279
311
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 >.
281
314
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.
286
317
287
318
[ Create a new GitHub repository] ( https://github.com/new ) , then initialize and
288
319
push your app to GitHub:
@@ -296,10 +327,13 @@ git push -u origin main
296
327
```
297
328
298
329
Once your app is on GitHub, you can deploy it on the Deno Deploy<sup >EA</sup >
299
- dashboard:
330
+ dashboard.
300
331
<a href =" https://app.deno.com/ " class =" docs-cta deploy-cta deploy-button " >Deploy
301
332
my app</a >
302
333
334
+ For a walkthrough of deploying your app, check out the
335
+ [ Deno Deploy tutorial] ( /examples/deno_deploy_tutorial/ ) .
336
+
303
337
🦕 Now you can build and run a Next.js app with Deno! To build on your app you
304
338
could consider [ adding a database] ( /runtime/tutorials/connecting_to_databases/ )
305
339
to replace your ` data.json ` file, or consider
0 commit comments