-3

To be clear, this has nothing to do with script tags. I am not using them. This is JS placed on the page using PHP include.

Most of my JavaScript files mix in PHP and use PHP's include instead of script tags. Usually the solution would be adding a query string to the end of src attribute for a script tag. As far as I know, I can't really do this when I use include. For the most part, I'm having issues on mobile for users to get the latest JS source instead of an old stored cached version on their device.

Chewie The Chorkie
  • 4,896
  • 9
  • 46
  • 90
  • 1
    A .php file is a .php file. A JavaScript file is a JavaScript file. One may contain syntax of the other, but the non-file extension code would just be treated like a string. – Scott Marcus Apr 17 '17 at 16:25
  • What I'm asking, is how do I force users to get the latest version of my source? – Chewie The Chorkie Apr 17 '17 at 16:30
  • 1
    setting an expires header is one possibility – Jeff Apr 17 '17 at 16:31
  • I think it's dumb that people so quickly resort to negative comments and down voting without explanation. It's a fair question. – Chewie The Chorkie Apr 17 '17 at 16:33
  • Nope its not. Have you googled *js always reload* – Jonas Wilms Apr 17 '17 at 16:35
  • Jonas - That would not work well for me as it would force users to download JS every time they visit a page. Mostly it only should happen when there is a new version. – Chewie The Chorkie Apr 17 '17 at 16:36
  • Jonas - I am familiar with that, but only works with using script tags, not php include. – Chewie The Chorkie Apr 17 '17 at 16:38
  • 2
    _"it would force users to download JS every time they visit a page"_ You can't know ahead of time when you're going to publish a change, so you can't tell how long to set your cache expiry for. That's why cache invalidation is one of the two hardest things in computer science. (The other one of course is naming things, and off-by-one errors.) – Alex Howansky Apr 17 '17 at 16:45
  • See that makes more sense to me and is way more constructive than making pointless remarks and downvoting. So maybe there's a good solution to renaming the page you're navigating to with a query string with each consecutive change? PHP could fetch that as a variable. I'm using vs code. – Chewie The Chorkie Apr 17 '17 at 16:49

2 Answers2

0

there is not much that you can do, usually javascript reloads automatically, however sometimes chrome keeps the cache and refuses to load the new code (in the PC you can try ctrl + F5).

What you can do is to show a little suggestion, like "If you have problems try to delete cookies and cache" in a dismissable tooltip/modal etc.

Disturb
  • 558
  • 8
  • 14
0

Ok so you if you use include() function to refers to a .php file. You might aswell inside that .php file add a variable of the address of your latest .js

//file to include
...
<script src="<php? echo $latest_JS_reference ?>" ></script>
...
siggi_pop
  • 568
  • 7
  • 10
  • 1
  • Thanks. I'm trying to remember if there's any reason I ever included JS without putting it within script tags. Some I do, and others I don't. Either way, it's either an echoed string like you have there or include. – Chewie The Chorkie Apr 17 '17 at 17:01
  • I'm getting a ton of JS errors using an embedded source like this. Tried it also with setting the header content-type to application/javascript. – Chewie The Chorkie Apr 17 '17 at 17:22
  • I just figured out why this doesn't work. It's because if I use script tags with an embedded source, it moves it into my head tags. (Probably browser dependent) Likely, I need to find a solution for having it be okay in the head tags. – Chewie The Chorkie Apr 17 '17 at 17:40