1

Should I use absolute paths for CSS or JS resources in <link and <script tags?

Is there any reason?

Some SEO optimization say fix it, should I do it for any reason?

I do not know why, if everything is working fine?

<link rel="stylesheet" href="/css/main_rtl.css?v=5e6e47e9"> <script src="/lib/js/frameworks.min.js?v=5e6e47e9"></script> 

Vs

<link rel="stylesheet" href="https://sample.com/css/main_rtl.css?v=5e6e47e9"> <script src="https://example.com/lib/js/frameworks.min.js?v=5e6e47e9"></script> 

Is there any SEO-related point?

Shorter version is smaller HTML at least.

  • each page has canonical
  • domain only serve https version(http redirect to https)
3
  • 1
    There is no difference Commented Oct 7, 2019 at 19:06
  • 3
    "Some SEO optimization say fix it" Which resource say what exactly? It would be better to explicitly cite the sources/references on which your question is built. Also about "Shorter version is smaller HTML at least." this shouldn't make a lot of differences today. Also the relative version is not without merits: if you change the hostname one day you have less changes to do. Commented Oct 7, 2019 at 19:24
  • @PatrickMevzek my friend as person suggest it Commented Oct 13, 2019 at 12:09

1 Answer 1

2

If your website is available on both http and https I definitely recommend using relative path and not absolute. Otherwise I cant really see any specific difference.

The SEO optimization services might recommend you to use absolute paths because it could in theory create some issues for them if you use relative path, but I would ignore that myself because I don't think it matter much for Google and other large indexers.

6
  • 3
    Personally I'd advice to use //example.com/assets/file.js to avoid issues between dev/staging/prod env. Also with many routes and eg emails the relatives paths are becoming a problem - it's easier to point an absolute path. Another point is that most frameworks provide you with param that's relative, but print out the actual absolute path. Commented Oct 7, 2019 at 21:35
  • 1
    @versedi Sounds like an answer! You make some excellent points. Cheers! Commented Oct 7, 2019 at 21:51
  • website serve only on https, http redirect to https for all requests Commented Oct 8, 2019 at 5:38
  • 1
    @versedi I disagree. There are two problem with using fully qualified paths. (1) You cant test it on a local server. With relative paths, I can test the whole thing on my development machine. (2) You cant shift the code to another domain, without making changes. I am working on 3 websites simultaneously and its really really helpful to be able to copy and paste the references. Finally, I could be wrong, but I suspect that having relative paths is faster to evaluate. Commented Jan 14, 2022 at 21:57
  • 1
    @RohitGupta the comment you were replying to was done in different time of how mature the front-end tool kit was. Nowadays go for relative all the time. Commented Jun 10, 2024 at 23:34

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.