strapi-community / strapi-tool-dockerize

Easy add support for docker to your strapi project
MIT License
535 stars 35 forks source link

Custom url not set during build time breaks admin routing #124

Open ErgEnn opened 2 weeks ago

ErgEnn commented 2 weeks ago

🐛 Bug Report

🤷‍♀️ What did you do

When updating the server.js to include url: env('PUBLIC_URL', 'http://localhost:1337') the admin interface routes don't get set correctly since the env var is not included in docker build container.

E.g.

server.js url: env('PUBLIC_URL', 'http://localhost:1337')

set environment variable: PUBLIC_URL=http://localhost:1337/cms

run: docker build -t strapi . docker run strapi

Strapi says that admin url is http://localhost:1337/cms/admin

Navigating to http://localhost:1337/cms/admin loads the html, but the JS paths are <script src='/admin/main.....' />, not <script src='/cms/admin/main.....' /> (note the missing /cms).

This is due to admin panel being built in React and it needs to know the url during build time to correctly set the paths.

🙇‍♀️ Expected behavior/code

When environment variable for url is set, the admin dashboard is built using the appropriate url.

👩‍💻 Environment

💡 Possible Solution

Add following lines to generated Dockerfile:

ARG PUBLIC_URL=http://localhost:1337
ENV PUBLIC_URL=${PUBLIC_URL}

Note: the name PUBLIC_URL needs to be the same as the argument in your url: env('PUBLIC_URL') call in server.js

ErgEnn commented 2 weeks ago

Turns out also STRAPI_ADMIN_BACKEND_URL needs to be set during build time since some places us it for url generation e.g. https://github.com/strapi/strapi/blob/171c6daa568f68cd001f5c65d22fb934cd295edf/packages/core/admin/admin/src/render.ts#L31C47-L31C71