Skip to content

Commit 12ce69e

Browse files
feat: move static pages by default (#816)
Release-As: 4.0.0-beta.8 Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 1918db2 commit 12ce69e

File tree

4 files changed

+29
-44
lines changed

4 files changed

+29
-44
lines changed

docs/large-functions.md

+20-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
## Troubleshooting large functions
22

3-
You may see an error about generated functions being too large. This is because when deploying your site it is packaged into a zipfile, which is limited by AWS to 50MB in size.
4-
There are two possible causes for this, each with its own solution. The list of largest files shown in the build logs will help you see what the cause is.
3+
You may see an error about generated functions being too large. This is because when deploying your site it is packaged
4+
into a zipfile, which is limited by AWS to 50MB in size. There are two possible causes for this, each with its own
5+
solution. The list of largest files shown in the build logs will help you see what the cause is.
56

6-
- **Large dependencies**
7-
This is the most common cause of the problem. Some node modules are very large, mostly those that include native modules. Examples include `electron` and `chromium`. The function bundler is usually able to find out which modules are actually used by your code, but sometimes it will incorrectly include unneeded modules. If this is the case, you can either remove the module from your dependencies if you installed it yourself, or exclude it manually by adding something like this to your `netlify.toml`, changing the value according to the problematic module. The `!` at the beginning of the module path indicates that it should be excluded:
7+
- **Large dependencies** This is the most common cause of the problem. Some node modules are very large, mostly those
8+
that include native modules. Examples include `electron` and `chromium`. The function bundler is usually able to find
9+
out which modules are actually used by your code, but sometimes it will incorrectly include unneeded modules. If this
10+
is the case, you can either remove the module from your dependencies if you installed it yourself, or exclude it
11+
manually by adding something like this to your `netlify.toml`, changing the value according to the problematic module.
12+
The `!` at the beginning of the module path indicates that it should be excluded:
813

9-
```toml
10-
[functions]
11-
included_files = ["!node_modules/a-large-module/**/*"]
12-
```
14+
```toml
15+
[functions]
16+
included_files = ["!node_modules/a-large-module/**/*"]
17+
```
1318

14-
If you do need large modules at runtime (e.g. if you are running Puppeteer in a Next API route), consider changing to a Netlify function which will have less overhead than the equivalent Next.js function.
19+
If you do need large modules at runtime (e.g. if you are running Puppeteer in a Next API route), consider changing to
20+
a Netlify function which will have less overhead than the equivalent Next.js function.
1521

16-
- **Large numbers of pre-rendered pages**
17-
If you have a very large number of pre-rendered pages, these can take up a lot of space in the function. There are two approaches to fixing this. One is to consider deferring the building of the pages. If you return `fallback: "blocking"` from `getStaticPaths` the rendering will be deferred until the first user requests the page. This is a good choice for low-traffic pages. It reduces build and deploy time, and can make your bundle a lot smaller.
18-
19-
The other option is to enable an experimental feature that moves static files out of the function bundle. To do this, set the environment variable `EXPERIMENTAL_MOVE_STATIC_PAGES` to true.
22+
- **Large numbers of pre-rendered pages** If you have a very large number of pre-rendered pages, these can take up a lot
23+
of space in the function. There are two approaches to fixing this. One is to consider deferring the building of the
24+
pages. If you return `fallback: "blocking"` from `getStaticPaths` the rendering will be deferred until the first user
25+
requests the page. This is a good choice for low-traffic pages. It reduces build and deploy time, and can make your
26+
bundle a lot smaller.

docs/middleware.md

+3-20
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,11 @@ export async function middleware(req: NextRequest) {
4444

4545
Because the middleware runs at the origin, it is run _after_ Netlify rewrites and redirects. If a static file is served
4646
by the Netlify CDN then the middleware is never run, as middleware only runs when a page is served by Next.js. This
47-
means that middleware should not be used with the `EXPERIMENTAL_MOVE_STATIC_FILES` option, as this causes
48-
statically-generated pages to be served by the Netlify CDN before any middleware can be run.
47+
means that any pages that match middleware routes are served from the origin rather than the CDN.
4948

5049
There is a bug in Next.js `<=12.0.3` that causes a proxy loop if you try to rewrite to a URL with a host other than
51-
localhost. This bug is fixed in version `12.0.4`. If you are using Next.js `<=12.0.3`, you can work around the bug by
52-
ensuring that when rewriting a request you either use a relative path, or manually set the host to `localhost` if
53-
returning a URL object. For example:
54-
55-
```typescript
56-
export function middleware(req: NextRequest) {
57-
const rewrittenUrl = req.nextUrl
58-
59-
// Change the URL in some way
60-
// ...
61-
62-
// Before returning the URL, set the host to localhost
63-
rewrittenUrl.host = 'localhost'
64-
65-
// ...then rewrite to the changed URL
66-
return NextResponse.rewrite(rewrittenUrl)
67-
}
68-
```
50+
localhost. This bug is fixed in version `12.0.4`, so if you are using middleware you should upgrade to that version or
51+
later.
6952

7053
If you have an issue with Next.js middleware on Netlify while it is beta, particularly if the issue cannot be reproduced
7154
when running locally, then please add a comment to

src/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ module.exports = {
5757
await movePublicFiles({ appDir, publish })
5858

5959
if (process.env.EXPERIMENTAL_MOVE_STATIC_PAGES) {
60+
console.log(
61+
"The flag 'EXPERIMENTAL_MOVE_STATIC_PAGES' is no longer required, as it is now the default. To disable this behavior, set the env var 'SERVE_STATIC_FILES_FROM_ORIGIN' to 'true'",
62+
)
63+
}
64+
65+
if (!process.env.SERVE_STATIC_FILES_FROM_ORIGIN) {
6066
await moveStaticPages({ target, failBuild, netlifyConfig, i18n })
6167
}
6268

test/index.js

-11
Original file line numberDiff line numberDiff line change
@@ -207,32 +207,26 @@ describe('onBuild()', () => {
207207

208208
test('generates static files manifest', async () => {
209209
await moveNextDist()
210-
process.env.EXPERIMENTAL_MOVE_STATIC_PAGES = 'true'
211210
await plugin.onBuild(defaultArgs)
212211
const manifestPath = path.resolve('.next/static-manifest.json')
213212
expect(existsSync(manifestPath)).toBeTruthy()
214213
const data = (await readJson(manifestPath)).sort()
215214
expect(data).toMatchSnapshot()
216-
delete process.env.EXPERIMENTAL_MOVE_STATIC_PAGES
217215
})
218216

219217
test('moves static files to root', async () => {
220218
await moveNextDist()
221-
process.env.EXPERIMENTAL_MOVE_STATIC_PAGES = 'true'
222219
await plugin.onBuild(defaultArgs)
223220
const data = JSON.parse(readFileSync(path.resolve('.next/static-manifest.json'), 'utf8'))
224221

225222
data.forEach((file) => {
226223
expect(existsSync(path.resolve(path.join('.next', file)))).toBeTruthy()
227224
expect(existsSync(path.resolve(path.join('.next', 'server', 'pages', file)))).toBeFalsy()
228225
})
229-
230-
delete process.env.EXPERIMENTAL_MOVE_STATIC_PAGES
231226
})
232227

233228
test('copies default locale files to top level', async () => {
234229
await moveNextDist()
235-
process.env.EXPERIMENTAL_MOVE_STATIC_PAGES = 'true'
236230
await plugin.onBuild(defaultArgs)
237231
const data = JSON.parse(readFileSync(path.resolve('.next/static-manifest.json'), 'utf8'))
238232

@@ -245,19 +239,14 @@ describe('onBuild()', () => {
245239
const trimmed = file.substring(locale.length)
246240
expect(existsSync(path.resolve(path.join('.next', trimmed)))).toBeTruthy()
247241
})
248-
249-
delete process.env.EXPERIMENTAL_MOVE_STATIC_PAGES
250242
})
251243

252244
test('skips static files that match middleware', async () => {
253245
await moveNextDist()
254-
process.env.EXPERIMENTAL_MOVE_STATIC_PAGES = 'true'
255246
await plugin.onBuild(defaultArgs)
256247

257248
expect(existsSync(path.resolve(path.join('.next', 'en', 'middle.html')))).toBeFalsy()
258249
expect(existsSync(path.resolve(path.join('.next', 'server', 'pages', 'en', 'middle.html')))).toBeTruthy()
259-
260-
delete process.env.EXPERIMENTAL_MOVE_STATIC_PAGES
261250
})
262251

263252
test('sets correct config', async () => {

0 commit comments

Comments
 (0)