Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: server.origin config trailing slash (fix #6622) #7865

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this line should be removed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's good to have it 👍

logger.warn(
colors.yellow(
`${colors.bold('(!)')} server.origin should not end with "/". Using "${
server.origin
}" instead.`
)
)
}

return server
}

Expand Down