Skip to content

Commit c0d3199

Browse files
vicbconico974
andauthored
sync aws doc with code (#34)
Co-authored-by: conico974 <[email protected]>
1 parent 61d651b commit c0d3199

File tree

7 files changed

+75
-75
lines changed

7 files changed

+75
-75
lines changed

pages/aws/config/custom_overrides.mdx

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Callout } from 'nextra/components'
33
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.
44

55
<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
77
```ts
88
edgeExternals: ['./customWrapper', './anyOtherOverrideUsed']
99
```
@@ -13,22 +13,22 @@ to your `open-next.config.ts` to avoid the edge runtime to try to compile overri
1313

1414
## Custom converter
1515

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.
1717

1818
You'll still have to use a fallback value during dev as this is not used by the dev server.
1919

2020
```ts
2121
// customConverter.ts
22-
import converter from 'open-next/converters/aws-apigw-v2.js'
23-
import type { Converter } from 'open-next/types/open-next'
22+
import converter from '@opennextjs/aws/overrides/converters/aws-apigw-v2.js'
23+
import type { Converter } from '@opennextjs/aws/types/overrides.js'
2424
import { Config } from 'sst/node/Config'
2525
const mySecretKey = Config.YOUR_SECRET_KEY
2626

2727
export default {
2828
convertFrom: async (event) => {
2929
const result = await converter.convertFrom(event)
3030
return {
31-
...result,
31+
...result,
3232
headers: {
3333
...result.headers,
3434
'inserted-in-converter': '1',
@@ -52,7 +52,7 @@ export default {
5252

5353
```ts
5454
// open-next.config.ts
55-
import type { OpenNextConfig } from 'open-next/types/open-next'
55+
import type { OpenNextConfig } from '@opennextjs/aws/types/open-next.js'
5656

