React Router - Uncaught SyntaxError: Unexpected to

2019-08-23 10:44发布

问题:

I am using react 16 and react router 4, webpack and webpack dev server.

I do have the following Route:

<Route path="/company/:symbol" render={this.getCompanyPageRoute} />

And I do navigate to this route through the following:

this.props.history.push('/company/blablabla');

Everything works fine. But if I refresh the page with this URL, I get the following error:

Uncaught SyntaxError: Unexpected token <

I've seen that there are several posts of this error, but I was not albe to fix the issue following those links.

回答1:

Posting the answer if it could help someone else.

Changing the output property of the webpack config to the following fixed the issue:

output: {
        path: BUILD_DIR,
        publicPath: '/',
        filename: 'bundle.js',
    }

The trick is made by: publicPath: '/'