You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/aws/config/custom_overrides.mdx
+19-19Lines changed: 19 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ import { Callout } from 'nextra/components'
3
3
In some cases the simple example is not enough, and you want to add more customization to your server. This is where the lazy loaded overrides come in. You can override any part of the server by providing a function that returns a promise that resolves to the override object. This is useful when you want to add custom logic to your server, like adding a custom queue, or adding a custom converter.
4
4
5
5
<Callout>
6
-
Be careful if you use the edge runtime (either in a function or by using the external middleware), we do 2 compilations of the `open-next.config.ts`, one for node and one for the edge runtime. If you're using some custom overrides, you likely want to add
6
+
Be careful if you use the edge runtime (either in a function or by using the external middleware), we do 2 compilations of the `open-next.config.ts`, one for node and one for the edge runtime. If you're using some custom overrides, you likely want to add
@@ -13,22 +13,22 @@ to your `open-next.config.ts` to avoid the edge runtime to try to compile overri
13
13
14
14
## Custom converter
15
15
16
-
Sometimes you might want to modify the object received by OpenNext. For example `Config.YOUR_SECRET_KEY` from sst cannot be used in the middleware, so you might want to add it to the headers. This is where the custom converter comes in. You can add a custom converter to modify the object before it is passed to OpenNext.
16
+
Sometimes you might want to modify the object received by OpenNext. For example `Config.YOUR_SECRET_KEY` from sst cannot be used in the middleware, so you might want to add it to the headers. This is where the custom converter comes in. You can add a custom converter to modify the object before it is passed to OpenNext.
17
17
18
18
You'll still have to use a fallback value during dev as this is not used by the dev server.
Copy file name to clipboardExpand all lines: pages/aws/config/full_example.mdx
+14-14Lines changed: 14 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,17 @@
1
-
Here is a detailed example of an `open-next.config.ts` file:
1
+
Here is a detailed example of an `open-next.config.ts` file:
2
2
This file need to be at the same place as your `next.config.js` file
3
3
4
4
`server` in here could refer to a lambda function, a docker container, a node server or whatever that can support running nodejs code. (Even cloudflare workers in the future)
5
5
6
6
For more information about the options here, take a look at the [components section](/aws/components/overview).
default: { // This is the default server, similar to the server-function in open-next v2
12
-
// You don't have to provide the below, by default it will generate an output
12
+
// You don't have to provide the below, by default it will generate an output
13
13
// for normal lambda as in open-next v2
14
-
override: {
14
+
override: {
15
15
wrapper: "aws-lambda-streaming", // This is necessary to enable lambda streaming
16
16
// You can override any part that is a `LazyLoadedOverride` this way
17
17
queue: () =>Promise.resolve({
@@ -24,7 +24,7 @@ const config = {
24
24
},
25
25
// Below we define the functions that we want to deploy in a different server
26
26
// This is only used if you want to split the server into multiple servers
27
-
functions: {
27
+
functions: {
28
28
ssr: {
29
29
routes: [
30
30
"app/api/isr/route", "app/api/sse/route", "app/api/revalidateTag/route", // app dir Api routes
@@ -37,7 +37,7 @@ const config = {
37
37
},
38
38
// This enables the bundled next server which is faster and reduce the size of the server
39
39
// This is also experimental and might not work in all cases
40
-
experimentalBundledNextServer: true
40
+
experimentalBundledNextServer: true
41
41
},
42
42
pageSsr: {
43
43
routes: ["pages/pageSsr"], // For page dir routes should be in the form `pages/${route}` without the extension, it should match the filesystem
@@ -56,24 +56,24 @@ const config = {
56
56
routes: ["app/ssr/page"],
57
57
patterns: ["ssr"],
58
58
override: {}
59
-
}
59
+
}
60
60
},
61
-
// By setting this, it will create another bundle for the middleware,
62
-
// and the middleware will be deployed in a separate server.
61
+
// By setting this, it will create another bundle for the middleware,
62
+
// and the middleware will be deployed in a separate server.
63
63
// If not set middleware will be bundled inside the servers
64
64
// It could be in lambda@edge, cloudflare workers, or anywhere else
65
65
// By default it uses lambda@edge
66
66
// This is not implemented in the reference construct implementation.
67
67
// This is optional, but might be necessary if you split your app into multiple servers
68
68
middleware: {
69
-
external: true
69
+
external: true
70
70
}
71
71
72
72
// Optional
73
73
imageOptimization: {
74
74
// This is the architecture of the image, it could be x64 or arm64
75
75
// This is necessary to bundle the proper version of sharp
76
-
arch: "x64",
76
+
arch: "x64",
77
77
}
78
78
79
79
// If you want to override the default build command, you can do it here
@@ -83,14 +83,14 @@ const config = {
83
83
dangerous: {
84
84
// This will disable the tag cache
85
85
// You can use it safely on page router, on app router it will break revalidateTag and revalidatePath
86
-
disableTagCache: true,
86
+
disableTagCache: true,
87
87
// This will disable the incremental cache
88
88
// This is generally not recommended, as this is necessary for ISR AND SSG routes as well as the fetch cache
89
-
disableIncrementalCache: true,
89
+
disableIncrementalCache: true,
90
90
}
91
91
92
92
//The path to the target folder of build output from the `buildCommand` option (the path which will contain the `.next` and `.open-next` folders). This path is relative from the current process.cwd() - Optional default to "."
93
-
buildOutputPath: "build",
93
+
buildOutputPath: "build",
94
94
95
95
//The path to the root of the Next.js app's source code. This path is relative from the current process.cwd(). - Optional default to "."
2. Update your `apps/next-site/next.config.js` add `output: ‘standalone’`, and you want to add `experimental.outputFileTracingRoot`, it should look a little like this:
6
+
2. Update your `apps/next-site/next.config.js` add `output: ‘standalone’`, and you want to add `experimental.outputFileTracingRoot`, it should look a little like this:
Now we have open-next configuration set up, you can try to run `open-next build` and depending on whether you have already built your next app or not
56
-
it might even work.
56
+
it might even work.
57
57
58
58
However, we don't want to rely on needing to manually running a build every time we want to deploy a change, so instead we can set up a target.
59
-
We do this in your project's `project.json` (in this case, living at `apps/next-site/project`), we want to find the targets object and update it:
59
+
We do this in your project's `project.json` (in this case, living at `apps/next-site/project`), we want to find the targets object and update it:
60
60
```diff
61
61
{
62
62
"name": "next-site",
@@ -66,7 +66,7 @@ We do this in your project's `project.json` (in this case, living at `apps/next-
66
66
"tags": [],
67
67
"targets": {
68
68
+ "open-next-build": { // name of the target, this is what you will call
69
-
+ "executor": "nx:run-commands",
69
+
+ "executor": "nx:run-commands",
70
70
+ "dependsOn": ["build"], // this ensures that Nx will build our next app before running this command.
71
71
+ "cache": true, // cache the output, good for if you want to use DTE/Nx cloud
72
72
+ "outputs": ["{projectRoot}/.open-next"], // tell nx where the output lives
@@ -79,7 +79,7 @@ We do this in your project's `project.json` (in this case, living at `apps/next-
79
79
}
80
80
81
81
```
82
-
Next we need to add the open-next directory to our eslint's `ignorePatterns` array
82
+
Next we need to add the open-next directory to our eslint's `ignorePatterns` array
83
83
```diff
84
84
{
85
85
"extends": [
@@ -120,13 +120,13 @@ now, when you run `nx open-next-build next-site`, nx will automatically build th
120
120
121
121
5. Deploying with SST
122
122
123
-
Now, we have a built app, ready to deploy, so how do we get it onto SST/AWS ? Good question!
123
+
Now, we have a built app, ready to deploy, so how do we get it onto SST/AWS ? Good question!
124
124
125
125
We are using `sst ion` in this example. i will assume you have already have the cli installed, (if not, check here on how!)[https://ion.sst.dev/],
126
126
but we will not use the SST cli to init this project, because it wants to add a package.json to your next app, and it will look like it's working, but you will end up with a big far server error (all because the package.json overrides whatever nx _thinks_ there should be, and it will miss a bunch of dependencies). we will instead manually set this up:
127
127
128
128
- let's add the sst package with `pnpm add sst@ion`, and the required packages for SST to work with AWS `pnpm add --save-dev aws-cdk-lib constructs @types/aws-lambda`
129
-
- then we want to manually create an `sst.config.ts` file in `apps/next-site` that looks like this:
129
+
- then we want to manually create an `sst.config.ts` file in `apps/next-site` that looks like this:
- now, you probably see some type errors, as SST is not initialized yet, but we can resolve this by running
152
152
```bash
153
-
$ cd apps/next-site && sst install
153
+
$ cd apps/next-site && sst install
154
154
```
155
155
156
156
this will resolve the type issues and initialise SST.
@@ -197,7 +197,7 @@ this will resolve the type issues and initialise SST.
197
197
}
198
198
```
199
199
200
-
- now, if you want to run `sst dev` you can do so with `sst dev "nx dev next-site"` similarly deploying can be done with `sst deploy`...but you probably want to set up that task chaining, again we can do that by adding a target to your app, and setting it's `dependsOn` to the `open-next-build`, here's what it might look like:
200
+
- now, if you want to run `sst dev` you can do so with `sst dev "nx dev next-site"` similarly deploying can be done with `sst deploy`...but you probably want to set up that task chaining, again we can do that by adding a target to your app, and setting it's `dependsOn` to the `open-next-build`, here's what it might look like:
201
201
202
202
```diff
203
203
{
@@ -208,9 +208,9 @@ this will resolve the type issues and initialise SST.
208
208
"tags": [],
209
209
"targets": {
210
210
"open-next-build": {
211
-
"executor": "nx:run-commands",
211
+
"executor": "nx:run-commands",
212
212
"dependsOn": ["build"],
213
-
"cache": true,
213
+
"cache": true,
214
214
"outputs": ["{projectRoot}/.open-next"],
215
215
"options": {
216
216
"cwd": "apps/next-site",
@@ -234,7 +234,7 @@ this will resolve the type issues and initialise SST.
234
234
+ "args": ["--stage=staging"]
235
235
+ },
236
236
+ "dev": {
237
-
+ "args": ["--stage=development"]
237
+
+ "args": ["--stage=development"]
238
238
+ }
239
239
+ }
240
240
+ }
@@ -244,9 +244,9 @@ this will resolve the type issues and initialise SST.
244
244
245
245
```
246
246
247
-
now we can run (or if you want a custom stage, you can simply do `nx deploy next-site --stage this-is-my-stage` and it will be passed directly to the sst command).
247
+
now we can run (or if you want a custom stage, you can simply do `nx deploy next-site --stage this-is-my-stage` and it will be passed directly to the sst command).
248
248
```bash
249
249
$ nx deploy next-site --configuration dev # using dev configuration (which sets the stage to development)
0 commit comments