-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow the default space to be accessed via /s/default
#77109
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,20 +5,22 @@ | |
*/ | ||
import { DEFAULT_SPACE_ID } from '../constants'; | ||
|
||
const spaceContextRegex = /^\/s\/([a-z0-9_\-]+)/; | ||
|
||
export function getSpaceIdFromPath( | ||
requestBasePath: string = '/', | ||
serverBasePath: string = '/' | ||
): string { | ||
let pathToCheck: string = requestBasePath; | ||
): { spaceId: string; pathHasExplicitSpaceIdentifier: boolean } { | ||
const pathToCheck: string = stripServerBasePath(requestBasePath, serverBasePath); | ||
|
||
if (serverBasePath && serverBasePath !== '/' && requestBasePath.startsWith(serverBasePath)) { | ||
pathToCheck = requestBasePath.substr(serverBasePath.length); | ||
} | ||
// Look for `/s/space-url-context` in the base path | ||
const matchResult = pathToCheck.match(/^\/s\/([a-z0-9_\-]+)/); | ||
const matchResult = pathToCheck.match(spaceContextRegex); | ||
|
||
if (!matchResult || matchResult.length === 0) { | ||
return DEFAULT_SPACE_ID; | ||
return { | ||
spaceId: DEFAULT_SPACE_ID, | ||
pathHasExplicitSpaceIdentifier: false, | ||
}; | ||
Comment on lines
+20
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't love that this function now has two responsibilities, but this function is called in some hot paths, and I didn't want to have to do regex matching twice when I'm already doing the work here to extract the space identifier. |
||
} | ||
|
||
// Ignoring first result, we only want the capture group result at index 1 | ||
|
@@ -28,7 +30,10 @@ export function getSpaceIdFromPath( | |
throw new Error(`Unable to determine Space ID from request path: ${requestBasePath}`); | ||
} | ||
|
||
return spaceId; | ||
return { | ||
spaceId, | ||
pathHasExplicitSpaceIdentifier: true, | ||
}; | ||
} | ||
|
||
export function addSpaceIdToPath( | ||
|
@@ -45,3 +50,10 @@ export function addSpaceIdToPath( | |
} | ||
return `${basePath}${requestedPath}`; | ||
} | ||
|
||
function stripServerBasePath(requestBasePath: string, serverBasePath: string) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No functional changes: extracted for readability |
||
if (serverBasePath && serverBasePath !== '/' && requestBasePath.startsWith(serverBasePath)) { | ||
return requestBasePath.substr(serverBasePath.length); | ||
} | ||
return requestBasePath; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,6 @@ import { | |
CoreSetup, | ||
} from 'src/core/server'; | ||
import { format } from 'url'; | ||
import { DEFAULT_SPACE_ID } from '../../../common/constants'; | ||
import { modifyUrl } from '../utils/url'; | ||
import { getSpaceIdFromPath } from '../../../common'; | ||
|
||
|
@@ -28,9 +27,9 @@ export function initSpacesOnRequestInterceptor({ http }: OnRequestInterceptorDep | |
|
||
// If navigating within the context of a space, then we store the Space's URL Context on the request, | ||
// and rewrite the request to not include the space identifier in the URL. | ||
const spaceId = getSpaceIdFromPath(path, serverBasePath); | ||
const { spaceId, pathHasExplicitSpaceIdentifier } = getSpaceIdFromPath(path, serverBasePath); | ||
|
||
if (spaceId !== DEFAULT_SPACE_ID) { | ||
if (pathHasExplicitSpaceIdentifier) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the actual change which enables the default space to be accessed via |
||
const reqBasePath = `/s/${spaceId}`; | ||
|
||
http.basePath.set(request, reqBasePath); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just from naively reading the tests, it's not clear that the default space can be used as an explicit space identifier.
Optional nit: what do you think about adding some additional test cases to make that more clear?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion! Done in
2084f12
(#77109)