V
V
Vlad2021-12-04 14:52:34
JavaScript
Vlad, 2021-12-04 14:52:34

How to write absolute paths in frontend assembly (Gulp 4)?

This build uses Gulp 4 and the gulp-rigger package to include templates.

The fact is that the project uses SPA technology and the links look like, for example, like https://mysite.com/Page/1 When you click on the links, the default action is canceled and a request like https://
is sent to the server using AJAX mysite.com/core.php?page=1 . In short, in this case, relative links no longer work, since the browser looks for files not in the root, but in the `Page/1` directory.

Is it possible in assembly some type variable like $_SERVER['SERVER_NAME']in php to add it to the path to css, js, img files and links? Or do you need to write js functions that would parse the necessary tags when loading the page and substitute location.origin in the path ?
If so, is there a ready solution for this? Maybe a class or separate functions?
Or is it possible to do this with some node.js plugin?

I'm looking for the best solution to run the application both locally on Open Server and on a remote server.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shamanov, 2021-12-05
@Vextor-ltd

relative/path/from/current/directory, / relative/path/from/root/site

https://mysite.com/Page/1:
<script src="/js/main.js">: https://mysite.com/js/main.js
<script src="js/main.js">: https://mysite.com/Page/1/js/main.js

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question