-
-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: correctly handle base URL that contains a URL origin (fix #149)
- Loading branch information
Showing
5 changed files
with
192 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,108 @@ | ||
import { assert, assertUsage, slice } from '../shared/utils' | ||
import { addUrlOrigin, assert, assertUsage, handleUrlOrigin, slice } from '../shared/utils' | ||
|
||
import { getSsrEnv } from './ssrEnv' | ||
|
||
export { analyzeBaseUrl } | ||
export { prependBaseUrl } | ||
export { removeBaseUrl } | ||
export { startsWithBaseUrl } | ||
export { assertBaseUrl } | ||
|
||
// Possible values: | ||
// Possible Base URL values: | ||
// `base: '/some-nested-path/'` | ||
// `base: 'https://another-origin.example.org/'` | ||
// `base: './'` | ||
|
||
function prependBaseUrl(url: string): string { | ||
let baseUrl = getNormalizedBaseUrl() | ||
if (baseUrl === '/') return url | ||
assert(baseUrl.endsWith('/')) | ||
baseUrl = slice(baseUrl, 0, -1) | ||
assert(!baseUrl.endsWith('/')) | ||
assert(url.startsWith('/')) | ||
return `${baseUrl}${url}` | ||
// `base: 'http://another-origin.example.org/'` | ||
// `base: './'` (WIP: not supported yet) | ||
function assertBaseUrl(baseUrl: string, userErrorMessagePrefix?: string) { | ||
if (!userErrorMessagePrefix) { | ||
assert(baseUrl.startsWith('/') || baseUrl.startsWith('http')) | ||
return | ||
} | ||
assertUsage( | ||
baseUrl.startsWith('/') || baseUrl.startsWith('http') || baseUrl.startsWith('./'), | ||
userErrorMessagePrefix + 'Wrong `base` value `' + baseUrl + '`; `base` should start with `/`, `./`, or `http`.' | ||
) | ||
assertUsage( | ||
!baseUrl.startsWith('./'), | ||
'Relative Base URLs are not supported yet (`baseUrl` that starts with `./`). Open a new GitHub ticket so we can discuss adding support for your use case.' | ||
) | ||
} | ||
|
||
function startsWithBaseUrl(url: string): boolean { | ||
const baseUrl = getNormalizedBaseUrl() | ||
if (baseUrl === '/') return true | ||
return url.startsWith(baseUrl) | ||
} | ||
function removeBaseUrl(url: string): string { | ||
const baseUrl = getNormalizedBaseUrl() | ||
if (baseUrl === '/') return url | ||
assert(startsWithBaseUrl(url)) | ||
function analyzeBaseUrl(url_: string) { | ||
// Unmutable | ||
const urlPristine = url_ | ||
// Mutable | ||
let url = url_ | ||
|
||
assert(url.startsWith('/') || url.startsWith('http')) | ||
const baseUrl = getBaseUrl() | ||
assert(baseUrl.startsWith('/') || baseUrl.startsWith('http')) | ||
|
||
if (baseUrl === '/') { | ||
return { urlWithoutBaseUrl: urlPristine, hasBaseUrl: true } | ||
} | ||
|
||
const { urlWithoutOrigin, urlOrigin } = handleUrlOrigin(url) | ||
let urlOriginHasBeenRemoved = false | ||
{ | ||
const baseUrlOrigin = handleUrlOrigin(baseUrl).urlOrigin | ||
const baseUrlHasOrigin = baseUrlOrigin !== null | ||
let urlHasOrigin = urlOrigin !== null | ||
assertUsage( | ||
!baseUrlHasOrigin || urlHasOrigin, | ||
`You provided a \`baseUrl\` (\`${baseUrl}\`) that contains a URL origin (\`${baseUrlOrigin!}\`) but the \`pageContext.url\` (\`${url}\`) you provided in your server middleware (\`const renderPage = createPageRenderer(/*...*/); renderPage(pageContext);\`) does not contain a URL origin. Either remove the URL origin from your \`baseUrl\` or make sure to always provide the URL origin in \`pageContext.url\`.` | ||
) | ||
if (urlHasOrigin && !baseUrlHasOrigin) { | ||
urlOriginHasBeenRemoved = true | ||
url = urlWithoutOrigin | ||
urlHasOrigin = false | ||
} | ||
assert(urlHasOrigin === baseUrlHasOrigin) | ||
} | ||
|
||
if (!url.startsWith(baseUrl)) { | ||
return { urlWithoutBaseUrl: urlPristine, hasBaseUrl: false } | ||
} | ||
assert(url.startsWith('/') || url.startsWith('http')) | ||
url = url.slice(baseUrl.length) | ||
/* url can actually start with `httpsome-pathname` | ||
assert(!url.startsWith('http')) | ||
*/ | ||
/* `handleUrlOrigin('some-pathname-without-leading-slash')` fails | ||
assert((handleUrlOrigin(url).urlOrigin===null)) | ||
*/ | ||
if (!url.startsWith('/')) url = '/' + url | ||
return url | ||
} | ||
assert(url.startsWith('/')) | ||
|
||
function getNormalizedBaseUrl(): string { | ||
let { baseUrl } = getSsrEnv() | ||
baseUrl = normalizeBaseUrl(baseUrl) | ||
return baseUrl | ||
if (urlOriginHasBeenRemoved) { | ||
assert(urlOrigin !== null) | ||
assert(urlOrigin.startsWith('http')) | ||
assert(url.startsWith('/')) | ||
url = addUrlOrigin(url, urlOrigin) | ||
assert(url.startsWith('http')) | ||
} | ||
|
||
return { urlWithoutBaseUrl: url, hasBaseUrl: true } | ||
} | ||
|
||
function normalizeBaseUrl(baseUrl: string): string { | ||
if (!baseUrl) baseUrl = '/' | ||
if (!baseUrl.endsWith('/')) baseUrl = `${baseUrl}/` | ||
if (!baseUrl.startsWith('/') && !baseUrl.startsWith('http') && !baseUrl.startsWith('./')) baseUrl = `/${baseUrl}` | ||
assert(baseUrl.startsWith('/') || baseUrl.startsWith('http') || baseUrl.startsWith('./')) | ||
assert(baseUrl.endsWith('/')) | ||
return baseUrl | ||
function prependBaseUrl(url: string): string { | ||
let baseUrl = getBaseUrl() | ||
|
||
// Probably safer to remove the origin; `prependBaseUrl()` is used when injecting static assets in HTML; | ||
// origin is useless in static asset URLs, while the origin causes trouble upon `https`/`http` mismatch. | ||
baseUrl = handleUrlOrigin(baseUrl).urlWithoutOrigin | ||
|
||
if (baseUrl === '/') return url | ||
|
||
if (baseUrl.endsWith('/')) { | ||
baseUrl = slice(baseUrl, 0, -1) | ||
} | ||
|
||
// We can and should expect `baseUrl` to not contain `/` doublets. (We cannot expect url to not contain `/` doublets.) | ||
assert(!baseUrl.endsWith('/')) | ||
assert(url.startsWith('/')) | ||
return `${baseUrl}${url}` | ||
} | ||
|
||
function assertBaseUrl(baseUrl: string, errorMessagePrefix = '') { | ||
assertUsage( | ||
baseUrl.startsWith('/') || baseUrl.startsWith('http') || baseUrl.startsWith('./'), | ||
errorMessagePrefix + 'Wrong `base` value `' + baseUrl + '`; `base` should start with `/`, `./`, or `http`.' | ||
) | ||
function getBaseUrl(): string { | ||
const { baseUrl } = getSsrEnv() | ||
assertBaseUrl(baseUrl) | ||
return baseUrl | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters