最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error if direct URL of react-router with params is passed to browser - Stack Overflow

programmeradmin1浏览0评论

FILES

When I access this URL http://localhost:8081/edit/01 with built-in navigation of react-router, it works fine. Route is <Route path="/edit/:id/" ponent={EditExpensePage} />.

But when I put the same URL directly in the browser, I get following two lines of error in console and page doesn't render anything.

Errors

GET http://localhost:8081/edit/bundle.js net::ERR_ABORTED
GET http://localhost:8081/edit/bundle.js 404 (Not Found)

I will be thankful for the help.

FILES https://github./microcyberz/expensify

When I access this URL http://localhost:8081/edit/01 with built-in navigation of react-router, it works fine. Route is <Route path="/edit/:id/" ponent={EditExpensePage} />.

But when I put the same URL directly in the browser, I get following two lines of error in console and page doesn't render anything.

Errors

GET http://localhost:8081/edit/bundle.js net::ERR_ABORTED
GET http://localhost:8081/edit/bundle.js 404 (Not Found)

I will be thankful for the help.

Share Improve this question asked Dec 19, 2017 at 19:58 Ahmad AliAhmad Ali 831 silver badge8 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

Here's your problem:

https://github./microcyberz/expensify/blob/master/public/index.html#L13

You're loading the bundle.js relative to the current path, in stead of absolute from the root. Change this to /bundle.js and you're good to go.

In your example, the js is loaded relative to the current url, resolving from /edit/123 to /edit/bundle.js

This is happening because your server is not redirecting to the index file. I mean what's behind http://localhost:8081? Is it node? Are you sure that every single request is handled the same way?

Also you may check how you include your bundle.js. Is the path relative like ./assets/js/bundle.js or absolute like /assets/js/bundle.js? Should be absolute.

This problem will also appear when using webpack and the HtmlWebpackPlugin. It can be solved by using the publicPath config option, which will prepend each script tag with a /:

//webpack.config.js
{
...
  plugins: [
    new HtmlWepbackPlugin({
      ...
      publicPath: '/'
    })
  ]
}


发布评论

评论列表(0)

  1. 暂无评论