Skip to content

Commit a391d32

Browse files
author
Luis Alvarez D
authored
Add docs for multi zones (#11348)
* Updated example * Added single now.json * Added Multi Zones docs * Fix titles * Added back the deploy button
1 parent 4801dcd commit a391d32

File tree

12 files changed

+81
-48
lines changed

12 files changed

+81
-48
lines changed

docs/advanced-features/multi-zones.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Multi Zones
2+
3+
<details>
4+
<summary><b>Examples</b></summary>
5+
<ul>
6+
<li><a href="/examples/with-zones">With Zones</a></li>
7+
</ul>
8+
</details>
9+
10+
A zone is a single deployment of a Next.js app. You can have multiple zones and merge them as a single app.
11+
12+
For example, let's say you have the following apps:
13+
14+
- An app for serving `/blog/**`
15+
- Another app for serving all other pages
16+
17+
With multi zones support, you can merge both these apps into a single one allowing your customers to browse it using a single URL, but you can develop and deploy both apps independently.
18+
19+
## How to define a zone
20+
21+
There are no special zones related APIs. You only need to do following:
22+
23+
- Make sure to keep only the pages you need in your app, meaning that an app can't have pages from another app, if app `A` has `/blog` then app `B` shouldn't have it too.
24+
- Make sure to add an [assetPrefix](/docs/api-reference/next.config.js/cdn-support-with-asset-prefix.md) to avoid conflicts with static files.
25+
26+
## How to merge zones
27+
28+
You can merge zones using any HTTP proxy.
29+
30+
For [ZEIT Now](https://zeit.co/now), you can use a single `now.json` to deploy both apps. It allows you to easily define routing routes for multiple apps like below:
31+
32+
```json
33+
{
34+
"version": 2,
35+
"builds": [
36+
{ "src": "blog/package.json", "use": "@now/next" },
37+
{ "src": "home/package.json", "use": "@now/next" }
38+
],
39+
"routes": [
40+
{ "src": "/blog/_next(.*)", "dest": "blog/_next$1" },
41+
{ "src": "/blog(.*)", "dest": "blog/blog$1" },
42+
{ "src": "(.*)", "dest": "home$1" }
43+
]
44+
}
45+
```
46+
47+
You can also configure a proxy server to route using a set of routes like the ones above, e.g deploy the blog app to `https://blog.example.com` and the home app to `https://home.example.com` and then add a proxy server for both apps in `https://example.com`.

docs/manifest.json

+4
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@
148148
{
149149
"title": "`src` Directory",
150150
"path": "/docs/advanced-features/src-directory.md"
151+
},
152+
{
153+
"title": "Multi Zones",
154+
"path": "/docs/advanced-features/multi-zones.md"
151155
}
152156
]
153157
},
File renamed without changes.

examples/with-zones/README.md

+12-22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
With Next.js you can use multiple apps as a single app using it's [multi-zones feature](https://nextjs.org/docs#multi-zones). This is an example showing how to use it.
44

5+
- All pages should be unique across zones. For example, the `home` app should not have a `pages/blog/index.js` page.
6+
- The `blog` app sets `assetPrefix` so that generated JS bundles are within the `/blog` subfolder.
7+
- To also support the plain `next dev` scenario, `assetPrefix` is set dynamically based on the `BUILDING_FOR_NOW` environment variable, see [`now.json`](now.json) and [`blog/next.config.js`](blog/next.config.js).
8+
- Images and other `/static` assets have to be prefixed manually, e.g., `` <img src={`${process.env.ASSET_PREFIX}/static/image.png`} /> ``, see [`blog/pages/blog/index.js`](blog/pages/blog/index.js).
9+
510
## Deploy your own
611

712
Deploy the example using [ZEIT Now](https://zeit.co/now):
@@ -29,31 +34,16 @@ curl https://codeload.github.com/zeit/next.js/tar.gz/canary | tar -xz --strip=2
2934
cd with-zones
3035
```
3136

32-
## Notes
33-
34-
In this example, we have two apps: 'home' and 'blog'. You can start each app separately, for example:
37+
For every app, install dependencies and run the dev server:
3538

3639
```bash
37-
cd blog
40+
npm install
41+
npm run dev
42+
# or
43+
yarn
3844
yarn dev
3945
```
4046

41-
Then, you can visit <http://localhost:3000> and develop your app.
42-
43-
## Special Notes
44-
45-
- All pages should be unique across zones. For example, the 'home' app should not have a `pages/blog/index.js` page.
46-
- The 'blog' app sets `assetPrefix` so that generated JS bundles are within the `/blog` subfolder.
47-
- To also support the plain `next dev` scenario, `assetPrefix` is set dynamically based on the `BUILDING_FOR_NOW` environment variable, see [`now.json`](now.json) and [`blog/next.config.js`](blog/next.config.js).
48-
- Images and other `/static` assets have to be prefixed manually, e.g., `` <img src={`${process.env.ASSET_PREFIX}/static/image.png`} /> ``, see [`blog/pages/blog/index.js`](blog/pages/blog/index.js).
49-
50-
## Production Deployment
51-
52-
We only need to run `now <app>`, to deploy the app:
53-
54-
```bash
55-
now blog
56-
now home
57-
```
47+
The `home` app will start in the default port (http://localhost:3000), and `blog` will start on http://localhost:4000.
5848

59-
> The rewrite destination in your `now.json` file in the `home` app must be adjusted to point to your deployment.
49+
Deploy it to the cloud with [ZEIT Now](https://zeit.co/import?filter=next.js&utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).

examples/with-zones/blog/now.json

-13
This file was deleted.

examples/with-zones/home/.gitignore

-4
This file was deleted.

examples/with-zones/home/now.json

-8
This file was deleted.

examples/with-zones/home/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "with-zones-home",
33
"version": "1.0.0",
44
"scripts": {
5-
"dev": "next dev -p 4001"
5+
"dev": "next dev"
66
},
77
"dependencies": {
88
"next": "latest",

examples/with-zones/now.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"version": 2,
3+
"build": {
4+
"env": {
5+
"BUILDING_FOR_NOW": "true"
6+
}
7+
},
8+
"builds": [
9+
{ "src": "blog/package.json", "use": "@now/next" },
10+
{ "src": "home/package.json", "use": "@now/next" }
11+
],
12+
"routes": [
13+
{ "src": "/blog/_next(.*)", "dest": "blog/_next$1" },
14+
{ "src": "/blog(.*)", "dest": "blog/blog$1" },
15+
{ "src": "(.*)", "dest": "home$1" }
16+
]
17+
}

0 commit comments

Comments
 (0)