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

Dynamic link generation for assets #158

@pavelskipenes

Description

@pavelskipenes

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or requestrefactor

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions