1
- import React , { PropTypes } from 'react' ;
1
+ import React from 'react' ;
2
2
import { format , parse } from 'url' ;
3
3
4
+ const findCurrentProtocol = ( ) => (
5
+ parse ( window . location . href ) . protocol
6
+ ) ;
7
+
8
+ const redirectToProtocol = ( protocol , { appendSource, disable = false } = { } ) => {
9
+ const currentProtocol = findCurrentProtocol ( ) ;
10
+
11
+ if ( protocol !== currentProtocol ) {
12
+ if ( disable === true ) {
13
+ console . info ( `forceProtocol: would have redirected from "${ currentProtocol } " to "${ protocol } "` ) ;
14
+ } else {
15
+ const url = parse ( window . location . href , true /* parse query string */ ) ;
16
+ url . protocol = protocol ;
17
+ if ( appendSource === true ) {
18
+ url . query . source = currentProtocol ;
19
+ }
20
+ window . location = format ( url ) ;
21
+ }
22
+ }
23
+ } ;
24
+
4
25
/**
5
26
* A Higher Order Component that forces the protocol to change on mount
6
27
*
@@ -14,29 +35,12 @@ const forceProtocol = ({ targetProtocol = 'https', sourceProtocol, disable = fal
14
35
static propTypes = { }
15
36
16
37
componentDidMount ( ) {
17
- this . redirectToProtocol ( targetProtocol , { appendSource : true } ) ;
38
+ redirectToProtocol ( targetProtocol , { appendSource : true , disable } ) ;
18
39
}
19
40
20
41
componentWillUnmount ( ) {
21
42
if ( sourceProtocol != null ) {
22
- this . redirectToProtocol ( sourceProtocol , { appendSource : false } ) ;
23
- }
24
- }
25
-
26
- redirectToProtocol ( protocol , { appendSource } ) {
27
- const currentProtocol = parse ( window . location . href ) . protocol ;
28
-
29
- if ( protocol !== currentProtocol ) {
30
- if ( disable === true ) {
31
- console . info ( `forceProtocol: would have redirected from "${ currentProtocol } " to "${ protocol } "` ) ;
32
- } else {
33
- const url = parse ( window . location . href , true /* parse query string */ ) ;
34
- url . protocol = protocol ;
35
- if ( appendSource === true ) {
36
- url . query . source = currentProtocol ;
37
- }
38
- window . location = format ( url ) ;
39
- }
43
+ redirectToProtocol ( sourceProtocol , { appendSource : false , disable } ) ;
40
44
}
41
45
}
42
46
@@ -65,6 +69,8 @@ const findSourceProtocol = (state, location) => {
65
69
66
70
export default forceProtocol ;
67
71
export {
72
+ findCurrentProtocol ,
68
73
findSourceProtocol ,
74
+ redirectToProtocol ,
69
75
protocols ,
70
76
} ;
0 commit comments