Skip to content

Commit c1667bb

Browse files
authoredJun 20, 2022
fix: SSR with relative base (#8683)
1 parent d11d6ea commit c1667bb

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed
 

‎packages/vite/src/node/config.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,15 @@ export async function resolveConfig(
483483
// resolve public base url
484484
const isBuild = command === 'build'
485485
const relativeBaseShortcut = config.base === '' || config.base === './'
486-
const base = relativeBaseShortcut && !isBuild ? '/' : config.base ?? '/'
486+
487+
// During dev, we ignore relative base and fallback to '/'
488+
// For the SSR build, relative base isn't possible by means
489+
// of import.meta.url. The user will be able to work out a setup
490+
// using experimental.buildAdvancedBaseOptions
491+
const base =
492+
relativeBaseShortcut && (!isBuild || config.build?.ssr)
493+
? '/'
494+
: config.base ?? '/'
487495
let resolvedBase = relativeBaseShortcut
488496
? base
489497
: resolveBaseUrl(base, isBuild, logger, 'base')

‎packages/vite/src/node/plugins/asset.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
105105
) => {
106106
return base.runtime
107107
? `"+${base.runtime(JSON.stringify(filename))}+"`
108-
: base.relative
108+
: base.relative && !config.build.ssr
109109
? absoluteUrlPathInterpolation(filename)
110110
: JSON.stringify((base.url ?? config.base) + filename).slice(1, -1)
111111
}

‎packages/vite/src/node/plugins/css.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
469469
chunkCSS = chunkCSS.replace(assetUrlRE, (_, fileHash, postfix = '') => {
470470
const filename = getAssetFilename(fileHash, config) + postfix
471471
chunk.viteMetadata.importedAssets.add(cleanUrl(filename))
472-
if (assetsBase.relative) {
472+
if (assetsBase.relative && !config.build.ssr) {
473473
// relative base + extracted CSS
474474
const relativePath = path.posix.relative(cssAssetDirname!, filename)
475475
return relativePath.startsWith('.')
@@ -488,7 +488,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
488488
)
489489
chunkCSS = chunkCSS.replace(publicAssetUrlRE, (_, hash) => {
490490
const publicUrl = publicAssetUrlMap.get(hash)!
491-
if (publicBase.relative) {
491+
if (publicBase.relative && !config.build.ssr) {
492492
return relativePathToPublicFromCSS + publicUrl
493493
} else {
494494
// publicBase.runtime has no effect for assets in CSS

‎packages/vite/src/node/plugins/worker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
338338
} else {
339339
// Relative base
340340
let outputFilepath: string
341-
if (assetsBase.relative) {
341+
if (assetsBase.relative && !config.build.ssr) {
342342
outputFilepath = path.posix.relative(
343343
path.dirname(chunk.fileName),
344344
filename

0 commit comments

Comments
 (0)