Skip to content

Commit d25b9d9

Browse files
DennisOSRMCopilot
andauthored
fix: keep debug map link on current frontend (#414)
Use the current page URL to build the debug viewer link so self-hosted and subpath deployments open their own /debug route instead of resolving elsewhere. Add regression coverage for root and subpath-hosted frontends. Closes #243. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 73b03f6 commit d25b9d9

4 files changed

Lines changed: 80 additions & 4 deletions

File tree

bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bundle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tools.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,10 @@ var Control = L.Control.extend({
8787
_openDebug: function() {
8888
var position = this._map.getCenter(),
8989
zoom = this._map.getZoom(),
90-
prec = 6;
91-
window.open("debug/#" + zoom + "/" + position.lat.toFixed(prec) + "/" + position.lng.toFixed(prec));
90+
prec = 6,
91+
debugUrl = new URL('debug/', window.location.href);
92+
debugUrl.hash = zoom + "/" + position.lat.toFixed(prec) + "/" + position.lng.toFixed(prec);
93+
window.open(debugUrl.href);
9294
},
9395

9496
_openMapillary: function() {

test/tools.test.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* @jest-environment jsdom
3+
*/
4+
5+
'use strict';
6+
7+
jest.mock('leaflet', () => {
8+
function Control() {}
9+
10+
Control.extend = function(props) {
11+
function Extended() {
12+
this.options = Object.assign({}, props.options);
13+
if (props.initialize) {
14+
props.initialize.apply(this, arguments);
15+
}
16+
}
17+
18+
Extended.prototype = Object.assign({}, props);
19+
return Extended;
20+
};
21+
22+
return {
23+
Control: Control,
24+
Mixin: { Events: {} },
25+
setOptions: function(target, options) {
26+
target.options = Object.assign(target.options || {}, options);
27+
}
28+
};
29+
});
30+
31+
describe('tools debug map link', () => {
32+
let tools;
33+
let control;
34+
let openSpy;
35+
36+
beforeEach(() => {
37+
jest.resetModules();
38+
tools = require('../src/tools');
39+
control = tools.control({}, {}, {});
40+
control._map = {
41+
getCenter: function() {
42+
return { lat: 38.8995, lng: -77.0269 };
43+
},
44+
getZoom: function() {
45+
return 13;
46+
}
47+
};
48+
openSpy = jest.spyOn(window, 'open').mockImplementation(function() {});
49+
});
50+
51+
afterEach(() => {
52+
openSpy.mockRestore();
53+
});
54+
55+
test('opens the debug map on the current frontend origin', () => {
56+
window.history.replaceState({}, '', '/?z=13');
57+
58+
control._openDebug();
59+
60+
expect(openSpy).toHaveBeenCalledWith(
61+
'http://localhost/debug/#13/38.899500/-77.026900'
62+
);
63+
});
64+
65+
test('preserves a frontend subpath when opening the debug map', () => {
66+
window.history.replaceState({}, '', '/osrm-frontend/index.html?z=13');
67+
68+
control._openDebug();
69+
70+
expect(openSpy).toHaveBeenCalledWith(
71+
'http://localhost/osrm-frontend/debug/#13/38.899500/-77.026900'
72+
);
73+
});
74+
});

0 commit comments

Comments
 (0)