Svelte route gives me 404
This is for those who want to host the svelte apps, If your hosting provider supports
adding redirect or rewrite rules then you can do this to serve the index.html
for any request you make which the router can handle.
For example,
Set source as /*
the /
refers to the path and the wildcard *
refers to match arbitrary request paths.
and then set your destination to just /
that's it. don't set the destination as /index.html
or else all requests will open your home page for example if you try to open www.example.com/about
then it will open www.example.com/index.html
and set the action as Rewrite
instead of Redirect
and this also solves the problem of refreshing the site and opening the URL directly from browswer
Change
"start": "sirv public --no-clear"
To this
"start": "sirv public -s --no-clear"
in your package.json
file, it worked for me.
You must make sure your server is serving the index.html
for every route path and not just for /
.
If you e.g. are using sirv
with one of the Svelte starter projects you can add the --single
flag to the script.
"scripts": {
"start": "sirv public --single",
"start:dev": "sirv public --dev --single"
},
As mentioned in one of the comments above, if using roll-up, the following combination of scripts will work when calling npm run dev
"scripts": {
"build": "rollup -c",
"dev": "rollup -c -w",
"start": "sirv public --single"
}