Skip to content

Commit 57d7d83

Browse files
authored
Fix hash router for urls ending with a hash (#4730)
1 parent be7e209 commit 57d7d83

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
### 🐞 Bug fixes
77
- Fix circle won't render on mesa 24.1 with AMD GPU ([#4062](https://github.com/maplibre/maplibre-gl-js/issues/4062))
8+
- Fix hash router for urls ending with a hashtag ([#4730](https://github.com/maplibre/maplibre-gl-js/pull/4730))
89
- _...Add new stuff here..._
910

1011
## 4.7.0

src/ui/hash.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,71 @@ describe('hash', () => {
327327
expect(window.location.hash).toBe('#baz&foo=bar');
328328
});
329329

330+
test('initialize http://localhost/#', () => {
331+
window.location.href = 'http://localhost/#';
332+
createHash().addTo(map);
333+
map.setZoom(3);
334+
expect(window.location.hash).toBe('#3/0/0');
335+
expect(window.location.href).toBe('http://localhost/#3/0/0');
336+
map.setCenter([2.0, 1.0]);
337+
expect(window.location.hash).toBe('#3/1/2');
338+
expect(window.location.href).toBe('http://localhost/#3/1/2');
339+
});
340+
341+
test('initialize http://localhost/##', () => {
342+
window.location.href = 'http://localhost/##';
343+
createHash().addTo(map);
344+
map.setZoom(3);
345+
expect(window.location.hash).toBe('#3/0/0');
346+
expect(window.location.href).toBe('http://localhost/#3/0/0');
347+
map.setCenter([2.0, 1.0]);
348+
expect(window.location.hash).toBe('#3/1/2');
349+
expect(window.location.href).toBe('http://localhost/#3/1/2');
350+
});
351+
352+
test('initialize http://localhost#', () => {
353+
window.location.href = 'http://localhost#';
354+
createHash().addTo(map);
355+
map.setZoom(4);
356+
expect(window.location.hash).toBe('#4/0/0');
357+
expect(window.location.href).toBe('http://localhost/#4/0/0');
358+
map.setCenter([2.0, 1.0]);
359+
expect(window.location.hash).toBe('#4/1/2');
360+
expect(window.location.href).toBe('http://localhost/#4/1/2');
361+
});
362+
363+
test('initialize http://localhost/', () => {
364+
window.location.href = 'http://localhost/';
365+
createHash().addTo(map);
366+
map.setZoom(5);
367+
expect(window.location.hash).toBe('#5/0/0');
368+
expect(window.location.href).toBe('http://localhost/#5/0/0');
369+
map.setCenter([2.0, 1.0]);
370+
expect(window.location.hash).toBe('#5/1/2');
371+
expect(window.location.href).toBe('http://localhost/#5/1/2');
372+
});
373+
374+
test('initialize default value for window.location.href', () => {
375+
createHash().addTo(map);
376+
map.setZoom(5);
377+
expect(window.location.hash).toBe('#5/0/0');
378+
expect(window.location.href).toBe('http://localhost/#5/0/0');
379+
map.setCenter([2.0, 1.0]);
380+
expect(window.location.hash).toBe('#5/1/2');
381+
expect(window.location.href).toBe('http://localhost/#5/1/2');
382+
});
383+
384+
test('initialize http://localhost', () => {
385+
window.location.href = 'http://localhost';
386+
createHash().addTo(map);
387+
map.setZoom(4);
388+
expect(window.location.hash).toBe('#4/0/0');
389+
expect(window.location.href).toBe('http://localhost/#4/0/0');
390+
map.setCenter([2.0, 1.0]);
391+
expect(window.location.hash).toBe('#4/1/2');
392+
expect(window.location.href).toBe('http://localhost/#4/1/2');
393+
});
394+
330395
test('map#remove', () => {
331396
const container = window.document.createElement('div');
332397
Object.defineProperty(container, 'clientWidth', {value: 512});

src/ui/hash.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export class Hash {
118118

119119
_updateHashUnthrottled = () => {
120120
// Replace if already present, else append the updated hash string
121-
const location = window.location.href.replace(/(#.+)?$/, this.getHashString());
121+
const location = window.location.href.replace(/(#.*)?$/, this.getHashString());
122122
window.history.replaceState(window.history.state, null, location);
123123
};
124124

0 commit comments

Comments
 (0)