iron-meteor / iron-router

A client and server side router designed specifically for Meteor.
MIT License
1.98k stars 413 forks source link

appcache leads to infinite loops for missing routes / notFoundTemplate not rendered #1202

Open djules75 opened 9 years ago

djules75 commented 9 years ago

Application works fine without appcache. After adding appcache (meteor add appcache), Iron Router sends the app in a redirect loop for missing routes instead of showing the notFoundTemplate (it works fine for existing routes).

For example, the navigation bar ends up in a loop with "http://127.0.0.1/idontexist" alternating with "http://127.0.0.1/#!idontexist".

I managed to reproduce the issue. Create a new app (meteor v1.0.3.1):

meteor create infinitelooop
meteor add iron:router
meteor add appcache

app.js:

Router.configure({
  // Defines template for missing routes and missing lists
  layoutTemplate: 'bare',
  notFoundTemplate: 'routeNotFound'

});

Router.route('/ok', {
  name: 'routeok'
});

app.html

<template name="bare">
    <div>
      {{>yield}}
    </div>
</template>

<template name="routeNotFound">
    <div>
        <h2>404</h2>
        <p>Sorry, there\'s no page at this address.</p>
    </div>
</template>

<template name="routeok">
    <div>
        <h2>This route works!</h2>
        <p>Welcome here...</p>
    </div>
</template>

Result: 127.0.0.1:3000/ok : works with or without appcache all other routes show the appNotFound template without appcache, or end up in a redirect loop if appcache is installed

miri-am commented 9 years ago

Is there a way / fix to handle this?

miri-am commented 9 years ago

this works as a workaround: http://stackoverflow.com/a/27790687/3223028

isAlmogK commented 9 years ago

I'm running into the same issue, I'm not sure I understood the workaround that's for redirect on user login in not for incorrect / missing route

workflow commented 9 years ago

@miri-am thx for posting this workaround!

@almogdesign It works like this:

  1. Remove any notFoundTemplate setting from Router.configure
  2. Add the following instead (adjust to your needs and template names)
Router.route('/(.*)', function() {//regex for every route, must be last
    if (this.ready()) {
        document.title = "404";
        this.render('error');
    } else this.render('loading');
})
isAlmogK commented 9 years ago

Thanks this worked