Commit 1fe759d
refactor(common): Align PathLocationStrategy constructor with default factory (angular#46929)
When using the Angular Router, one of `APP_BASE_HREF` or a `<base>` in
the header must be provided. When _not_ using the `RouterModule`,
injecting the `LocationStrategy` will result in the
`PathLocationStrategy` being provided with a default value used in place
of `APP_BASE_HREF` that is `document?.location?.origin ?? ''`.
It can be quite surprising and annoying that once you add `RouterModule`
to the application, suddenly the `APP_BASE_HREF` must be specifically
provide something new when it could use a sensible default instead.
The current behavior (before this commit) is as follows:
* When `RouterModule` is not provided (or the dev doesn't specifically provide
`PathLocationStrategy`): use `DOCUMENT.location?.origin ?? ''`.
Note that the base href in the dom and `APP_BASE_HREF` are not used.
* When `RouterModule` _is_ provided:
1. APP_BASE_HREF if defined
2. Get base href from DOM
3. throw if neither of the two above are defined
This commit updates this behavior to be aligned regardless of `RouterModule`
usage. The order (by default) is now:
1. Developer provided `APP_BASE_HREF`
2. base href from the DOM
3. `location.origin`
4. If none of the above exist, use `''`
This is slightly different than the behavior before. However, I believe
it is more appropriate. For the case without `RouterModule`, it would
likely be surprising that `APP_BASE_HREF` and the base href from the DOM
are ignored by default. For the case with `RouterModule`, we now have a
more sensible fallback/default when neither `APP_BASE_HREF` nor `<base>`
are defined (instead of just throwing an error).
PR Close angular#469291 parent 57d4628 commit 1fe759d
1 file changed
+7
-23
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | 49 | | |
58 | 50 | | |
59 | 51 | | |
| |||
86 | 78 | | |
87 | 79 | | |
88 | 80 | | |
89 | | - | |
90 | | - | |
| 81 | + | |
| 82 | + | |
91 | 83 | | |
92 | 84 | | |
93 | 85 | | |
| |||
110 | 102 | | |
111 | 103 | | |
112 | 104 | | |
113 | | - | |
| 105 | + | |
114 | 106 | | |
115 | 107 | | |
116 | 108 | | |
| |||
120 | 112 | | |
121 | 113 | | |
122 | 114 | | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
| 115 | + | |
| 116 | + | |
133 | 117 | | |
134 | 118 | | |
135 | 119 | | |
| |||
0 commit comments