Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught SyntaxError: Unexpected token < #1676

Closed
samassango opened this issue Mar 31, 2017 · 17 comments
Closed

Uncaught SyntaxError: Unexpected token < #1676

samassango opened this issue Mar 31, 2017 · 17 comments

Comments

@samassango
Copy link

React Boilerplate

Before opening a new issue, please take a moment to review our community guidelines to make the contribution process easy and effective for everyone involved.

Please direct redux-saga related questions to stack overflow:
http://stackoverflow.com/questions/tagged/redux-saga

For questions related to the boilerplate itself, you can also find answers on our gitter chat:
https://gitter.im/mxstbr/react-boilerplate

Before opening a new issue, you may find an answer in already closed issues:
https://github.com/react-boilerplate/react-boilerplate/issues?q=is%3Aissue+is%3Aclosed

Uncaught SyntaxError: Unexpected token <

Description

I don't know whats causing this issue, but it seems to be breaking a route i've been working on, on prod. My localhost is perfectly fine, i don't even know where to begin to debug this. Please help.

These are the following errors i'm getting from the console log.

  1. Uncaught SyntaxError: Unexpected token <
  2. Dynamic page loading failed Error: Loading chunk 4 failed.
    at HTMLScriptElement.c
  3. Uncaught (in promise) Error: Loading chunk 4 failed.
    at HTMLScriptElement.c

Looking at this error a bit closer, i saw that it fails when load chunck 4, so i don't know what the issue is, granted my localhost is fine. i'm using react-boilerplate

(Add images if possible)
screen shot 2017-03-30 at 11 48 15 am

Versions

Version : 3.0.0
node : >=7.1.0
npm : >=3

  • React-Boilerplate (see package.json):
  • Node/NPM:
  • Browser:
@veebuv
Copy link

veebuv commented Apr 3, 2017

Have you tried clearing your cache ?

@rodrigorahal
Copy link

rodrigorahal commented Apr 20, 2017

From the Error message, it seems like your server is returning a html file response to your 4.*.chunk.js request, most likely it is returning index.html which starts with a < token that causes the SyntaxError.

With react-router the routing is done on the client so the server is configured to serve the index.html file to every request, except for the static assets, then react-router can do its job.

For some reason the 4.*.chunk.js is not found in the static assets and index.html gets returned.

@nhducit
Copy link

nhducit commented May 16, 2017

I have this issue also when I deploy new version
Root cause: the index.html is cached => it points to old js files
Solution: do not allow browser cache index.html by configure your nginx/apache/express
add this header cache-control: no-cache; to index.html response

@julienben
Copy link
Member

@nhducit I tried your suggestion but the ServiceWorker caches index.html (and main.*.js) regardless of the server headers so there were still requests for chunks that had been replaced by a more recent deploy.

I've implemented this as a solution instead: https://zach.codes/handling-client-side-app-updates-with-service-workers/

@nhducit
Copy link

nhducit commented May 17, 2017 via email

@varghesethomase
Copy link

Pointing out an observation. I found that app.get('*') always serves index.html. This is the root cause of this bug. Like when a resource is not found or somehow the build is broken even, this issue can come over. I guess the exact requested file being served will help? And what about serving sw.js in case when an asset is not found there?

@yantakus
Copy link
Contributor

yantakus commented Aug 7, 2017

Please have a look at this related issue. Our development server serves sw.js with Cache-Control: public, max-age=0 header instead of Cache-Control: no-store. Any ideas on how to serve it with the right header?

@julienben
Copy link
Member

julienben commented Aug 8, 2017

Not sure but you may want to simply not serve the offline plugin in development. I have this at the bottom of app.js:

if (process.env.NODE_ENV === 'production') {
  const OfflinePluginRuntime = require('offline-plugin/runtime'); // eslint-disable-line global-require
  OfflinePluginRuntime.install({
    // Tells to new SW to take control immediately
    onUpdateReady: () => OfflinePluginRuntime.applyUpdate(),
    // Tells the UpdateLink component to reload into the new version
    onUpdated: () => {
      window.swUpdate = true;
    },
  });
}

@julienben
Copy link
Member

As for production, I use Firebase hosting so I only needed to add this to my firebase.json:

"headers": [
      {
        "source" : "/sw.js",
        "headers" : [
          {
            "key" : "Cache-Control",
            "value" : "no-store"
          }
        ]
      }
    ]

@varghesethomase
Copy link

varghesethomase commented Aug 8, 2017

@yantakus how do you run it in development servers? as npm start ? In that case offline-plugin never gets initiated i suppose. If you are using npm run start:production, then the offline plugin is at use. The cache headers can be set manually using nginx as it is given in the .nginx.conf file. I also believe max-age=0 is the default behaviour of Express server.

@yantakus
Copy link
Contributor

yantakus commented Aug 8, 2017

@julienben Never wrap runtime initialization in a condition as stated here, in this case your events won't get fired. It won't be installed in development anyway, because it is generated by webpack only in production. I experimented myself and I didn't get the events fired when I had plugin installation inside condition.
The only tiny issue you will get is a warning in console in development, but it is absolutely OK as stated here.

@varghesethomase by development server I meant npm run start:production, sorry for being unclear. I have to make it work locally to test this excellent idea. I want to tweak it a bit and testing it right in production is a bad idea.
If we serve all the files with express.js, what does nginx has to do with it?

@julienben
Copy link
Member

@yantakus Thanks for the tips! Strange given that the condition should always be true in production but I'll assume it's just webpack magic. Also, note that it's present in app.js of the boilerplate's demo app.

Btw, the custom Link component has worked very well for me. One caveat though: I was forced to change the authentication routes. As a consequence, users with cached versions of the app would be trying to login through an invalid route until they manually updated the SW. It happened before the app went public but might have been a serious problem if not.

@yantakus
Copy link
Contributor

Back to the original issue. Me and my team realized why this happens: when you build a new version, new chunks are generated and the old ones are deleted. But the index.html file is not refreshed and it still links to the old chunks which are not there anymore. Because all the non-asset URLs are rewritten to index.html, server responds with 404 page instead of the missing chunks. Unexpected token < is the first character in any html page. Have a look at my SO answer on how to resolve this problem.

@julienben
Copy link
Member

@yantakus This is pretty much the solution I used. Also I found out my teammates were still reflexively using Link so I wrote a tiny ESLint rule to protect from that.

@engmyahya
Copy link

I got Unexpected token < with in promise .... and fixed with me with installing the latest webpack, and webpack-dev-server , and also service worker should be there. This is my sample https://github.com/engmyahya/ReactJS.Web

@gretzky
Copy link
Member

gretzky commented Feb 16, 2018

Closing due to inactivity

@lock
Copy link

lock bot commented May 29, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators May 29, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants