Repository navigation
Fix #12689 - Remove unnecessary src=null on WMS tile load error - #12691
Conversation
…oad error Setting img.src = null does not clear the image; the DOM coerces it to the string "null", causing a real (always-404) network request. image.setState(3) alone already triggers the source's tileloaderror/imageloaderror events (state-based, no DOM event dependency), making the src=null line redundant.
Verifies the tile error handler no longer sets img.src = null on failure.
|
Converted to draft waiting for 2026.02.00 to be out |
allyoucanmap
left a comment
There was a problem hiding this comment.
A final note( that we should probably track in a different issue), other layer type implementations in OpenLayers have custom tile load function settings image to null that may cause a similar problem (to be investigated):
- WMTSLayer
- ArcGISLayer
- TileProviderLayer
- TMSLayer
| throw new Error(response.dataText); | ||
| } | ||
| }).catch(errorMessage => { | ||
| image.getImage().src = null; // needed to trigger the MS imageloaderror event in Map.onLayerError |
There was a problem hiding this comment.
The src equal to null was there to cover the single tile case that uses a different image class (ol/Image) that does not include the setState function as the tile image class (ol/ImageTile). Anyway the null should be removed and a proposal is to include a separate function to handle both cases:
import ImageState from 'ol/ImageState';
// ...
const setErrorState = (image) => {
if (image.setState) {
image.setState(ImageState.ERROR);
return;
}
image.state = ImageState.ERROR;
image.changed();
};then replace image.setState in the load function:
- image.setState(3);
+ setErrorState(image);there are multiple image.setState in the custom loadFunction and I think we need to update them all
| it('render wms singleTile layer with error does not request a spurious "null" url', (done) => { | ||
| mockAxios.onGet().reply(r => { | ||
| expect(r.url.indexOf('SAMPLE_URL') >= 0 ).toBeTruthy(); | ||
| return [200, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | ||
| "<ows:ExceptionReport xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" + | ||
| " <ows:Exception exceptionCode=\"InvalidParameterValue\" locator=\"srsname\">\n" + | ||
| " <ows:ExceptionText>msWFSGetFeature(): WFS server error. Invalid GetFeature Request</ows:ExceptionText>\n" + | ||
| " </ows:Exception>\n" + | ||
| "</ows:ExceptionReport>"]; | ||
| }); | ||
| const options = { | ||
| type: 'wms', | ||
| visibility: true, | ||
| singleTile: true, | ||
| url: 'SAMPLE_URL', | ||
| name: 'osm:vector_tile' | ||
| }; | ||
| const layer = ReactDOM.render(<OpenlayersLayer | ||
| type="wms" | ||
| options={{ | ||
| ...options | ||
| }} | ||
| map={map} />, document.getElementById("container")); | ||
| expect(layer.layer.getSource()).toBeTruthy(); | ||
| layer.layer.getSource().on('imageloaderror', (e) => { | ||
| setTimeout(() => { | ||
| // the error handler must not set img.src = null, which the DOM coerces | ||
| // to the literal string "null" and resolves into a spurious request | ||
| const img = e.image.getImage(); | ||
| expect(img.getAttribute('src') === 'null' || (img.src || '').endsWith('/null')).toBe(false); | ||
| done(); | ||
| }, 200); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
This test was not reaching the if (headers) { branch inside the custom loadFunction because the requestsConfigurationRules weren't set. It was reaching the img.src = newSrc triggering the default image error. We need two test one for singleTile and one for the tiled layer both setting the request rules to ensure it reach the correct section of the loadFunction, below an update version of the two tests:
it('render wms singleTile layer with error does not request a spurious "null" url', (done) => {
ConfigUtils.setConfigProp('requestsConfigurationRules', [{
urlPattern: '.*\\/geoserver.*',
headers: {
Authorization: 'Bearer ${securityToken}'
}
}]);
setStore({
getState: () => ({
security: {
token: "########-####-####-####-###########"
}
})
});
let request;
mockAxios.onGet().reply((r) => {
request = r;
return [200, new Blob(["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<ows:ExceptionReport xmlns:ows=\"http://www.opengis.net/ows\">\n" +
" <ows:Exception exceptionCode=\"InvalidParameterValue\" locator=\"srsname\">\n" +
" <ows:ExceptionText>WMS server error. Invalid GetMap Request</ows:ExceptionText>\n" +
" </ows:Exception>\n" +
"</ows:ExceptionReport>"], {type: 'text/xml'})];
});
const options = {
type: 'wms',
visibility: true,
singleTile: true,
url: '/geoserver/wms',
name: 'ws:layer'
};
const layer = ReactDOM.render(<OpenlayersLayer
type="wms"
options={{
...options
}}
map={map}
securityToken="########-####-####-####-###########" />, document.getElementById("container"));
ConfigUtils.setConfigProp('requestsConfigurationRules', undefined);
expect(layer.layer.getSource()).toBeTruthy();
layer.layer.getSource().once('imageloaderror', (e) => {
expect(request).toBeTruthy();
expect(request.headers.Authorization).toBe('Bearer ########-####-####-####-###########');
// the error handler must not set img.src = null, which the DOM coerces
// to the literal string "null" and resolves into a spurious request
const img = e.image.getImage();
expect(img.getAttribute('src') === 'null' || (img.src || '').endsWith('/null')).toBe(false);
done();
});
});
it('render wms tiled layer with error does not request a spurious "null" url', (done) => {
ConfigUtils.setConfigProp('requestsConfigurationRules', [{
urlPattern: '.*\\/geoserver.*',
headers: {
Authorization: 'Bearer ${securityToken}'
}
}]);
setStore({
getState: () => ({
security: {
token: "########-####-####-####-###########"
}
})
});
let request;
mockAxios.onGet().reply((r) => {
request = r;
return [200, new Blob(["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<ows:ExceptionReport xmlns:ows=\"http://www.opengis.net/ows\">\n" +
" <ows:Exception exceptionCode=\"InvalidParameterValue\" locator=\"srsname\">\n" +
" <ows:ExceptionText>WMS server error. Invalid GetMap Request</ows:ExceptionText>\n" +
" </ows:Exception>\n" +
"</ows:ExceptionReport>"], {type: 'text/xml'})];
});
const options = {
type: 'wms',
visibility: true,
singleTile: false,
url: '/geoserver/wms',
name: 'ws:tiled_layer'
};
const layer = ReactDOM.render(<OpenlayersLayer
type="wms"
options={{
...options
}}
map={map}
securityToken="########-####-####-####-###########" />, document.getElementById("container"));
ConfigUtils.setConfigProp('requestsConfigurationRules', undefined);
expect(layer.layer.getSource()).toBeTruthy();
layer.layer.getSource().once('tileloaderror', (e) => {
expect(request).toBeTruthy();
expect(request.headers.Authorization).toBe('Bearer ########-####-####-####-###########');
// the error handler must not set img.src = null, which the DOM coerces
// to the literal string "null" and resolves into a spurious request
const img = e.tile.getImage();
expect(img.getAttribute('src') === 'null' || (img.src || '').endsWith('/null')).toBe(false);
done();
});
});…ailure ol/Image has no setState, unlike ol/ImageTile: notify the state change manually so imageloaderror is emitted without setting img.src to null. Tests now configure requestsConfigurationRules to reach the custom load function error branch.
|
PR updated @allyoucanmap |
|
Successfully created backport PR for |
…) (#12749) * Fix #12689 - Remove unnecessary src=null on WMS tile load error Setting img.src = null does not clear the image; the DOM coerces it to the string "null", causing a real (always-404) network request. image.setState(3) alone already triggers the source's tileloaderror/imageloaderror events (state-based, no DOM event dependency), making the src=null line redundant. * Add test for #12689 spurious /null request removal Verifies the tile error handler no longer sets img.src = null on failure. * Fix #12689 - set error state on single tile WMS load failure ol/Image has no setState, unlike ol/ImageTile: notify the state change manually so imageloaderror is emitted without setting img.src to null. Tests now configure requestsConfigurationRules to reach the custom load function error branch. (cherry picked from commit 40197f0)
* Fix #12371 WMS GetMap unhandled error for custom requestsConfigurationRules (#12372) (cherry picked from commit 7fa7016) * Fix #12371 additional condition to prevent tile exception reloding (#12397) (cherry picked from commit fb43e3f) * Fix #12689 - Remove unnecessary src=null on WMS tile load error (#12691) * Fix #12688 - Cap auto-refresh on recurring WMS tile load errors (#12690) (cherry picked from commit 023a180) --------- Co-authored-by: Stefano Cudini <[email protected]> Co-authored-by: Lorenzo Natali <[email protected]>
Closes #12689
Removes
image.getImage().src = null;from the tile load error handler inWMSLayer.js. Assigningnullto an<img>'ssrccoerces to the literal string"null", causing a real (always-404) request to a relative URL namednull.The line immediately after it,
image.setState(3), already sets OpenLayers' internal tile/image state toERROR. Traced throughol/source/UrlTile.js(handleTileChange) andol/source/Image.js(handleImageChange): both dispatchtileloaderror/imageloaderrorpurely based on that internal state, synchronously, with no dependency on the<img>element's nativeerrorDOM event. So thesrc = nullline isn't needed for the error signal MapStore listens for (Map.onLayerError) — it's redundant, and its only effect is the spurious network request.Verified locally: removing the line eliminates the
/nullrequests while the layer's error state andonLayerErrorstill propagate correctly.