5757
const config = {
5858
default: {
@@ -70,7 +70,7 @@ Here we provide a few examples for some custom wrapper.
7070

7171
```ts
7272
// customWrapper.ts
73-
import defaultWrapper from 'open-next/wrappers/aws-lambda.js'
73+
import defaultWrapper from '@opennextjs/aws/overrides/wrappers/aws-lambda.js'
7474

7575
//Here you can define some globals
7676
declare global {
@@ -88,7 +88,7 @@ export default defaultWrapper
8888

8989
```ts
9090
// open-next.config.ts
91-
import type { OpenNextConfig } from 'open-next/types/open-next'
91+
import type { OpenNextConfig } from '@opennextjs/aws/types/open-next.js'
9292
const config = {
9393
default: {
9494
override: {
@@ -106,8 +106,8 @@ import { NextResponse } from 'next/server'
106106
import type { NextRequest } from 'next/server'
107107

108108
// Here you need to mock the global if not present
109-
// One way to avoid issues with different implementation would be to create an api endpoint
110-
// that uses the exact same logic as the global you defined earlier,
109+
// One way to avoid issues with different implementation would be to create an api endpoint
110+
// that uses the exact same logic as the global you defined earlier,
111111
// and that is only available during development i.e. /api/dev/myApi
112112
if(!globalThis.myApi) {
113113
globalThis.myApi = async () => {
@@ -117,7 +117,7 @@ if(!globalThis.myApi) {
117117

118118
export function middleware(request: NextRequest) {
119119
// You can also send an error in the api endpoint itself
120-
// Or you could add all the dev endpoint in their own lambda
120+
// Or you could add all the dev endpoint in their own lambda
121121
// that you do not deploy in production
122122
if(request.nextUrl.pathname.startsWith('/api/dev') && process.env.NODE_ENV === 'production') {
123123
return NextResponse('This route is only available in development',{
@@ -129,16 +129,16 @@ export function middleware(request: NextRequest) {
129129

130130
// ... your code here
131131
}
132-
132+
133133
```
134134

135135

136136
### Use middy.js with the wrapper
137137

138138
```ts
139139
// customWrapper.ts
140-
import streamingWrapper from 'open-next/wrappers/aws-lambda.js'
141-
import {WrapperHandler} from 'open-next/types/open-next'
140+
import streamingWrapper from '@opennextjs/aws/overrides/wrappers/aws-lambda.js'
141+
import type { WrapperHandler } from '@opennextjs/aws/types/overrides.js'
142142
import middy from '@middy/core'
143143
import httpSecurityHeaders from '@middy/http-security-headers'
144144

@@ -158,7 +158,7 @@ export default {
158158

159159
```ts
160160
// open-next.config.ts
161-
import type { OpenNextConfig } from 'open-next/types/open-next'
161+
import type { OpenNextConfig } from '@opennextjs/aws/types/open-next.js'
162162
const config = {
163163
default: {
164164
override: {
@@ -182,11 +182,11 @@ import type {
182182
APIGatewayProxyEventV2,
183183
APIGatewayProxyResultV2,
184184
} from "aws-lambda";
185-
import type { StreamCreator } from "open-next/http/openNextResponse";
185+
import type { StreamCreator } from "@opennextjs/aws/http/openNextResponse.js";
186186
import { Writable } from "node:stream";
187187

188-
import { WarmerEvent, WarmerResponse } from "open-next/adapters/warmer-function";
189-
import type { WrapperHandler } from "open-next/types/open-next";
188+
import { WarmerEvent, WarmerResponse } from "@opennextjs/aws/adapters/warmer-function.js";
189+
import type { WrapperHandler } from "@opennextjs/aws/types/overrides.js";
190190

191191
type AwsLambdaEvent =
192192
| APIGatewayProxyEventV2
@@ -268,7 +268,7 @@ export default {
268268

269269
```ts
270270
// open-next.config.ts
271-
import type { OpenNextConfig } from 'open-next/types/open-next'
271+
import type { OpenNextConfig } from '@opennextjs/aws/types/open-next.js'
272272
const config = {
273273
default: {
274274
override: {

pages/aws/config/full_example.mdx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff 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:
22
This file need to be at the same place as your `next.config.js` file
33

44
`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)
55

66
For more information about the options here, take a look at the [components section](/aws/components/overview).
77

88
```ts
9-
import type { OpenNextConfig } from 'open-next/types/open-next'
9+
import type { OpenNextConfig } from '@opennextjs/aws/types/open-next.js'
1010
const config = {
1111
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
1313
// for normal lambda as in open-next v2
14-
override: {
14+
override: {
1515
wrapper: "aws-lambda-streaming", // This is necessary to enable lambda streaming
1616
// You can override any part that is a `LazyLoadedOverride` this way
1717
queue: () => Promise.resolve({
@@ -24,7 +24,7 @@ const config = {
2424
},
2525
// Below we define the functions that we want to deploy in a different server
2626
// This is only used if you want to split the server into multiple servers
27-
functions: {
27+
functions: {
2828
ssr: {
2929
routes: [
3030
"app/api/isr/route", "app/api/sse/route", "app/api/revalidateTag/route", // app dir Api routes
@@ -37,7 +37,7 @@ const config = {
3737
},
3838
// This enables the bundled next server which is faster and reduce the size of the server
3939
// This is also experimental and might not work in all cases
40-
experimentalBundledNextServer: true
40+
experimentalBundledNextServer: true
4141
},
4242
pageSsr: {
4343
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 = {
5656
routes: ["app/ssr/page"],
5757
patterns: ["ssr"],
5858
override: {}
59-
}
59+
}
6060
},
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.
6363
// If not set middleware will be bundled inside the servers
6464
// It could be in lambda@edge, cloudflare workers, or anywhere else
6565
// By default it uses lambda@edge
6666
// This is not implemented in the reference construct implementation.
6767
// This is optional, but might be necessary if you split your app into multiple servers
6868
middleware: {
69-
external: true
69+
external: true
7070
}
7171

7272
// Optional
7373
imageOptimization: {
7474
// This is the architecture of the image, it could be x64 or arm64
7575
// This is necessary to bundle the proper version of sharp
76-
arch: "x64",
76+
arch: "x64",
7777
}
7878

7979
// If you want to override the default build command, you can do it here
@@ -83,14 +83,14 @@ const config = {
8383
dangerous: {
8484
// This will disable the tag cache
8585
// You can use it safely on page router, on app router it will break revalidateTag and revalidatePath
86-
disableTagCache: true,
86+
disableTagCache: true,
8787
// This will disable the incremental cache
8888
// 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,
9090
}
9191

9292
//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",
9494

9595
//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 "."
9696
appPath: "app",

pages/aws/config/nx.mdx

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ Here's a detailed exampled of how to add open-next + SST to an existing Nx works
33

44
1. install `open-next`: `pnpm add —save-dev open-next`
55

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:
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:
77
```javascript
88
//@ts-check
9-
9+
1010
// eslint-disable-next-line @typescript-eslint/no-var-requires
1111
const { composePlugins, withNx } = require('@nx/next');
1212
const { join } = require('node:path');
13-
13+
1414
/**
1515
* @type {import('@nx/next/plugins/with-nx').WithNxOptions}
1616
**/
@@ -26,18 +26,18 @@ const nextConfig = {
2626
+ outputFileTracingRoot: join(__dirname, '../../'),
2727
+ },
2828
};
29-
29+
3030
const plugins = [
3131
// Add more Next.js plugins to this list if needed.
3232
withNx,
3333
];
34-
34+
3535
module.exports = composePlugins(...plugins)(nextConfig);
3636
```
3737

38-
3. Create `open-next.config.js` inside your apps root directory, it should look a little something like this:
38+
3. Create `open-next.config.js` inside your apps root directory, it should look a little something like this:
3939
```javascript
40-
import type { OpenNextConfig } from 'open-next/types/open-next';
40+
import type { OpenNextConfig } from '@opennextjs/aws/types/open-next.js';
4141

4242
const config = {
4343
default: {},
@@ -50,13 +50,13 @@ const config = {
5050
export default config;
5151
```
5252

53-
4. Set up nx's targets/tasks
53+
4. Set up nx's targets/tasks
5454

5555
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.
5757

5858
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:
6060
```diff
6161
{
6262
"name": "next-site",
@@ -66,7 +66,7 @@ We do this in your project's `project.json` (in this case, living at `apps/next-
6666
"tags": [],
6767
"targets": {
6868
+ "open-next-build": { // name of the target, this is what you will call
69-
+ "executor": "nx:run-commands",
69+
+ "executor": "nx:run-commands",
7070
+ "dependsOn": ["build"], // this ensures that Nx will build our next app before running this command.
7171
+ "cache": true, // cache the output, good for if you want to use DTE/Nx cloud
7272
+ "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-
7979
}
8080

8181
```
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
8383
```diff
8484
{
8585
"extends": [
@@ -120,13 +120,13 @@ now, when you run `nx open-next-build next-site`, nx will automatically build th
120120

121121
5. Deploying with SST
122122

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!
124124

125125
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/],
126126
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:
127127

128128
- 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:
130130

131131
```typescript
132132
/// <reference path="./.sst/platform/config.d.ts" />
@@ -150,7 +150,7 @@ export default $config({
150150

151151
- now, you probably see some type errors, as SST is not initialized yet, but we can resolve this by running
152152
```bash
153-
$ cd apps/next-site && sst install
153+
$ cd apps/next-site && sst install
154154
```
155155

156156
this will resolve the type issues and initialise SST.
@@ -197,7 +197,7 @@ this will resolve the type issues and initialise SST.
197197
}
198198
```
199199

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:
201201

202202
```diff
203203
{
@@ -208,9 +208,9 @@ this will resolve the type issues and initialise SST.
208208
"tags": [],
209209
"targets": {
210210
"open-next-build": {
211-
"executor": "nx:run-commands",
211+
"executor": "nx:run-commands",
212212
"dependsOn": ["build"],
213-
"cache": true,
213+
"cache": true,
214214
"outputs": ["{projectRoot}/.open-next"],
215215
"options": {
216216
"cwd": "apps/next-site",
@@ -234,7 +234,7 @@ this will resolve the type issues and initialise SST.
234234
+ "args": ["--stage=staging"]
235235
+ },
236236
+ "dev": {
237-
+ "args": ["--stage=development"]
237+
+ "args": ["--stage=development"]
238238
+ }
239239
+ }
240240
+ }
@@ -244,9 +244,9 @@ this will resolve the type issues and initialise SST.
244244

245245
```
246246

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).
248248
```bash
249249
$ nx deploy next-site --configuration dev # using dev configuration (which sets the stage to development)
250250
# nx deploy next-site -c dev # OR
251-
# nx deploy next-site --stage my-stage # Custom Stage
251+
# nx deploy next-site --stage my-stage # Custom Stage
252252
```

0 commit comments

Comments
 (0)