October: MediaLibrary returns only fully qualified URLs

Created on 23 Jan 2019  Â·  25Comments  Â·  Source: octobercms/october

With one of latest upgrade came new feature based on link policy - MediaLibrary class returns only fully qualified URLs.
For my usecase is this solution unhandy, I prefer images with relative URLs, based on setting of cms.storage.media.path. I think this would be default solution and absolute paths would become optional.

Proposal of solution

Add to config/cms.php option cms.storage.media.forceAbsolutePath to switch on current behavior. Implement this option in MediaLibrary::getPathUrl.
Or maybe @mjauvin or @w20k will come with something better.

Completed Bug

Most helpful comment

Fixed by https://github.com/octobercms/october/commit/ccb2c372bb1fb3980c1240ffb2f7aeef5e786dae. Media library will no longer force absolute URLs unless cms.linkPolicy is set to force.

All 25 comments

@Rike-cz @mjauvin Why do you want relative URLs over fully qualified URLs? Can you provide a pros vs cons list?

@LukeTowers I agree that if the image src URL is generated dynamically, that should be fine (although I frankly don't see the need to have a fully qualified URL if the resource is local to the website), but if a static version of the website is generated, the static website's image URLs will not be usable with a different base website URL.

I agree with @mjauvin. My use case is what he wrote. I create new website locally on some-domain.localhost, make a tuning according to customer on some-domain.my-site.tld and finally move it on his domain some-domain.tld. I can do that very simply because of Laravel/OctoberCMS environments setting. But I need to fill website with content (StaticPages etc.) on my localhost, and forcing fully qualified URLs for images pasted to StaticPage content is unhandy.
I really do not know why relative paths are rejected. What's wrong with them?

So the problem you are describing is that the MediaLibrary generates FQ urls that are stored within files if used together with StaticPages, making them hardcoded instead of dynamic (relative) URLs that are/can be generated by the CMS on runtime.

@CptMeatball Exactly!

Also there is an issue if you want to add https (ssl) down the road

This issue will be closed and archived in 3 days, as there has been no activity in the last 30 days. If this issue is still relevant or you would like to see action on it, please respond and we will get the ball rolling.

I am afraid this is not finished.

Just a random idea that popped into my head: we should probably have the config for setting absolute vs relative URLs default to loading from debug (i.e. if no value set, debug mode enabled means that relative URLs are generated).

We may also want to look at making relative links generated by default (in a sense reverting https://github.com/octobercms/october/pull/3536) because that was a breaking change for a bunch of sites that were combining | media with | url to generate the absolute links. It also causes issues when you're inserting images into richeditor fields on local dev sites and then when you go to production all of your content is referencing your dev URL, which is something that's always been a massive pain point of WP so it's something that we should be avoiding.

I totally agree with Luke, here, for what it's worth.

| media is easy to handle. The rich editor is going to be the challenge, because you'll need to provide the full URL for it to display in the rich editor correctly, whilst simultaneously being stored without the full URL so that you can populate with the correct URL based on the environment, or storage method used.

I had discussed one method of potentially handling this here: https://github.com/rainlab/pages-plugin/issues/405#issuecomment-527729577.

I have used that on a proprietary system before - it worked, but some might consider it "flimsy".

A potential way to expand on that idea would be to store the location of the media item relative to the media root in data-media. You would then need to parse the content before display and change the src or href attribute to use the full URL based on where it is storage URL and appending the relative media URL to the end.

I've recently had to deal with a WordPress site using a page builder plugin that was using full URLs for images and other resources and that was a pain to migrate to a previous host with a development domain to another domain and then to the final live domain.

I never want to have to deal with this again.

@mjauvin What's even more fun is handling assets from multiple locations (CDNs) in a Wordpress install. At least with an install that has all the images locally, there's a couple of plugins that will rewrite the URLs for you when you move address. :nauseated_face:

@bennothommo I used such a plugin to rename the URLs to the new site, but somehow it missed some of them.

It's one year old issue, what to do with that? PR or forget?

I'll write a PR for this.

I have already create a PR https://github.com/octobercms/october/pull/4423 from June, almost 1 year but it still open

Yes, but there should be a configuration setting for relative/absolute with default being relative. @LukeTowers explained this in a comment above, see: https://github.com/octobercms/october/issues/4082#issuecomment-538716087

Hello, I miss this discussion, so I add this #4968
I am absolutely for keep relative paths in all content. For me, main reason is developing websites localy, put content with customer on local (or some dev domain) and after customer's review, checking bugs etc. deploy it to another (public) domain.

@Rike-cz @mjauvin Why do you want relative URLs over fully qualified URLs? Can you provide a pros vs cons list?

I'm for relative paths as well. Local development (eg. *.test domain) or moving website to another domain to name just few examples where absolute paths are useless.

My current client can't use domain without www within their local network => images failed to display when I made changes while logged in via https://example.com/backend instead of https://www.example.com/backend. That's how I came across this issue today.

This feature should be marked as breaking change (major) since if affects currently deployed websites and should definitely be configurable.

Could you provide your thoughts on this @daftspunk? I'm fine implementing it at a later date, just curious for your thoughts on the matter.

Fixed by https://github.com/octobercms/october/commit/ccb2c372bb1fb3980c1240ffb2f7aeef5e786dae. Media library will no longer force absolute URLs unless cms.linkPolicy is set to force.

Nice! :)

On Thu, Apr 2, 2020 at 12:18 PM Luke Towers notifications@github.com
wrote:

Closed #4082 https://github.com/octobercms/october/issues/4082 via
ccb2c37
https://github.com/octobercms/october/commit/ccb2c372bb1fb3980c1240ffb2f7aeef5e786dae
.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/octobercms/october/issues/4082#event-3192967335, or
unsubscribe
https://github.com/notifications/unsubscribe-auth/AAPLTPRMHOGBM5XKVYSHG4LRKS3HDANCNFSM4GR44FYQ
.

--
Marc

Thank you!

Was this page helpful?
0 / 5 - 0 ratings