Skip to content

Commit

Permalink
fix: SSR with relative base (#8683)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Jun 20, 2022
1 parent d11d6ea commit c1667bb
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
10 changes: 9 additions & 1 deletion packages/vite/src/node/config.ts
Expand Up @@ -483,7 +483,15 @@ export async function resolveConfig(
// resolve public base url
const isBuild = command === 'build'
const relativeBaseShortcut = config.base === '' || config.base === './'
const base = relativeBaseShortcut && !isBuild ? '/' : config.base ?? '/'

// During dev, we ignore relative base and fallback to '/'
// For the SSR build, relative base isn't possible by means
// of import.meta.url. The user will be able to work out a setup
// using experimental.buildAdvancedBaseOptions
const base =
relativeBaseShortcut && (!isBuild || config.build?.ssr)
? '/'
: config.base ?? '/'
let resolvedBase = relativeBaseShortcut
? base
: resolveBaseUrl(base, isBuild, logger, 'base')
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/asset.ts
Expand Up @@ -105,7 +105,7 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
) => {
return base.runtime
? `"+${base.runtime(JSON.stringify(filename))}+"`
: base.relative
: base.relative && !config.build.ssr
? absoluteUrlPathInterpolation(filename)
: JSON.stringify((base.url ?? config.base) + filename).slice(1, -1)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/css.ts
Expand Up @@ -469,7 +469,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
chunkCSS = chunkCSS.replace(assetUrlRE, (_, fileHash, postfix = '') => {
const filename = getAssetFilename(fileHash, config) + postfix
chunk.viteMetadata.importedAssets.add(cleanUrl(filename))
if (assetsBase.relative) {
if (assetsBase.relative && !config.build.ssr) {
// relative base + extracted CSS
const relativePath = path.posix.relative(cssAssetDirname!, filename)
return relativePath.startsWith('.')
Expand All @@ -488,7 +488,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
)
chunkCSS = chunkCSS.replace(publicAssetUrlRE, (_, hash) => {
const publicUrl = publicAssetUrlMap.get(hash)!
if (publicBase.relative) {
if (publicBase.relative && !config.build.ssr) {
return relativePathToPublicFromCSS + publicUrl
} else {
// publicBase.runtime has no effect for assets in CSS
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/worker.ts
Expand Up @@ -338,7 +338,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
} else {
// Relative base
let outputFilepath: string
if (assetsBase.relative) {
if (assetsBase.relative && !config.build.ssr) {
outputFilepath = path.posix.relative(
path.dirname(chunk.fileName),
filename
Expand Down

0 comments on commit c1667bb

Please sign in to comment.