serve icon indicating copy to clipboard operation
serve copied to clipboard

vue-router not working when running on serve

Open lightzane opened this issue 5 years ago • 9 comments

Since vue-router is working in development mode, I think I had mistakenly log issue on vue-router github. Because I lately found out that I'm running it on serve (after npm run build for vue) and found out that there is a separate github repo for that.

Issue: Vue-router not working on serve dist folder.

Please see issue logged on vue-router: https://github.com/vuejs/vue-router/issues/2668 or https://github.com/vuejs/vue-router/issues/2671

Update Additional note: vue-router is working on router-link, but accessing the router in URL directly, it always shows 404. (but this works in npm run serve = not yet on dist directory) This is to add note that I also tried using this:

npm install -g serve
# -s flag means serve it in Single-Page Application mode
# which deals with the routing problem below
serve -s dist

lightzane avatar Mar 24 '19 02:03 lightzane

Serve v6.4.8 work fine on my computer

gustiyoga avatar May 29 '19 02:05 gustiyoga

I'm experiencing a similar issue using vuepress and serve. It works fine, if I use the UI to navigate around, but trying to "deeplink" to a subpage by typing into the address bar, and it fails.

$ serve --version
11.1.0

chris-dura avatar Aug 15 '19 21:08 chris-dura

Can you exclude, that it is an issue with vue-router? (e.g. did you try to use a other http server for testing purposes?)

mara-schulke avatar Sep 21 '19 05:09 mara-schulke

I had a very similar problem with react-router and just solved it by adding this line to serve.json.

"rewrites": [ { "source": "/**", "destination": "/index.html" } ]

Probably this happens because serve has no idea about client-side routes, it just serves static files. So the trick is to ask it to serve index.html for all routes and let your client-side router handle the rest

emadshaaban92 avatar May 25 '20 21:05 emadshaaban92

With client side routing, no matter the framework, you need to either pass the -s param to serve to make the redirects for you or configure the redirects options like in the comment above. Ideally, you should also include a robots.txt file or be more specific about what urls are allowed based on the client side routing. This list must be generated from the list of routes and the format varies from platform to platform.

In short, this is not a problem with serve nor with vue-router

posva avatar May 28 '20 07:05 posva

@emadshaaban92 You just helped me figuring a similar issue out when deploying to Vercel! Thank you!

ccarstens avatar May 28 '21 11:05 ccarstens

I had a very similar problem with react-router and just solved it by adding this line to serve.json.

"rewrites": [ { "source": "/**", "destination": "/index.html" } ]

Probably this happens because serve has no idea about client-side routes, it just serves static files. So the trick is to ask it to serve index.html for all routes and let your client-side router handle the rest

this worked for me 😀

hassnian avatar Jun 30 '22 11:06 hassnian

I had a very similar problem with react-router and just solved it by adding this line to serve.json.

"rewrites": [ { "source": "/**", "destination": "/index.html" } ]

Probably this happens because serve has no idea about client-side routes, it just serves static files. So the trick is to ask it to serve index.html for all routes and let your client-side router handle the rest

Solved my problem

shashinvision avatar Jul 06 '22 22:07 shashinvision

My solution was to add -s (Rewrite all not-found requests to index.html) from my package scripts, without adding a serve.json file.

This example serves the dist folder on http://localhost:9000

serve -s dist -l 9000

mauroaccornero avatar Nov 03 '22 11:11 mauroaccornero