bertramdev / asset-pipeline

The core implementation of the asset pipeline for the jvm
193 stars 91 forks source link

Generated relative URLs are not relative enough #256

Open mbezjak opened 4 years ago

mbezjak commented 4 years ago

When asset-pipeline generates asset relative URLs it assumes it is relative to the server and not to the currently opened URL.

Assumptions

Observations

Problem

Using grails 3.x, suppose you are accessing your applications through a gateway (e.g. traefik) and are configuring the gateway to use context paths to distinguish applications (e.g. http://example.com/app1/, http://example.com/app2/; in traefik PathPrefixStrip: /app1) and not DNS approach (e.g. http://app1.example.com, http://app2.example.com). Then:

But from the outside, there is no such asset http://example.com/app.js. It should actually be http://example.com/app1/app.js. So the problem is that currently asset-pipeline makes assumptions as to how the application will be deployed and made accessible to the outside world.

Current workarounds

Solution?

I was wondering if this would be a good direction to take asset-pipeline... If instead of generating /app.js URL, it generated app.js, then it would be relative to the currently opened URL. Basically <script src="app.js"> in the browser that is currently visiting http://example.com/app1/ translates to http://example.com/app1/app.js.

Code

I was looking at the code. I think it's these are the relevant lines, right? https://github.com/bertramdev/asset-pipeline/blob/rel-3.0.10/asset-pipeline-grails/src/main/groovy/asset/pipeline/grails/AssetProcessorService.groovy#L133-L136.