[Support Guide] Why do I see “Uncaught SyntaxError: Unexpected token <” errors? How can I use chunking or versioning for my assets at Netlify?

The error Uncaught SyntaxError: Unexpected token < is most commonly caused by your site’s code referring to an asset that is no longer available.

Most commonly, this is due to a filename change in assets generated during your build. The same situation happens if you use versioned or chunked javascript where the filename changes with every deploy.

Let’s look at an example:

A file that was named /assets/main-123abc.js would change to /assets/main-456def.js – where there is a variable hash included in the filename. Your build tools may be doing that for you automatically even if you don’t intend for them to be.

In many cases, this is not a needed pattern on Netlify, since it is used for cache-busting. You can read more about how we handle caching in this thread and also in this article about our CDN’s caching.

The primary pattern where this makes sense even on our CDN is with javascript code-splitting, or with versioning. If disabling those hashes are not an option, there are a few workarounds such as the ones mentioned in this spectrum thread that may help you create a successful implementation.

If you are still experiencing problems and none of these solutions work, please start a new topic where you describe your specific situation, and mention that you have read this post already, so you don’t get sent back here :slight_smile:

1 Like

Another reason you might see this error is if you leave the site load from an older deploy and then a new deploy is done and the old assets references are no longer available. You’ll receive that error because you are trying to load a JS file but it loads our html 404 page which begins with a < because the file is no longer there.

This same problem exists for non-bundled sites as well when requesting assets that aren’t there, but with a non-SPA site it generally doesn’t error out the entire application if an image fails to load or CSS fails to load, so the failure isn’t as critical.

Really the best way to deal with this right now is to use a service worker. The service worker can intercept the request, and instead of giving you a 404, it can pop up a message to tell your visitor the site has been changed and the they should reload to see the new version. If the page has inputs it can cache them or tell the visitor they will lose anything unsaved, etc.