jorgegorka / svelte-router

Svelte Router adds routing to your Svelte apps. It's designed for Single Page Applications (SPA). Includes localisation, guards and nested layouts.
MIT License
524 stars 42 forks source link

Basic webpack implementation gives failed to construct URL error #1

Closed C02Equinox closed 5 years ago

C02Equinox commented 5 years ago

I implemented a basic app with 2 routes via the webpack starter template, started up the webpack dev server and get that error message below:

url_parser.js:2 Uncaught TypeError: Failed to construct 'URL': Invalid URL
    at UrlParser (url_parser.js:2)
    at SpaRouter (router.js:91)
    at Module../src/main.js (main.js:6)
    at __webpack_require__ (bootstrap:19)
    at Object.1 (routes.js:12)
    at __webpack_require__ (bootstrap:19)
    at bootstrap:83
    at bootstrap:83

Any ideas?

In debug mode, the string passed into new URL(urlString) is "/"

NorseGaud commented 5 years ago

Same here. routes.js

import PublicIndex from './views/public/index.svelte'
import PublicLayout from './views/public/layout.svelte'
const routes = [
  {
    name: '/',
    component: PublicIndex,
    layout: PublicLayout
  }
]
export { routes }

App.svelte

<script>
  import { Router } from 'svelte-router-spa'
</script>

<Router />

main.js

import App from './App.svelte'
import { SpaRouter } from 'svelte-router-spa'
import { routes } from './routes'
import NotFound from './views/not_found.svelte'

SpaRouter({
  routes,
  pathName: document.location.pathname,
  notFound: NotFound
}).getActiveRoute

const app = new App({
  target: document.body
})

export default app
NorseGaud commented 5 years ago

Hmm, I'm curious about UrlParser... It seems to be used in a way that's not supported as console.log(UrlParser('http://localhost:5000/')); provides what we need.

I've been playing around and it's almost as if the code needs to take document.location.href and append pathName to the end of it. I'm not a JS guy so I'll likely eventually figure it out unless someone else knows how to accomplish this.

jorgegorka commented 5 years ago

Hi @NorseGaud & @C02Equinox:

pathName requires a full url, including the protocol and domain part.

I've updated the docs because they were wrong. Apologies if they've confused you.

If you left this param undefined it defaults to document.location.href

@NorseGaud Your pull request won't solve this issue because URLParse needs the full url as the first param and a template to parse named params as the second argument. Thanks anyway for your PR.

NorseGaud commented 5 years ago

Thanks @jorgegorka. Changing main.js to have pathName: document.location.href instead of document.location.pathName is confirmed to be working.