Skip to content

Commit 31b6529

Browse files
authored
Add more information about allowedHosts for servers using Vite (#2813)
* Add info for Vite server re. allowed hosts * Add JS example version * Revert unintended changes
1 parent 9d346bf commit 31b6529

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

docusaurus/docs/cms/configurations/admin-panel.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,61 @@ export default ({ env }) => ({
118118
</TabItem>
119119
</Tabs>
120120

121+
### Trust additional hosts during development
122+
123+
When you run `strapi develop`, the admin panel is served through a Vite development server that Strapi boots in [middleware mode](https://vite.dev/config/server-options#server-middlewaremode) and proxies through the Koa app. By default, Vite validates the `Host` header against a small set of safe values. Requests that do not match are rejected with an "Invalid Host" response, which prevents you from previewing the admin panel when it is reached through a tunnel, reverse proxy, or custom domain. Strapi exposes Vite's [`server.allowedHosts`](https://vite.dev/config/server-options.html#server-allowedhosts) option so that you can extend that allowlist when necessary.
124+
125+
#### How Strapi loads custom Vite configuration
126+
127+
During development Strapi builds a base Vite configuration, then tries to load a user-supplied config from `./src/admin/vite.config.{js,ts,mjs}`. If the file exports a function it is invoked with the base configuration and Strapi uses the returned value. The project templates ship with an example file that simply merges a custom alias into the provided configuration, which is a good starting point for your own overrides.
128+
129+
#### Allow additional hosts
130+
131+
Create `./src/admin/vite.config.ts` (or `.js`) if it does not already exist and extend the dev-server configuration. The following example snippet adds 2 custom domains while keeping the rest of Strapi's defaults untouched:
132+
133+
<Tabs groupId="js-ts">
134+
135+
<TabItem label="JavaScript" value="js">
136+
137+
```js title="src/admin/vite.config.js"
138+
import { mergeConfig } from 'vite';
139+
140+
export default (config) => {
141+
return mergeConfig(config, {
142+
server: {
143+
allowedHosts: ['preview.my-app.test', '.example-proxy.internal'],
144+
},
145+
});
146+
};
147+
```
148+
149+
</TabItem>
150+
151+
<TabItem label="TypeScript" value="ts">
152+
153+
```ts title="src/admin/vite.config.ts"
154+
import { mergeConfig, type UserConfig } from 'vite';
155+
156+
export default (config: UserConfig) => {
157+
return mergeConfig(config, {
158+
server: {
159+
allowedHosts: ['preview.my-app.test', '.example-proxy.internal'],
160+
},
161+
});
162+
};
163+
```
164+
165+
</TabItem>
166+
</Tabs>
167+
168+
A few tips while configuring `allowedHosts`:
169+
170+
- Pass a string array or `'all'`, matching Vite's accepted shapes.
171+
- Leading dots (`.example.com`) allow any subdomain.
172+
- Combine this option with Strapi's existing `hmr.clientPort` setting when accessing the admin panel through tunnels that rewrite ports.
173+
174+
After saving the file, restart `strapi develop`. Vite will now trust requests whose `Host` header matches the entries you provided, so proxied or tunneled URLs will load without triggering host validation errors.
175+
121176
### Deploy on different servers {#deploy-on-different-servers}
122177

123178
Unless you chose to deploy Strapi's back-end server and admin panel server on different servers, by default:

0 commit comments

Comments
 (0)