Skip to content

Commit 84771e7

Browse files
J3m5JH
andauthored
Add instructions for building Next.js client with SSG locally in Docker Compose (#1918)
* docs(deployment/docker-compose.md): Add a section Building Next.js client locally with SSG Added detailed instructions for building a Next.js client with Static Site Generation (SSG) in a Docker Compose environment. The steps include adjustments to the compose.prod.yaml file, starting the php service container, optional creation of a .env file for the Next.js client, building the pwa service, and finally bringing up the full project. This setup ensures the Next.js client can access the API at build time for static page generation. * docs(create-client/nextjs.md): Generating a production build locally with docker compose Updated the Next.js documentation to include instructions for generating a production build locally using Docker Compose. The update provides a link to the detailed steps in the Docker Compose deployment guide. * docs(deployment/kubernetes.ms): Added Docker build command for SSG in Kubernetes doc Added optional Docker build command for SSG projects in Kubernetes doc. This command allows the PWA to access a locally running API during the build process, useful for generating static pages at build time. * docs(deployement/docker-compose): add a step to override the php env variables To ensure TRUSTED_HOSTS and MERCURE_PUBLIC_URL remain valid when altering SERVER_NAME to use an HTTP scheme or include a port, these variables must be overridden. This adjustment accommodates configurations where SERVER_NAME deviates from the standard HTTPS format or includes port specifications. * docs(deployement/docker-compose): adapt docs for variables update --------- Co-authored-by: JH <[email protected]>
1 parent 9c6d206 commit 84771e7

File tree

3 files changed

+83
-8
lines changed

3 files changed

+83
-8
lines changed

create-client/nextjs.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ yarn dev
9898

9999
Go to `http://localhost:3000/books/` to start using your app.
100100

101+
## Generating a production build locally with docker compose
102+
103+
If you want to generate a production build locally with docker compose, follow [these instructions](../deployment/docker-compose.md)
104+
101105
## Screenshots
102106

103107
![List](images/nextjs/create-client-nextjs-list.png)

deployment/docker-compose.md

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ Go to `https://your-domain-name.example.com` and enjoy!
8989

9090
Alternatively, if you don't want to expose an HTTPS server but only an HTTP one, run the following command:
9191

92-
```console
93-
SERVER_NAME=:80 \
92+
```bash
93+
SERVER_NAME=http://localhost \
94+
MERCURE_PUBLIC_URL=http://localhost/.well-known/mercure \
95+
TRUSTED_HOSTS='^localhost|php$' \
9496
APP_SECRET=ChangeMe \
9597
CADDY_MERCURE_JWT_SECRET=ChangeThisMercureHubJWTSecretKey \
9698
docker compose -f -compose.yaml -f compose.prod.yaml up --wait
@@ -114,3 +116,66 @@ As a shortcut, `private_ranges` may be configured to trust all private IP ranges
114116
+ trusted_proxies private_ranges
115117
+}
116118
```
119+
120+
## Building Next.js client locally with SSG
121+
122+
When deploying API Platform with Docker Compose and you need to build a Next.js client that utilizes Static Site Generation (SSG), a specific setup is required.
123+
This setup ensures the Next.js client can access the API at build time to generate static pages.
124+
125+
### Configuration Steps
126+
127+
#### 1. Adjust the compose.prod.yaml file
128+
129+
Modify the pwa service to ensure network communication between the pwa and php services during the build:
130+
131+
```yaml
132+
pwa:
133+
build:
134+
context: ./pwa
135+
target: prod
136+
network: host
137+
extra_hosts:
138+
- php=127.0.0.1
139+
```
140+
141+
#### 2. Build and start the php service
142+
143+
Begin by starting the php service container:
144+
145+
```bash
146+
SERVER_NAME=http://localhost \
147+
MERCURE_PUBLIC_URL=http://localhost/.well-known/mercure \
148+
TRUSTED_HOSTS='^localhost|php$' \
149+
APP_SECRET=!ChangeMe! \
150+
CADDY_MERCURE_JWT_SECRET=ChangeThisMercureHubJWTSecretKey \
151+
POSTGRES_PASSWORD=!ChangeMe! \
152+
docker compose -f compose.yaml -f compose.prod.yaml up -d --build --wait php
153+
```
154+
155+
#### 3. Optional: Env file with create-client
156+
157+
If your are using the [create-client](../create-client/nextjs.md) generator inside your Next.js client, you need to create a `.env` file in the `pwa` directory with the `NEXT_PUBLIC_ENTRYPOINT` environment variable to ensure the Next.js client knows where to find the API:
158+
159+
```dotenv
160+
NEXT_PUBLIC_ENTRYPOINT=http://php
161+
```
162+
163+
#### 4. Build the pwa service
164+
165+
```bash
166+
docker compose -f compose.yaml -f compose.prod.yaml build pwa
167+
```
168+
169+
#### 5. Finally, bring up the full project
170+
171+
```bash
172+
SERVER_NAME=http://localhost \
173+
MERCURE_PUBLIC_URL=http://localhost/.well-known/mercure \
174+
TRUSTED_HOSTS='^localhost|php$' \
175+
APP_SECRET=!ChangeMe! \
176+
CADDY_MERCURE_JWT_SECRET=ChangeThisMercureHubJWTSecretKey \
177+
POSTGRES_PASSWORD=!ChangeMe! \
178+
docker compose -f compose.yaml -f compose.prod.yaml up -d --wait
179+
```
180+
181+
These steps ensure the Next.js client can statically generate pages by accessing the API during the build process.

deployment/kubernetes.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ docker build -t gcr.io/test-api-platform/caddy:0.1.0 -t gcr.io/test-api-platform
4444
docker build -t gcr.io/test-api-platform/pwa:0.1.0 -t gcr.io/test-api-platform/pwa:latest pwa --target prod
4545
```
4646

