Comments (4)
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.
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.
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:
(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.
Ahhhh ... that answers the question very well thanks. Good to know about sirv.
from svelte-spa-router.
Related Issues (20)
- project active? HOT 1
- vite support HOT 1
- a11y warnings HOT 1
- Active links - allow duplicate classes
- In vite, i can't use the proxy /dev HOT 1
- Redirection from route pre-condition HOT 5
- Nested routes as maps HOT 1
- If I want to go to the login page in the sidebar + SPA form, is it possible with route guard? HOT 4
- Is this not possible in routeLoaded? HOT 5
- What is the black bar in this image? HOT 2
- Feature request: Svelte 4 support HOT 13
- FEATURE REQUEST: pass next() in precondition to decide whether the router should *change* the current route HOT 1
- Load user data HOT 4
- How to check hash-bashed history HOT 1
- Is there an alternative to the "Failed to fetch dynamically import problem" issue in Vite + Svelte spa environment? HOT 1
- WARNING: The following packages have a svelte field in their package.json but no exports condition for svelte. [email protected] HOT 8
- Typescript problematic types after "wrapping" the route HOT 2
- navigating within same component does not work HOT 5
- Typescript support: Package path ./wrap is not exported from package svelte-spa-router HOT 2
- Will the library support Svelte 5? HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from svelte-spa-router.