2
votes

It is working when I click every links of my project and go to each pages but I don't know why I got 404 error when I reload page in polymer. Please help me what I'm missing to add.

Here is my app-location, app-route and iron-pages.

<app-location route="{{route}}"></app-location>
<app-route
    route="{{route}}"
    pattern="/:page"
    data="{{routeData}}"
    tail="{{subroute}}"></app-route>

<iron-pages selected="[[page]]" attr-for-selected="name">
  <good-article name="article" route="{{subroute}}"></good-article>
  <good-detail name="detail" route="{{subroute}}"></good-detail>
  <good-login name="login" route="{{subroute}}"></good-login>
  <good-form name="form" route="{{subroute}}"></good-form>
  <good-list name="list" route="{{subroute}}"></good-list>
  <good-404 name="404"></good-404>
</iron-pages>

here is script tag.

<script>
Polymer({
  is: 'good-app',

  properties: {
    page: {
      type: String,
      reflectToAttribute: true,
      observer: '_pageChanged',
    },
  },

  observers: [
    '_routePageChanged(routeData.page)',
  ],

  _routePageChanged: function(page) {
    this.page = page || 'article';
  },

  _pageChanged: function(page) {
    // Load page import on demand. Show 404 page if fails
    var resolvedPageUrl = this.resolveUrl('good-' + page + '.html');
    this.importHref(resolvedPageUrl, null, this._showPage404, true);
  },

  _showPage404: function() {
    this.page = '404';
  },
});
</script>
2

2 Answers

1
votes

This is likely caused by server not rerouting all urls to the index.html file. Since your app manages routes with javascript, server does not know where to redirect when it get urls other than index.html. Therefore you should configure your server (I assume .htaccess if it's an Apache server) to reroute all urls to index.html so the app can handle them.

1
votes

this is because there is no such file as the one requested in the URL, the file that handles these routes is the one containing the JavaScript logic for that, so, all URLs should be redirected to it first.

add this .htaccess file to your folder and replace index.html with your index name if its different.

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.html [L]

    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>