Open JDruery opened 4 months ago
Hello @JDruery,
Thank you for reporting this issue. The problem you're encountering during the production build appears to be related to the Next.js client's inability to access the API, which is essential for server-side generation (SSG) during the build process. Here's how to resolve it:
1. Set the API Endpoint in the .env file
First, we need to ensure that the Next.js client knows where to find the API. Create a .env
file in the pwa
directory and add the following line:
NEXT_PUBLIC_ENTRYPOINT=http://php
This environment variable (NEXT_PUBLIC_ENTRYPOINT) tells the Next.js client the base URL of your API, enabling it to make requests during the build and runtime.
2. Configure Docker Compose for Network Access
In the compose.prod.yaml
file, we'll adjust the pwa
service to ensure it can communicate with the php
service during the build. Here's the modification:
pwa:
build:
context: ./pwa
target: prod
network: host
extra_hosts:
- "php=127.0.0.1"
These changes allow the pwa build process to access the php service by mapping the php hostname to the local machine, facilitating SSG.
3. Adjust Environment Variables for the PHP Service
For seamless operation of the API and Mercure Hub on a local setup without HTTPS, we need to tweak the php service configuration in the compose.yaml file. This adjustment ensures they can properly handle requests from localhost. The changes below include modifications to the environment variables, with the original settings preserved as comments for easy reference and reversal if needed:
php:
image: ${IMAGES_PREFIX:-}app-php
depends_on:
- database
restart: unless-stopped
environment:
PWA_UPSTREAM: pwa:3000
SERVER_NAME: ${SERVER_NAME:-localhost}, php:80
MERCURE_PUBLISHER_JWT_KEY: ${CADDY_MERCURE_JWT_SECRET:-!ChangeThisMercureHubJWTSecretKey!}
MERCURE_SUBSCRIBER_JWT_KEY: ${CADDY_MERCURE_JWT_SECRET:-!ChangeThisMercureHubJWTSecretKey!}
TRUSTED_PROXIES: ${TRUSTED_PROXIES:-127.0.0.0/8,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16}
# TRUSTED_HOSTS: ^${SERVER_NAME:-example\.com|localhost}|php$$
TRUSTED_HOSTS: ^localhost|php$$
DATABASE_URL: postgresql://${POSTGRES_USER:-app}:${POSTGRES_PASSWORD:-!ChangeMe!}@database:5432/${POSTGRES_DB:-app}?serverVersion=${POSTGRES_VERSION:-15}&charset=${POSTGRES_CHARSET:-utf8}
MERCURE_URL: ${CADDY_MERCURE_URL:-http://php/.well-known/mercure}
# MERCURE_PUBLIC_URL: https://${SERVER_NAME:-localhost}/.well-known/mercure
MERCURE_PUBLIC_URL: http://localhost/.well-known/mercure
MERCURE_JWT_SECRET: ${CADDY_MERCURE_JWT_SECRET:-!ChangeThisMercureHubJWTSecretKey!}
4. Build and start the php service
Let's start the php
service to make it accessible:
SERVER_NAME=http://localhost APP_SECRET=!ChangeMe! CADDY_MERCURE_JWT_SECRET=ChangeThisMercureHubJWTSecretKey POSTGRES_PASSWORD=!ChangeMe! docker compose -f compose.yaml -f compose.prod.yaml up -d --build --wait php
5. Build the pwa service
docker compose -f compose.yaml -f compose.prod.yaml build pwa
6. Bring up the full project
SERVER_NAME=http://localhost APP_SECRET=!ChangeMe! CADDY_MERCURE_JWT_SECRET=ChangeThisMercureHubJWTSecretKey POSTGRES_PASSWORD=!ChangeMe! docker compose -f compose.yaml -f compose.prod.yaml up -d --wait
With these adjustments, your production build should succeed, and the Next.js client will be able to access the API during the build process and at runtime.
I've updated my initial post due to additional issues encountered during runtime. For the API and Mercure to function correctly without HTTPS, it's necessary to revise the TRUSTED_HOSTS and MERCURE_PUBLIC_URL environment variables in the compose.yaml file.
API Platform version(s) affected: 3.2.16
Description
With a fresh installation, I was able to run the generator for the default Greeting entity. The app works in dev mode. When I try to build the app in production mode with http only, it fails with the following message:
How to reproduce