Skip to content

Commit

Permalink
refactor base handling
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Jul 19, 2024
1 parent 36acb8b commit 8efd085
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions vike/utils/parseUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,15 @@ function parseUrl(url: string, baseServer: string): UrlPrivate {
})

// Origin + pathname
const { protocol, origin, pathnameWithBase } = getPathnameWithBase(urlWithoutHashNorSearch, baseServer)
assert(origin === null || origin === decodeSafe(origin)) // AFAICT decoding the origin is useless
assert(pathnameWithBase.startsWith('/'))
assert(origin === null || url.startsWith(origin))

// `pathnameOriginal`
let { protocol, origin, pathnameAbsoluteWithBase } = getPathnameWithBase(urlWithoutHashNorSearch, baseServer)
const pathnameOriginal = urlWithoutHashNorSearch.slice((origin || '').length)

assertUrlComponents(url, origin, pathnameOriginal, searchOriginal, hashOriginal)

// Base URL
let { pathname, hasBaseServer } = analyzeBaseServer(pathnameWithBase, baseServer)
let { pathname, hasBaseServer } = removeBaseServer(pathnameAbsoluteWithBase, baseServer)
pathname = decodePathname(pathname)

// More props
const href = createUrlFromComponents(origin, pathname, searchOriginal, hashOriginal)
const hrefOriginal = createUrlFromComponents(origin, pathnameOriginal, searchOriginal, hashOriginal)
const host = !origin ? null : origin.slice(protocol!.length)
Expand Down Expand Up @@ -140,22 +135,22 @@ function decodePathname(urlPathname: string) {
function getPathnameWithBase(
url: string,
baseServer: string
): { origin: null | string; pathnameWithBase: string; protocol: null | string } {
): { origin: null | string; pathnameAbsoluteWithBase: string; protocol: null | string } {
// Search and hash already extracted
assert(!url.includes('?') && !url.includes('#'))

// url has origin
{
const { protocol, origin, pathname } = parseOrigin(url)
if (origin) {
return { protocol, origin, pathnameWithBase: pathname }
return { protocol, origin, pathnameAbsoluteWithBase: pathname }
}
assert(pathname === url)
}

// url doesn't have origin
if (url.startsWith('/')) {
return { protocol: null, origin: null, pathnameWithBase: url }
return { protocol: null, origin: null, pathnameAbsoluteWithBase: url }
} else {
// url is a relative path

Expand All @@ -171,8 +166,8 @@ function getPathnameWithBase(
base = baseServer
}

const pathnameWithBase = resolveUrlPathnameRelative(url, base)
return { protocol: null, origin: null, pathnameWithBase: pathnameWithBase }
const pathnameAbsoluteWithBase = resolveUrlPathnameRelative(url, base)
return { protocol: null, origin: null, pathnameAbsoluteWithBase: pathnameAbsoluteWithBase }
}
}
function parseOrigin(url: string): { pathname: string; origin: null | string; protocol: null | string } {
Expand Down Expand Up @@ -244,20 +239,21 @@ function resolveUrlPathnameRelative(pathnameRelative: string, base: string) {
return pathnameAbsolute
}

function analyzeBaseServer(pathnameWithBase: string, baseServer: string): { pathname: string; hasBaseServer: boolean } {
assert(pathnameWithBase.startsWith('/'))
assert(!pathnameWithBase.includes('?'))
assert(!pathnameWithBase.includes('#'))
function removeBaseServer(
pathnameAbsoluteWithBase: string,
baseServer: string
): { pathname: string; hasBaseServer: boolean } {
assert(pathnameAbsoluteWithBase.startsWith('/'))
assert(isBaseServer(baseServer))

// Mutable
let urlPathname = pathnameWithBase
let urlPathname = pathnameAbsoluteWithBase

assert(urlPathname.startsWith('/'))
assert(baseServer.startsWith('/'))

if (baseServer === '/') {
const pathname = pathnameWithBase
const pathname = pathnameAbsoluteWithBase
return { pathname, hasBaseServer: true }
}

Expand All @@ -269,7 +265,7 @@ function analyzeBaseServer(pathnameWithBase: string, baseServer: string): { path
}

if (!urlPathname.startsWith(baseServerNormalized)) {
const pathname = pathnameWithBase
const pathname = pathnameAbsoluteWithBase
return { pathname, hasBaseServer: false }
}
assert(urlPathname.startsWith('/') || urlPathname.startsWith('http'))
Expand Down

0 comments on commit 8efd085

Please sign in to comment.