1 | Uncaught (in promise) DOMException: Failed to execute 'pushState' on 'History': A history state object with URL 'http://example.com.au/dev/despatch' cannot be created in a document with origin 'https://example.com.au' and URL 'https://example.com.au/dev/despatch'. |
I got the above error when attempting to click an Inertia JS Link.
The problem ended up being that I had
<CakePHP 4 Web Application Served via HTTP>---<Ha Proxy terminating HTTPS>----Web Client
The issue was CakePHP 4 was creating a full URL but it was http://example.com/dev/despatch
1 | return Router::url( $this ->getRequest()->getRequestTarget(), true); |
To fix it I modified the $fullBaseUrl
code in config/bootstrap.php
to detect the front end proxy via a header as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /* * Set the full base URL. * This URL is used as the base of all absolute links. */ // mine is set to App.fullBaseUrl = false; $fullBaseUrl = Configure::read( 'App.fullBaseUrl' ); if (! $fullBaseUrl ) { $s = null; // HaProxy is passing HTTP_X_FORWARDED_PROTO header back to CakePHP 4 // read it and change scheme to https if (env( 'HTTPS' ) || env( 'HTTP_X_FORWARDED_PROTO' ) === 'https' ) { $s = 's' ; } $httpHost = env( 'HTTP_HOST' ); if (isset( $httpHost )) { $fullBaseUrl = 'http' . $s . '://' . $httpHost ; } unset( $httpHost , $s ); } if ( $fullBaseUrl ) { Router::fullBaseUrl( $fullBaseUrl ); } unset( $fullBaseUrl ); |
Now it correctly sets the Full Base URL and no longer has the front end error as above.
0 Comments