47+
Optional: If your pwa project use Static Site Generation (SSG) and you need to build it against the API running locally, you can build the pwa with the command below.
48+
49+
```bash
50+
docker build -t gcr.io/test-api-platform/pwa:0.1.0 -t gcr.io/test-api-platform/pwa:latest pwa --target prod --network=host --add-host php=127.0.0.1
51+
```
52+
4753
#### 2. Push your images to your Docker registry
4854

4955
```console
@@ -53,12 +59,12 @@ docker push gcr.io/test-api-platform/caddy
5359
docker push gcr.io/test-api-platform/pwa
5460
```
5561

56-
Optional push the version images:
62+
Optional: push the version images:
5763

5864
```console
59-
docker push gcr.io/test-api-platform/php:0.1.0
60-
docker push gcr.io/test-api-platform/caddy:0.1.0
61-
docker push gcr.io/test-api-platform/pwa:0.1.0
65+
docker push gcr.io/test-api-platform/php:0.1.0
66+
docker push gcr.io/test-api-platform/caddy:0.1.0
67+
docker push gcr.io/test-api-platform/pwa:0.1.0
6268
```
6369

6470
The result should look similar to these images.
@@ -114,7 +120,7 @@ Replace the values with the image parameters from the stage above.
114120
The parameter `php.appSecret` is the `AppSecret` from ./.env
115121
Fill the rest of the values with the correct settings.
116122
For available options see /helm/api-platform/values.yaml.
117-
If you want a test deploy you can set corsAllowOrigin='*'
123+
If you want a test deploy you can set corsAllowOrigin='\*'
118124

119125
After a successful installation, there is a message at the end.
120126
You can copy these commands and execute them to set a port-forwarding and
@@ -166,7 +172,7 @@ You can upgrade with the same command from the installation and pass all paramet
166172
### 2. Use :latest tags
167173

168174
Infos about [best practices for tagging images for Kubernetes](https://kubernetes.io/docs/concepts/containers/images/)
169-
You have to use the *.image.pullPolicy=Always see the last 3 parameters.
175+
You have to use the \*.image.pullPolicy=Always see the last 3 parameters.
170176

171177
```console
172178
PHP_POD=$(kubectl --namespace=bar get pods -l app=php -o jsonpath="{.items[0].metadata.name}")

0 commit comments

Comments
 (0)