Skip to content

Commit 7202ab6

Browse files
committedAug 18, 2023
fix: apply config.redirects to URL without Base URL
1 parent f45db2a commit 7202ab6

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed
 

‎vite-plugin-ssr/node/runtime/renderPage.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ import {
2020
getGlobalObject,
2121
checkType,
2222
assertUsage,
23-
normalizeUrlPathname
23+
normalizeUrlPathname,
24+
removeBaseServer,
25+
modifyUrlPathname,
26+
prependBase
2427
} from './utils.js'
2528
import {
2629
assertNoInfiniteAbortLoop,
@@ -461,13 +464,18 @@ function normalizePathname(pageContextInit: { urlOriginal: string }) {
461464
}
462465

463466
function getPermanentRedirect(pageContextInit: { urlOriginal: string }) {
464-
const { redirects } = getGlobalContext()
465-
const urlTarget = resolveRedirects(redirects, pageContextInit.urlOriginal)
466-
if (!urlTarget) return null
467-
const httpResponse = createHttpResponseObjectRedirect(
468-
{ url: urlTarget, statusCode: 301 },
469-
pageContextInit.urlOriginal
470-
)
467+
const { redirects, baseServer } = getGlobalContext()
468+
const urlWithoutBase = removeBaseServer(pageContextInit.urlOriginal, baseServer)
469+
let urlPathname: undefined | string
470+
let urlRedirect = modifyUrlPathname(urlWithoutBase, (urlPathname_) => {
471+
urlPathname = urlPathname_
472+
return resolveRedirects(redirects, urlPathname)
473+
})
474+
assert(urlPathname)
475+
if (urlRedirect === urlWithoutBase) return null
476+
urlRedirect = prependBase(urlRedirect, baseServer)
477+
assert(urlRedirect !== pageContextInit.urlOriginal)
478+
const httpResponse = createHttpResponseObjectRedirect({ url: urlRedirect, statusCode: 301 }, urlPathname)
471479
const pageContextHttpResponse = { ...pageContextInit, httpResponse }
472480
return pageContextHttpResponse
473481
}

‎vite-plugin-ssr/utils/parseUrl-extras.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
export { prependBase }
22
export { isBaseAssets }
33
export { normalizeUrlPathname }
4+
export { removeBaseServer }
5+
export { modifyUrlPathname }
46

57
import { assertUrlComponents, createUrlFromComponents, isBaseServer, parseUrl } from './parseUrl.js'
68
import { assert } from './assert.js'
@@ -27,6 +29,14 @@ function prependBase(url: string, baseServer: string): string {
2729
return `${baseServerNormalized}${url}`
2830
}
2931

32+
function removeBaseServer(url: string, baseServer: string): string {
33+
const { hasBaseServer, origin, pathname, pathnameOriginal, searchOriginal, hashOriginal } = parseUrl(url, baseServer)
34+
assert(hasBaseServer)
35+
assertUrlComponents(url, origin, pathnameOriginal, searchOriginal, hashOriginal)
36+
const urlWithoutBase = createUrlFromComponents(origin, pathname, searchOriginal, hashOriginal)
37+
return urlWithoutBase
38+
}
39+
3040
function normalizeBaseAssets(baseAssets: string) {
3141
let baseAssetsNormalized = baseAssets
3242
if (baseAssetsNormalized.endsWith('/')) {

0 commit comments

Comments
 (0)
Please sign in to comment.