erikringsmuth / app-router

Router for Web Components
https://erikringsmuth.github.io/app-router/
MIT License
611 stars 83 forks source link

Default route (without hash) not working #20

Closed jokesterfr closed 9 years ago

jokesterfr commented 9 years ago

Hi, thank you for publishing this module, it sounds very promising. I'm trying to use it, and I'm facing a little issue. Here is my index.html file using app-router:

<!doctype html>
<html>
    <head>
        <title>Testing<title>
        <script src="./bower_components/platform/platform.js"></script>
        <link rel="import" href="./bower_components/app-router/app-router.html">
        <link rel="import" href="./bower_components/polymer/polymer.html">
    </head>
    <body unresolved>
        <app-router trailingSlash="ignore">
            <app-route path="/" import="./pages/home-page.html"></app-route>
            <app-route path="/home" import="./pages/home-page.html"></app-route>
            <app-route path="/test" import="./pages/test-page.html"></app-route>
            <app-route path="*" import="./pages/not-found-page.html"></app-route>
        </app-router>
    </body>
</html>

My webapp is hosted under http://localhost:8080/test/index.html, and my concern is about getting the home page in those cases:

  1. http://localhost:8080/test/index.html#/home
  2. http://localhost:8080/test/index.html#/
  3. http://localhost:8080/test/index.html#
  4. http://localhost:8080/test/index.html
  5. http://localhost:8080/test/#/home
  6. http://localhost:8080/test/#/
  7. http://localhost:8080/test/#
  8. http://localhost:8080/test/

Cases 1-2-5-6 give me the home-page (as expected) Cases 3-4-7-8 give me the not-found-page (not expected)

Do you have any clue why isn't app-router targeting the home page when no hash is given?

FYI my server.js is:

var express = require('express')
  , bodyParser = require('body-parser')
  , errorHandler = require('errorhandler')
  , methodOverride = require('method-override');

var port = parseInt(process.env.PORT, 10) || 8080;
var rootdir = __dirname + '/www';

var app = express();
app.get('/', function (req, res) {
    res.redirect('/test/index.html');
});

app.use(methodOverride());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));

app.use(express.static(rootdir));
app.use(errorHandler({
    dumpExceptions: true,
    showStack: true
}));

console.log('Portal server listening on port', port);
app.listen(port);

Thank you, Jk.

erikringsmuth commented 9 years ago

The router considers a path a hash path if the hash starts with #/. All other cases it thinks it's a regular path. You can force the router to use the hash for paths at all times with this:

<app-router pathType="hash" trailingSlash="ignore">

I think this should do it. Let me know!

jokesterfr commented 9 years ago

Thank you @erikringsmuth for the quick answer! This works better, as cases 3 and 7 now work, but 4 and 8 are still wrong. I feel I can do a manual hash change while loading index.html page, isn't it supported by app-router itself?

Example of what could change it:

window.location.hash = '/';
erikringsmuth commented 9 years ago

I've avoided adding redirects so far but that's something I should tackle at some point. I'll add a few of these as test cases and that should help me work out what's going on with 4 and 8.