简体   繁体   中英

refreshing a detail page in Aurelia results in blank

I have created a new sample app with aurelia-cli. A weird behaviour that got me stuck is with routing. This is my main route.

{
   route: "projects",
   title: 'Project Section',
   name:'project-section',
   moduleId: './modules/projects/project-section',
   nav: true
}

and this is my project-section file

export class ProjectSection {
    configureRouter(config, router) {
    config.map([
        { route: '',    moduleId: './projects-list', nav: false, title: 'Projects List' },
        { route: ':id', moduleId: './project-detail', nav: false, title: 'Project Detail' }
    ]);
    this.router = router;
    }
}

now when I open a link like http://myapp.com/projects , it works fine, and if I refresh the page, it still works.

If I click a link to open the detail page http://myapp.com/projects/9348 that also works fine. But on refreshing this detail page, browser goes blank with

GET http://localhost:9000/projects/scripts/vendor-bundle.js net::ERR_ABORTED

error message in console

Am I missing something obvious? Is there a config setting to enable the refreshing of pages with /:id like routes?

The code is here at github aurelia-sample and you clone and run as usual

au run --watch

Thanks for any help

EDIT: does it have anything to do with

config.options.pushState = true;

in the app config?

I don't know if your problem has been resolved. But Yes, it is to do with the pushState being set to true . I myself faced this issue before. It is something to do with how Aurelia loads the JS for the page. I was unable to resolve it (albeit I'll admit I looked only for about 20 mins). But from what I saw you need to configure some setting to make this work.

Ok. This will help http://aurelia.io/docs/routing/configuration#options . Baiscally:

  • Add a <base href="http://localhost:9000"> to index.html to redirect loading content from the base url. The rest of the configuration can be as you have kept it.
  • Also add config.options.root = '/' in your router config

The point that you are missing is that, (IMHO kind of counterintuitively), the subroutes defined by ProjectsSection only come to life once you actually navigate to /projects .

In other words, the following sequence works:

  1. navigate to projects-list component (route of '' relative to the route of the section, that is, projects/ relative to the app)
  2. Refresh that same page
  3. Navigate to a details page.

The reason this sequence works has to do with how Aurelia resolves routes.

  • It first tries to match your route against the root configuration. It sees that the url starts with projects . So it checks to which component projects leads. It is ProjectSection in your case, therefore it uses that and checks whether it finds a configureRouter method on it. It does, because you've defined one, so it invokes it.
  • From then on, the route that it will try to match against that configuration is the the original route without the prefix which lead to that component. It was projects/ which lead to ProjectSection , followed by nothing, so the remainder of the route is / which is resolved as per the configuration you've created inside ProjectSection . In your case, that leads to projects-list .

The important part about this is that by performing this sequence, Aurelia gets a chance to invoke the configureRouter method on ProjectSection (since at (2), it navigates once again to projects/ relative to the app root). Therefore, a configuration which can be used against the url will exist.

In the problematic case, if you immediately navigate to /projects/:id , it will be matched against the root level configuration. There is no match, and since reloading counts as a first page load, there is no route to fall back to. That's why you are getting the error.

The important part about this scenario is that, contrary to the former case, the router skips invoking the configureRouter method on ProjectSection . If you set a breakpoint in that method, you will see that it does not get invoked. That's why I commented on is behavior as being counter-intuitive, because the behavior you (and me as well, when I first faced this problem) expect is a fairly common scenario.


I don't know of any official way to resolve this, so my suggestion is that you could try defining a wildcard route on the app level config like so:

{
  route: "projects/*",
  title: 'Project Section',
  name:'project-section',
  moduleId: './modules/projects/project-section',
  nav: true
}

and see if it works (I am not sure - I've tried to provide you with a reason, but I don't have the means to try to reproduce it at this very moment).

Try adding a redirect route in the ProjectSection configureRouter function like so (taking your example):

export class ProjectSection {
    configureRouter(config, router) {
        config.map([
            { route: '', redirect: 'list' }, // Redirects to the list route when no specific route in the project section is specified
            { route: 'list', moduleId: './projects-list', nav: false, title: 'Projects List' },
            { route: ':id', moduleId: './project-detail', nav: false, title: 'Project Detail' }
        ]);
        this.router = router;
    }
}

Thanks for all the answers posted. I finally resolved the issue. I am still not sure if this is the right way or not but here is the trick that works.

Taking hint from Rud156's and answer from this question How to remove # from URL in Aurelia I just added

<base href="/">

in the index.html file, and everything works as expected.

To further investigate the issue I cloned the aurelia sample contact app

1- when you run the app as it is after cloning, everything works great.

2- if you add

config.options.pushState = true;

in the src/app.js, child routes will stop working.

3- Add

<base href="/">

in the index.html and everything will start working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM