Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Dynamic link generation for assets #158

Open
pavelskipenes opened this issue Jun 8, 2022 · 0 comments · May be fixed by #169
Open

Dynamic link generation for assets #158

pavelskipenes opened this issue Jun 8, 2022 · 0 comments · May be fixed by #169
Labels
bug Something isn't working enhancement New feature or request refactor

Comments

@pavelskipenes
Copy link
Collaborator

pavelskipenes commented Jun 8, 2022

Currently the project is hosted at a university server within a virtual host like so:

https://org.ntnu.no/project1
https://org.ntnu.no/project2
https://org.ntnu.no/project3
...

That means relative paths in assets like images and stylesheets would not work since they would be passed outside virtual host and managed by NTNU's own rules often resulting in 404. Hard coding (absolute path) is also not feasible since local development builds would end up fetching assets from live server. This is important since you don't want to be able to perform destructive actions on live server when developing. The running script needs to figure out if it's being run under a virtual host and prepend virtual host path to all assets if so.

Current solution is a hard coded baseurl inside settings/settings.json (generated from docker/docker.json). That is not optimal since if there is a mismatch between baseurl in settings.json and an alias. Think 127.0.0.1, localhost, 192.168.0.xxx or even custom hostname like mylaptop.local. Also when accessing the web site from another client on local network (like a phone or a tablet) then they would obviously get a 404 if any assets are located on "localhost" from the perspective of the client.

var_dump()-ing the super global $_SERVER in both production and in development contains a key called SCRIPT_NAME. It contains values /svommer/index.php in production and /index.php in development. Transforming that string with following function would extract (if any) virtual host present (even nested virtual hosts)

function getVirtualHostPath(): string
{
    $temp = str_replace(basename(__FILE__), "", $_SERVER["SCRIPT_NAME"]);
    return substr($temp, 1);
}

The output of that function can be safely prepended to any relative path as it returns an empty string if no virtual host is present.

$_SERVER["SCRIPT_NAME"] value is not going anywhere soon as it's a part of CGI 1.1 specification. Related SO answer.

@pavelskipenes pavelskipenes added bug Something isn't working enhancement New feature or request refactor labels Jun 8, 2022
@pavelskipenes pavelskipenes self-assigned this Jun 8, 2022
@pavelskipenes pavelskipenes linked a pull request Jan 26, 2023 that will close this issue
11 tasks
@pavelskipenes pavelskipenes removed their assignment Jan 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working enhancement New feature or request refactor
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant