Skip to content

Commit

Permalink
Merge pull request #7084 from TerriaJS/fix-cesium-baseUrl
Browse files Browse the repository at this point in the history
Fix Cesium resolving relative baseUrl wrongly by passing in absolute URL
  • Loading branch information
steve9164 committed Mar 27, 2024
2 parents f3f438e + 60e3346 commit c06d822
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -9,6 +9,7 @@
- **Breaking changes:**
- `generateCatalogIndex` now uses `commander` to parse arguments. Run `node ./build/generateCatalogIndex.js --help` for more information.
- Fixed exception thrown from `objectArrayTrait` when a model has 0 strata and a `MergeStrategy` of `topStratum`.
- Fixed a bug with passing a relative baseUrl to Cesium 1.113.0.
- Fix `generateCatalogIndex` after `searchProvider` changes
- Fix bug with relative URLs being ignored in `generateCatalogIndex`
- Fix bug with ArcGisMapServerImageryProvider not correctly identifying if the `tile` endpoint can be used
Expand Down Expand Up @@ -54,6 +55,7 @@
- Add option to enable/disable shortening share URLs via InitSourceData.
- Fix bug in ArcGisMapServerCatalogItem.
- Add examples.
- Upgraded Cesium to 1.113.0 (i.e. `terriajs-cesium@6.2.0` & `terriajs-cesium-widgets@4.4.0`).

#### 8.4.1 - 2023-12-08

Expand Down
29 changes: 16 additions & 13 deletions lib/Models/Terria.ts
Expand Up @@ -428,8 +428,10 @@ export default class Terria {
readonly modelIdShareKeysMap = observable.map<string, string[]>();

/** Base URL for the Terria app. Used for SPA routes */
readonly appBaseHref: string =
typeof document !== "undefined" ? document.baseURI : "/";
readonly appBaseHref: string = ensureSuffix(
typeof document !== "undefined" ? document.baseURI : "/",
"/"
);
/** Base URL to Terria resources */
readonly baseUrl: string = "build/TerriaJS/";

Expand Down Expand Up @@ -691,18 +693,25 @@ export default class Terria {
constructor(options: TerriaOptions = {}) {
makeObservable(this);
if (options.appBaseHref) {
this.appBaseHref = new URL(
options.appBaseHref,
typeof document !== "undefined" ? document.baseURI : "/"
).toString();
this.appBaseHref = ensureSuffix(
new URL(
options.appBaseHref,
typeof document !== "undefined" ? document.baseURI : undefined
).href,
"/"
);
}

if (options.baseUrl) {
this.baseUrl = ensureSuffix(options.baseUrl, "/");
}

// Construct an absolute URL to send to Cesium, as otherwise it resolves relative
// to document.location instead of the correct document.baseURI
const cesiumBaseUrlRelative =
options.cesiumBaseUrl ?? `${this.baseUrl}build/Cesium/build/`;
this.cesiumBaseUrl = ensureSuffix(
options.cesiumBaseUrl ?? `${this.baseUrl}build/Cesium/build/`,
new URL(cesiumBaseUrlRelative, this.appBaseHref).href,
"/"
);
// Casting to `any` as `setBaseUrl` method is not part of the Cesiums' type definitions
Expand Down Expand Up @@ -1249,12 +1258,6 @@ export default class Terria {
new URI(newUrl).filename("").query("").hash("")
);

if (!this.appBaseHref.endsWith("/")) {
console.warn(
`Terria expected appBaseHref to end with a "/" but appBaseHref is "${this.appBaseHref}". Routes may not work as intended. To fix this, try setting the "--baseHref" parameter to a URL with a trailing slash while building your map, or constructing the Terria object with an appropriate appBaseHref (with trailing slash).`
);
}

// /catalog/ and /story/ routes
if (newUrl.startsWith(this.appBaseHref)) {
const pageUrl = new URL(newUrl);
Expand Down
8 changes: 4 additions & 4 deletions test/Models/TerriaSpec.ts
Expand Up @@ -76,17 +76,17 @@ describe("Terria", function () {
baseUrl: "./",
cesiumBaseUrl: "some/path/to/cesium"
});
expect(terria.cesiumBaseUrl).toBe("some/path/to/cesium/");
const path = new URL(terria.cesiumBaseUrl).pathname;
expect(path).toBe("/some/path/to/cesium/");
});

it("should default to a path relative to `baseUrl`", function () {
terria = new Terria({
appBaseHref: "/",
baseUrl: "some/path/to/terria"
});
expect(terria.cesiumBaseUrl).toBe(
"some/path/to/terria/build/Cesium/build/"
);
const path = new URL(terria.cesiumBaseUrl).pathname;
expect(path).toBe("/some/path/to/terria/build/Cesium/build/");
});

it("should update the baseUrl setting in the cesium module", function () {
Expand Down

0 comments on commit c06d822

Please sign in to comment.