2222 * SOFTWARE.
2323 */
2424
25+ // Webpack-injected; equals output.publicPath.
26+ // Examples: '/', '/latest/', '/pr-preview/pr-123/'.
27+ declare const __webpack_public_path__ : string
28+
2529const MINOR_VERSION_REGEX = / ^ v \d + _ \d + $ /
2630
2731type ParsedUrl = {
28- prPrefix : string
32+ basePrefix : string
2933 minorVersion : string | null
3034 page : string
3135 sectionId : string | undefined
3236}
3337
38+ /**
39+ * Returns '' for a root deploy, '/latest' on instructure.design,
40+ * '/pr-preview/pr-<n>' for PR previews. For sub-host deployments it can be
41+ * '/<repo>/pr-preview/pr-<n>'.
42+ */
43+ function getDeployBase ( ) : string {
44+ if ( typeof __webpack_public_path__ === 'string' && __webpack_public_path__ ) {
45+ return __webpack_public_path__ . replace ( / \/ + $ / , '' )
46+ }
47+ return ''
48+ }
49+
3450function parseCurrentUrl ( ) : ParsedUrl {
3551 const { pathname, hash } = window . location
36- const cleanPath = pathname . replace ( / ^ \/ + | \/ + $ / g, '' )
52+
53+ const deployBase = getDeployBase ( )
54+ let rest = pathname
55+ if ( deployBase && rest . startsWith ( deployBase ) ) {
56+ rest = rest . slice ( deployBase . length )
57+ }
58+
59+ const cleanPath = rest . replace ( / ^ \/ + | \/ + $ / g, '' )
3760 const segments = cleanPath . split ( '/' ) . filter ( Boolean )
3861
39- let prPrefix = ''
62+ let appPrefix = ''
4063 let idx = 0
4164
42- // Detect PR preview prefix: /pr-preview/pr-123
43- if (
44- segments . length >= 2 &&
45- segments [ 0 ] === 'pr-preview' &&
46- segments [ 1 ] . startsWith ( 'pr-' )
47- ) {
48- prPrefix = `/${ segments [ 0 ] } /${ segments [ 1 ] } `
49- idx = 2
50- }
51-
52- // Detect /latest/ prefix
53- if ( idx === 0 && segments [ idx ] === 'latest' ) {
54- prPrefix = '/latest'
65+ if ( segments [ idx ] === 'latest' ) {
66+ appPrefix = '/latest'
5567 idx ++
5668 }
5769
@@ -71,7 +83,7 @@ function parseCurrentUrl(): ParsedUrl {
7183 sectionId = decodeURI ( hash . replace ( / ^ # + / , '' ) )
7284 }
7385
74- return { prPrefix , minorVersion, page, sectionId }
86+ return { basePrefix : deployBase + appPrefix , minorVersion, page, sectionId }
7587}
7688
7789type BuildUrlOptions = {
@@ -80,13 +92,13 @@ type BuildUrlOptions = {
8092}
8193
8294function buildUrl ( targetPage : string , options ?: BuildUrlOptions ) : string {
83- const { prPrefix } = parseCurrentUrl ( )
95+ const parsed = parseCurrentUrl ( )
8496 const minorVersion =
8597 options ?. minorVersion !== undefined
8698 ? options . minorVersion
87- : parseCurrentUrl ( ) . minorVersion
99+ : parsed . minorVersion
88100
89- let url = prPrefix
101+ let url = parsed . basePrefix
90102
91103 if ( minorVersion ) {
92104 url += `/${ minorVersion } `
@@ -123,7 +135,7 @@ function navigateToVersion(version: string | null): void {
123135 * On PR previews this is e.g. `/pr-preview/pr-2425`, otherwise empty string.
124136 */
125137function getAssetBasePath ( ) : string {
126- return parseCurrentUrl ( ) . prPrefix
138+ return getDeployBase ( )
127139}
128140
129141export {
0 commit comments