Skip to content

Fix #12689 - Remove unnecessary src=null on WMS tile load error - #12691

Merged
offtherailz merged 3 commits into
geosolutions-it:masterfrom
offtherailz:fix/wms-tile-error-null-src
Jul 28, 2026
Merged

offtherailz merged 3 commits into
geosolutions-it:masterfrom
offtherailz:fix/wms-tile-error-null-src

Conversation

@offtherailz

Copy link
Copy Markdown
Member

Closes #12689

Removes image.getImage().src = null; from the tile load error handler in WMSLayer.js. Assigning null to an <img>'s src coerces to the literal string "null", causing a real (always-404) request to a relative URL named null.

The line immediately after it, image.setState(3), already sets OpenLayers' internal tile/image state to ERROR. Traced through ol/source/UrlTile.js (handleTileChange) and ol/source/Image.js (handleImageChange): both dispatch tileloaderror/imageloaderror purely based on that internal state, synchronously, with no dependency on the <img> element's native error DOM event. So the src = null line 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 /null requests while the layer's error state and onLayerError still propagate correctly.

…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.
@tdipisa
tdipisa marked this pull request as draft July 21, 2026 14:35
@tdipisa

tdipisa commented Jul 21, 2026

Copy link
Copy Markdown
Member

Converted to draft waiting for 2026.02.00 to be out

@tdipisa
tdipisa marked this pull request as ready for review July 22, 2026 16:32

@allyoucanmap allyoucanmap left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment on lines +360 to +393
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);
});
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@offtherailz

Copy link
Copy Markdown
Member Author

PR updated @allyoucanmap

@offtherailz
offtherailz requested a review from allyoucanmap July 28, 2026 10:52
@offtherailz
offtherailz merged commit 40197f0 into geosolutions-it:master Jul 28, 2026
15 checks passed
@offtherailz

Copy link
Copy Markdown
Member Author

Successfully created backport PR for 2026.02.xx:

offtherailz added a commit that referenced this pull request Jul 29, 2026
…) (#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)
allyoucanmap pushed a commit that referenced this pull request Aug 3, 2026
* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WMS tile load error handler sets img.src = null, causing a spurious network request to a literal "null" URL

3 participants