@@ -37,6 +37,26 @@ export function hasLoggedInBefore() {
37
37
return GitpodCookie . isPresent ( document . cookie ) ;
38
38
}
39
39
40
+ const SEGMENT_SEPARATOR = "/" ;
41
+ const getContextUrlFromHash = ( input : string ) : URL | undefined => {
42
+ if ( typeof URL . canParse !== "function" ) {
43
+ return undefined ;
44
+ }
45
+ if ( URL . canParse ( input ) ) {
46
+ return new URL ( input ) ;
47
+ }
48
+
49
+ const chunks = input . split ( SEGMENT_SEPARATOR ) ;
50
+ for ( const chunk of chunks ) {
51
+ input = input . replace ( `${ chunk } ${ SEGMENT_SEPARATOR } ` , "" ) ;
52
+ if ( URL . canParse ( input ) ) {
53
+ return new URL ( input ) ;
54
+ }
55
+ }
56
+
57
+ return undefined ;
58
+ } ;
59
+
40
60
type LoginProps = {
41
61
onLoggedIn ?: ( ) => void ;
42
62
} ;
@@ -49,10 +69,19 @@ export const Login: FC<LoginProps> = ({ onLoggedIn }) => {
49
69
const enterprise = ! ! authProviders . data && authProviders . data . length === 0 ;
50
70
51
71
useEffect ( ( ) => {
52
- if ( urlHash . length > 0 ) {
53
- const url = new URL ( urlHash ) ;
54
- setHostFromContext ( url . host ) ;
55
- setRepoPathname ( url . pathname ) ;
72
+ try {
73
+ if ( urlHash . length > 0 ) {
74
+ const url = new URL ( urlHash ) ;
75
+ setHostFromContext ( url . host ) ;
76
+ setRepoPathname ( url . pathname ) ;
77
+ }
78
+ } catch ( error ) {
79
+ // hash is not a valid URL, try to extract the context URL when there are parts like env vars or other prefixes
80
+ const contextUrl = getContextUrlFromHash ( urlHash ) ;
81
+ if ( contextUrl ) {
82
+ setHostFromContext ( contextUrl . host ) ;
83
+ setRepoPathname ( contextUrl . pathname ) ;
84
+ }
56
85
}
57
86
} , [ urlHash ] ) ;
58
87
0 commit comments