Skip to content

Commit

Permalink
docs: server.origin config trailing slash (fix #6622) (#7865)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Apr 27, 2022
1 parent b092697 commit 5c1ee5a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/config/index.md
Expand Up @@ -706,7 +706,7 @@ Defines the origin of the generated asset URLs during development.
```js
export default defineConfig({
server: {
origin: 'http://127.0.0.1:8080/'
origin: 'http://127.0.0.1:8080'
}
})
```
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/config.ts
Expand Up @@ -461,7 +461,7 @@ export async function resolveConfig(
)
: ''

const server = resolveServerOptions(resolvedRoot, config.server)
const server = resolveServerOptions(resolvedRoot, config.server, logger)

const optimizeDeps = config.optimizeDeps || {}

Expand Down
18 changes: 17 additions & 1 deletion packages/vite/src/node/server/index.ts
Expand Up @@ -56,6 +56,7 @@ import type { OptimizedDeps } from '../optimizer'
import { resolveHostname } from '../utils'
import { searchForWorkspaceRoot } from './searchRoot'
import { CLIENT_DIR } from '../constants'
import type { Logger } from '../logger'
import { printCommonServerUrls } from '../logger'
import { performance } from 'perf_hooks'
import { invalidatePackageData } from '../packages'
Expand Down Expand Up @@ -92,6 +93,8 @@ export interface ServerOptions extends CommonServerOptions {
fs?: FileSystemServeOptions
/**
* Origin for the generated asset URLs.
*
* @example `http://127.0.0.1:8080`
*/
origin?: string
/**
Expand Down Expand Up @@ -701,7 +704,8 @@ function resolvedAllowDir(root: string, dir: string): string {

export function resolveServerOptions(
root: string,
raw?: ServerOptions
raw: ServerOptions | undefined,
logger: Logger
): ResolvedServerOptions {
const server: ResolvedServerOptions = {
preTransformRequests: true,
Expand All @@ -727,6 +731,18 @@ export function resolveServerOptions(
allow: allowDirs,
deny
}

if (server.origin?.endsWith('/')) {
server.origin = server.origin.slice(0, -1)
logger.warn(
colors.yellow(
`${colors.bold('(!)')} server.origin should not end with "/". Using "${
server.origin
}" instead.`
)
)
}

return server
}

Expand Down

0 comments on commit 5c1ee5a

Please sign in to comment.