Code Monkey home page Code Monkey logo

Comments (4)

ItalyPaleAle avatar ItalyPaleAle commented on August 15, 2024

I've tried your sample and I've then try repro-ing this on my local machine. To me, this looks more like an oddity in now.sh, the platform you're using for hosting?

When visiting https://svelte-spa-router-example.laughingbubba.now.sh/should-be-router404 as in your example, the expected behavior would be for the server to return a 404 error. Unless there's a file called /should-be-router404/index.html, in fact, the server shouldn't return your app, and it should instead show a 404 error (again, the server, not the router). This is also the behavior when running npm run start-example (which uses the serve module), when deploying to S3 or Azure Blob, etc.

I'm not very familiar with now.sh, but could this be an issue with misconfigured routes in the platform? Their docs show something like: https://zeit.co/docs/v2/advanced/routes/

Anyways, with the specific regards to your request ("Would be nice when you hit the root path it automatically redirected to the hashbang root"): this should be relatively easy to do in your app's code. Before you start your Svelte app, add something like this:

if (!window.location.hash || window.location.hash == '#') {
    history.replaceState(undefined, undefined, '#/')
}

In the sample code for the router, this code would likely go into main.js like this:

import App from './App.svelte'

if (!window.location.hash || window.location.hash == '#') {
    history.replaceState(undefined, undefined, '#/')
}

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

export default app

PS: You're welcome :)

from svelte-spa-router.

LaughingBubba avatar LaughingBubba commented on August 15, 2024

Hi there, I deployed to now.sh to see how it would play out. However, the now.sh behavior is the same as if you used sirv-cli via npm run dev.

I guess my point is that the catch-all '*' should catch /something and route to </NotFound> but it doesn't, it routes to <Home/> even though it is not an exact match to /.

I guess one persons bug is another persons feature. The one and only test you have for the catch-all is http://localhost:5000/#/does/not/exist . What would you expect for http://localhost:5000/does/not/exist ?

Anyways, thanks for the prompt response and the tip.

Cheers.

from svelte-spa-router.

ItalyPaleAle avatar ItalyPaleAle commented on August 15, 2024

I don't recommend using sirv: at the beginning that's what I was using myself for this module devDependency, but it had multiple bugs. I switched to serve.

What I'm trying to say is that on a normally-configured server, http://localhost:5000/does/not/exist, the server should return a 404 error and should NOT respond with your app. The router shouldn't even be involved in there, as you should see a 404 page like this:

image

(Depending on your web server, it would be something similar)

That's why I think it might be an "issue" with now.sh and sirv. Does it make sense?

However, even in your case you could try to do something at the app level. Your window.location.hash would be empty, but you might have the full URL (like http://localhost:5000/does/not/exist`) inside window.location.href. You could extract the /does/not/exist part and redirect to http://localhost:5000/#/does/not/exist. The benefit here is that if your users visit /exists, they'd be redirected to #/exists too :) Again, this is not a usual behavior and it's probably something "weird" that now.sh and sirv are doing to support SPAs that don't use hash-based routing. This router was designed instead for apps deployed to block storage (AWS S3, Azure Blob Storage, etc), or even IPFS, where this behavior certainly doesn't exist. But if you plan on now.sh being your web server, then you could use this feature at your advantage.

from svelte-spa-router.

LaughingBubba avatar LaughingBubba commented on August 15, 2024

Ahhhh ... that answers the question very well thanks. Good to know about sirv.

from svelte-spa-router.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.