apostrophecms / apostrophecms/apostrophe

3.x: asset paths are not respecting baseUrl

Open
#3,559 12 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
JavaScript
Stars
4.6k
Forks
650
Avg merge
19h 21m
Merged PRs (30d)
23

Description

## To Reproduce
**UPDATE**: Actually it is more complex than that because of the `apos.prefix` config option. See my first comment with the additional findings.

Add pathname to the baseUrl config value in app.js:
```js
// app.js
require('apostrophe')({
baseUrl: 'http://somedomain.com/root',
// ...
});
```
Assets are linked with paths `/apos-frontend/releases/...` while they should be `/root/apos-frontend/releases/...`

## Expected behavior
Respect the configured `baseUrl` pathname.

## Describe the bug
The whole issue is easily fixable via the [dedicated method](https://github.com/apostrophecms/apostrophe/blob/main/modules/%40apostrophecms/asset/index.js#L593) to get the base asset path. A quick fix would be just adding `self.apos.baseUrl` when appropriate. However, keeping the absolute path sounds like a great idea. This is my monkey patching (used on current staging apps) in order to illustrate it:

```js
// modules/@apostrophecms/asset/index.js
const remote = process.env.APOS_UPLOADFS_ASSETS;

module.exports = {
extendMethods(self) {
return {
getAssetBaseUrl(_super) {
const url = _super();
const baseUrl = self.apos.baseUrl;

if (remote || !baseUrl) {
return url;
}

const { pathname } = new URL(baseUrl);
if (pathname !== '/') {
return pathname + url;
}

return url;
}
};
}
};
```

The above logic could be applied once on init and cached as property. And some additional try - catch security might be needed to ensure URL is not malformed.

Contributor guide

Open the contributing guide

Research direction

Read modules/@apostrophecms/asset/index.js, especially getAssetBaseUrl around the linked location, then trace how baseUrl and apos.prefix affect generated asset URLs. Verify that a non-root baseUrl pathname is included while remote assets remain unaffected; done means assets resolve under the configured /root/ path.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
web-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.