0
votes

How do I stop Angular2 router appending the same route twice when i refresh the page? It might be something other than angular router.

Go to the root of the app it redirects contact, which is good.
localhost:3000/contact

Refresh the page and it appends the same route
localhost:3000/contact/contact

the express server only contains 2 routes handlers. Return static files and for everything else return the index.html file.

app.get(/\.\w{2,4}$/, function (req, res) {
  res.sendFile(path.join(__dirname, req.url));
});

app.get('*', function (req, res) {
  res.sendFile(path.join(__dirname, 'index.html'));
});

index.html has base href set

<base href="/">

This is the angular route config

export const routes: Routes = [
  {path: '', redirectTo: 'contact', pathMatch: 'full'}
]
1
I doubt the code in your question causes the issue. Can you please provider more information (other routes, ...). Can you reproduce in Plunker? How does it behave with HashLocationStrategy?Günter Zöchbauer
Hey Günter, It can't be reproduced in Plunker as the issue happens when I refresh the page and the issue is local to my environment. If it helps the files came from this plunk plnkr.co/edit/HEoqGVNga8s5tO4niNfg?p=preview via the angular site. I was looking at how ngModules worked as it all changed since RC4. So I took the files from this plunk and added a very simple express server .screenm0nkey
The linked Plunker doesn't contain anything.Günter Zöchbauer
My bad. Please try now:)screenm0nkey
I found the issue, posted below. I wasn't looking hard enough :)screenm0nkey

1 Answers

1
votes

I found the issue in the index file. This was overwriting my base setting...

<script>document.write('<base href="' + document.location + '" />');</script>