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: packages/runtime/docs/runtime.md
+95Lines changed: 95 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,14 @@ This is a description how the runtime internally works.
6
6
7
7
## Next.js milestones
8
8
9
+
This contains a list of milestones where functionality in Next.js was changed that affects the runtime.
10
+
11
+
### [11.1.3-canary.96](https://github.com/vercel/next.js/releases/tag/v11.1.3-canary.96) - 23 Oct 2021
12
+
13
+
With this release a new manifest (`*.nft.json`) was introduced, so that each SSR page also contains the files it requires from third-party libraries.
14
+
This basically runs a version of [`@vercel/nft`](https://www.npmjs.com/package/@vercel/nft) in Next.js instead of this builder.
15
+
Therefore no file-tracing is required after this release.
16
+
9
17
### [10.0.9-canary.4](https://github.com/vercel/next.js/releases/tag/v10.0.9-canary.4) - 09 Mar 2021
10
18
11
19
Beginning with this version the builder no longer sets the environment variable `NEXT_PRIVATE_TARGET=experimental-serverless-trace` and uses the default `target: server`.
@@ -19,6 +27,13 @@ The path of the server output folder also changed from `.serverless` to `.server
19
27
### [9.0.4-canary.1](https://github.com/vercel/next.js/releases/tag/v9.0.4-canary.1) - 06 Aug 2019
20
28
21
29
Beginning with with this version, the builder uses `target: experimental-serverless-trace` instead of `target: serverless` when building Next.js.
30
+
This means that instead of including the whole content of `node-modules` in the Lambda, only files that are actually used in code are included in the Lambda package (File-tracing).
31
+
This is done via the [`@vercel/nft`](https://www.npmjs.com/package/@vercel/nft) package
32
+
33
+
### [7.0.3-alpha.0](https://github.com/vercel/next.js/releases/tag/v7.0.3-alpha.0) - 30 Oct 2018
34
+
35
+
All versions prior to this released are considered as legacy versions by the build tool.
36
+
Legacy means that no file-tracing is active and everything from the `node_modules` is included in the Lambda.
22
37
23
38
## Procedure
24
39
@@ -81,3 +96,83 @@ module.exports = function (...args) {
81
96
return finalConfig;
82
97
};
83
98
```
99
+
100
+
### 3. Build Next.js
101
+
102
+
Building Next.js is simply running `next build` which produces a bunch of files that are written to the `.next` folder.
103
+
104
+
The following files are then used in the further process:
105
+
106
+
- Routes manifest
107
+
- Images manifest
108
+
- prerender manifest
109
+
Prerendered routes emit a `.html` file but should not be treated as a static page.
110
+
Lazily prerendered routes have a fallback `.html` file on newer Next.js versions so we need to also not treat it as a static page here.
111
+
112
+
### 4. Building Routes
113
+
114
+
The following things are extracted from the routes manifest:
115
+
116
+
- redirects
117
+
- rewrites
118
+
- dataroutes (available at `/_next/data`) for both dynamic SSG and SSP pages.
119
+
- Can also have a nextLocale
120
+
121
+
### 5. Create image config
122
+
123
+
The image config is created from the Images manifest.
124
+
125
+
### 6. Build Lambdas
126
+
127
+
#### Important variables
128
+
129
+
-`pagesDir`
130
+
Path to the server (or serverless) directory inside the `.next folder`
131
+
-`pages`
132
+
Contains a list of all `.js` files exported in the `pagesDir`.
133
+
Has at least 1 entry (`_error.js`).
134
+
-`staticPageFiles`
135
+
Container a list of all `.html` files exported in the `pagesDir`
136
+
-`staticPages`
137
+
-`dynamicPages`
138
+
Contains all SSR pages that have a dynamic pattern in its path (e.g. `[someId].js`)
139
+
-`pseudoLayers`
140
+
-`apiPseudoLayers`
141
+
-`nonLambdaSsgPages`
142
+
-`apiLambdaGroups`
143
+
-`pageLambdaGroups`
144
+
145
+
First the static output (prerendered HTML pages `.html`) is analyzed.
146
+
They are categorized into static routes (e.g. `/test`) and dynamic pages (`/[slug]`).
147
+
148
+
Then it is determined if the 404 page is static (prerendered) or dynamic (SSR).
149
+
150
+
Each static route from the prerender manifest is then checked if it can be added to `nonLambdaSsgPages`.
151
+
152
+
#### 6.x Tracing
153
+
154
+
> Tracing is only executed for Node.js versions `>9.0.4-canary.1`
155
+
156
+
Then the tracing is executed.
157
+
158
+
For this every page from `pages` is categorized into `apiPages` or `nonApiPages`.
159
+
Only pages that are not already in `nonLambdaSsgPages` are added to `nonApiPages`.
160
+
161
+
Then nft is executed for both the `apiPages` and `nonApiPages`.
162
+
163
+
From that the traced files are collected into `tracedFiles` and `apiTracedFiles`.
164
+
Then a pseudoLayer is created from `tracedFiles` and `apiTracedFiles`, which contain the dependencies of the pages and apiPages.
165
+
166
+
A pseudoLayer is an object that contains for each filePath the compressed buffer of the original file (PseudoFile) or the information about the symlink of the file (PseudoSymbolicLink).
167
+
The created pseudoLayers are then pushed to `pseudoLayers` and `apiPseudoLayers`
168
+
169
+
#### 6.x Creating serverless functions
170
+
171
+
-**Shared Lambdas**
172
+
Every page in `pages` (that is not `_app.js` or `_document.js`) is assigned to a LambdaGroup.
173
+
If the page is already in `nonLambdaSsgPages` it is not added to the Lambda.
174
+
A LambdaGroup is a collection of multiple pages or ApiPages that can be combined into a single Lambda (Which reduces the total number of Lambdas needed for serving the app.)
175
+
A LambdaGroup has the name of the form `__NEXT_PAGE_LAMBDA_<GroupIndex>` for pages and `__NEXT_API_LAMBDA_<GroupIndex>` for apiPages.
176
+
The aim is that each LambdaGroup stays below 50mb (Compressed code size limit from AWS Lambda), so when a LambdaGroup exceeds this limit, a new group is crated.
177
+
178
+
For each page is then a new route is added to `dynamicPageLambdaRoutes`
0 commit comments