question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ionic template SPA server not working properly

See original GitHub issue

I’m submitting a…

[x ] Bug report [ ] Feature request [ ] Documentation issue or request

Current behavior

If I create a test component (doesn’t matter what it does) and add a routing example like below:

const routes: Routes = [
  { path: 'login', component: LoginComponent, data: { title: extract('Login') } },
  { path: 'test/:testId', component: TestComponent, data: { title: extract('Test') } }
];

Going to http://localhost:4200/test/randomId doesn’t work. All of the script tags are getting 404 error because its trying to load them from “test” folder - http://prntscr.com/ifskud

However if I load the login component view first and the navigate to test component view, it works fine.

Environment

The issue only appears when working with Ionic generated templates.

{
  "generator-ngx-rocket": {
    "version": "3.2.1",
    "props": {
      "appName": "test5",
      "target": [
        "web",
        "cordova"
      ],
      "pwa": false,
      "mobile": [
        "ios",
        "android"
      ],
      "ui": "ionic",
      "auth": true,
      "projectName": "test-5",
      "packageManager": "npm"
    }
  }
}

Possible solution

I noticed that only Ionic generated templates have this in index.html:

<base href="./"/>

Changing it to this seems to fix the issue

<base href="/"/>

I was just wondering if this change might break something else as if there might be a specific reason Ionic templates use a different base href?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sinediedcommented, Feb 19, 2018

The only workable fix for this until (if) https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/77 is fixed, is to remove <base href="/"/> completely and switch Angular to legacy hashbang routing instead of HTML5 routing 😞 I’ll create a PR to implement this only for cordova-based target, even though I tried to avoid it in first place.

1reaction
sinediedcommented, Feb 19, 2018

Thanks for reporting the issue. The <base href="./"/> is not specifically for Ionic, but whenever Cordova target is enabled as it is needed for apps to works with Cordova on iOS 😕

It was a quick workaround but since it causes issues with routing, I have to find something else…

Read more comments on GitHub >

github_iconTop Results From Across the Web

<base href="/"> breaks application · Issue #77 - GitHub
I am using this plugin for a Ionic/Angular app, but when I add to my index.html it breaks ... Ionic template SPA server...
Read more >
SPA will not call my api running on localhost when executed ...
It has an endpoint that does a fetch to localhost:5001/api/weather. The SPA runs fine in browser and loads the data from my server...
Read more >
Ionic angular http post not working on device but works fine on ...
Apparently angular httpClient has some issues when executed on native devices and emulators, my solution was migrating the request to ionic ...
Read more >
10 Common Ionic Problems & Error Messages (And How to ...
This error is quite easy to find, it indicates you are calling a function from your template which you haven't implemented inside your...
Read more >
Developing a SPA (Single Page Application) with Ionic
Working with Ionic · Clone the sample repository to local · Edit "config.ts" under "src" directory · Install dependencies and start the server...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found