14

Sometimes when developing my current node project I will get a hanging refresh. Where the page will never load. I check the network tab in Chrome and see that its always hung up on a static files. The static file that gets stuck will differ, sometimes it will be a CSS file other times an image file.

I have tried to optimize all my files in hopes to resolve this issue but nothing has fixed it. If I hit refresh during a long load it will load the page correctly on the 2nd request. This does not happen every time I try to load the page but very often when switching between pages.

If I disable cache under the chrome network inspector, It will almost always happen.

**This is my 1st major node project so I could have made mistakes along the way. **

Entire project is hosted on github: http://github.com/polonel/trudesk

Example Load times: (Open image in new tab to see full-size)

Chris
  • 315
  • 1
  • 2
  • 16
  • Can you post the code you're using to server the static files? – slebetman Sep 29 '14 at 18:31
  • 3
    Did you try moving your static file middleware before *all* other middleware? That way the session middleware isn't hitting mongodb, etc. which could slow things down before it gets a chance to serve the file. – mscdex Sep 29 '14 at 18:56
  • 1
    Moving to the top of my middleware did indeed help with load times but I'm still looking at ~5-10sec load times. – Chris Sep 29 '14 at 19:18
  • I have the same issue although load times are not as long as yours. Also trying to figure it out. – mvbl fst Nov 06 '14 at 01:07

2 Answers2

27

I had exactly the same issue. I just moved to a place with a pretty bad internet connection. Loading time of static files in my node.js app increased to more then 40s per file.

I just moved the static middleware

app.use(express.static(__dirname + '/public'));

to the top of app.configure function, before all the other app.* calls.

It's now working significantly faster.

pzagor2
  • 709
  • 10
  • 24
  • 3
    THANK YOU SIR. Holy cow this problem haunted me for weeks. You're a saint. – Augie Gardner Mar 15 '16 at 07:48
  • This solved this very weird error that started happening out of nowhere! Any idea why this is and why could it just start happening out of the blue? – Trufa Apr 03 '19 at 21:26
  • I have compression before static folder for image so we don't get much of loss weight :sweat_smile: but a lot of time taken. – Et7f3XIV Aug 05 '20 at 15:01
0

I spent about 3 hours last night trying to solve this problem. I found that there was a while statement that was slowing the page down terribly:

while ((incomingUsername !== "") && (incomingPassword !== "")){
   newAccount(incomingUsername, incomingPassword);
}
function newAccount(name, password){
    console.log("ACCOUNT REGISTRATION INITIATED");
}

When I commented out the while statement, the page loaded in a matter of seconds.

Nicholas Smith
  • 674
  • 1
  • 13
  • 27
  • 1
    Looks to me like the while loop would never exit. The variables in the _condition_ never change (as far as we can see here.) That would slow down a page load. – Grant Lindsay Dec 26 '16 at 16:49