Skip to content

Commit

Permalink
Add docs for multi zones (#11348)
Browse files Browse the repository at this point in the history
* Updated example

* Added single now.json

* Added Multi Zones docs

* Fix titles

* Added back the deploy button
  • Loading branch information
Luis Alvarez D authored Mar 26, 2020
1 parent 4801dcd commit a391d32
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 48 deletions.
47 changes: 47 additions & 0 deletions docs/advanced-features/multi-zones.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Multi Zones

<details>
<summary><b>Examples</b></summary>
<ul>
<li><a href="/examples/with-zones">With Zones</a></li>
</ul>
</details>

A zone is a single deployment of a Next.js app. You can have multiple zones and merge them as a single app.

For example, let's say you have the following apps:

- An app for serving `/blog/**`
- Another app for serving all other pages

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.

## How to define a zone

There are no special zones related APIs. You only need to do following:

- 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.
- Make sure to add an [assetPrefix](/docs/api-reference/next.config.js/cdn-support-with-asset-prefix.md) to avoid conflicts with static files.

## How to merge zones

You can merge zones using any HTTP proxy.

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:

```json
{
"version": 2,
"builds": [
{ "src": "blog/package.json", "use": "@now/next" },
{ "src": "home/package.json", "use": "@now/next" }
],
"routes": [
{ "src": "/blog/_next(.*)", "dest": "blog/_next$1" },
{ "src": "/blog(.*)", "dest": "blog/blog$1" },
{ "src": "(.*)", "dest": "home$1" }
]
}
```

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`.
4 changes: 4 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@
{
"title": "`src` Directory",
"path": "/docs/advanced-features/src-directory.md"
},
{
"title": "Multi Zones",
"path": "/docs/advanced-features/multi-zones.md"
}
]
},
Expand Down
File renamed without changes.
34 changes: 12 additions & 22 deletions examples/with-zones/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

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.

- All pages should be unique across zones. For example, the `home` app should not have a `pages/blog/index.js` page.
- The `blog` app sets `assetPrefix` so that generated JS bundles are within the `/blog` subfolder.
- 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).
- 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).

## Deploy your own

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

## Notes

In this example, we have two apps: 'home' and 'blog'. You can start each app separately, for example:
For every app, install dependencies and run the dev server:

```bash
cd blog
npm install
npm run dev
# or
yarn
yarn dev
```

Then, you can visit <http://localhost:3000> and develop your app.

## Special Notes

- All pages should be unique across zones. For example, the 'home' app should not have a `pages/blog/index.js` page.
- The 'blog' app sets `assetPrefix` so that generated JS bundles are within the `/blog` subfolder.
- 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).
- 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).

## Production Deployment

We only need to run `now <app>`, to deploy the app:

```bash
now blog
now home
```
The `home` app will start in the default port (http://localhost:3000), and `blog` will start on http://localhost:4000.

> The rewrite destination in your `now.json` file in the `home` app must be adjusted to point to your deployment.
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)).
13 changes: 0 additions & 13 deletions examples/with-zones/blog/now.json

This file was deleted.

4 changes: 0 additions & 4 deletions examples/with-zones/home/.gitignore

This file was deleted.

8 changes: 0 additions & 8 deletions examples/with-zones/home/now.json

This file was deleted.

2 changes: 1 addition & 1 deletion examples/with-zones/home/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "with-zones-home",
"version": "1.0.0",
"scripts": {
"dev": "next dev -p 4001"
"dev": "next dev"
},
"dependencies": {
"next": "latest",
Expand Down
File renamed without changes
File renamed without changes
17 changes: 17 additions & 0 deletions examples/with-zones/now.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"version": 2,
"build": {
"env": {
"BUILDING_FOR_NOW": "true"
}
},
"builds": [
{ "src": "blog/package.json", "use": "@now/next" },
{ "src": "home/package.json", "use": "@now/next" }
],
"routes": [
{ "src": "/blog/_next(.*)", "dest": "blog/_next$1" },
{ "src": "/blog(.*)", "dest": "blog/blog$1" },
{ "src": "(.*)", "dest": "home$1" }
]
}

0 comments on commit a391d32

Please sign in to comment.