vuejs - How to remove hash from url using vue-router in Laravel without requesting to server again? - vue.js

I want to remove hash(#) from url in vuejs using vue-router in Laravel. So I used mode:'history' or history: true
const router = new VueRouter({
routes: routes,
mode: 'history'
//history: true
});
and it works perfectly but the problem is that each time request is changed for example from example.com/home to example.com/user the request will be sent to server and all the page will be refreshed however I want to only change the content between head and foot of the page. So when I mark an string in the top menu it will not unmarked when going to another url but now it sends the server and the page loads completely when not using mode:'hash'.
How can I remove hash without sending another request to server in order not to load the page again completely and load only body part?
Thanks

I don't have created links yet I just change it manually in url. If you are saying that doesn't work manually so why it works with mode: 'hash'? So if router-link works just like that I should use it I think. I didn't know about that
ok… I get it
if you are using history mode, you have to use <route-link> because, as noted above,
In HTML5 history mode, router-link will intercept the click event so that the browser doesn't try to reload the page.
When you enter a new url, the browser loads that page, that's the browser's way of operating and you can't get around that. The framework however handler it differently, by updating the url and the content, but not actually redirecting(reloading)
The reason why this works with the hashbang, is that the broser treats everything after the # character as in-page navigation. Meaning it doesn't consider it as a redirect. The hash character was traditionally used in HTML to allow navigate to items within a page.
For example, about-us.html#contact redirects a user to the about page and scrolls to the contact form.
The modern js frameworks use the hash to hack this navigation by not redirecting, and using the content after the hash to pass routes.
For example, if you have a route such as localhost:8080/#/about-us, the localhost:8080/# part is the same as localhost:8080/index.html# so changing anything after the # character keeps the browser on the same page, and the javascript (vue router) handles any changes that are needed.
Hope this clears it up. Fwiw, I haven't used history mode on any of my projects.

Related

Force SPA route navigation to return 200

I published my website (a SPA made with vue) to Github-Pages. This website uses "history mode", so the # does not appear when navigating to a different "page".
When direct URL navigation (user types website.com/downloads for example) or a refresh while not on the root page happens, the website tries to display 404.html.
When the 404.html loads, it redirects to the homepage, passing the route name taken from the URL:
<script>
const segment = 1;
//Gets the relative path and the hash of the URL.
sessionStorage.redirect = location.pathname.split('/').slice(0, 1 + segment).join('/');
sessionStorage.hash = location.hash;
//Forces the navigation back to the main page, since it's the only entry point that works.
location.replace('/' + location.pathname.slice(1).split('/').slice(segment).join('/'));
</script>
For the user, it is a bit noticeable, but it will display the correct page.
But while loading, it will report a 404 in the network tab, which could cause issues in integrations with other websites.
Is there anyway to fake a 200 response when loading these pages?
This is a typical issue with Single Page Applications using history mode (history.pushState) to simulate a full URL so that a page isn’t reloaded when the URL changes.
Since vue.js is an SPA framework, it means there is only one HTML and tag containing the “app” id. Due to this disadvantage, Google bots would not be able to read the content of a particular landing page and your website might not get the higher rankings. To make Google bots read the content, you can use two method, “Pre-rendering” and “Server-side rendering.”
Also you can try using routing in <li> and <a> tags and buttons instead of href=“/path”. Using a router link makes page navigation very fast and it benefits the SEO of your website as well.

Vue router history mode but when i use hash on url it reloads the page

I am using vue router and have already set mode to history. My app works fine except that when i try to use hash (#) in url in order to scroll the page to an element in the same page (eg: #information) the page reloads (meaning it re-requests data from my backend). I think the router thinks the page has changed and tries to load that page.
EDIT: The page does not really reload. It re-fetches data from by backend. I understand this as a reload though.
I tried googling but all i get is how to use history mode. Maybe i am searching the wrong way.
Any clues on how to fix this?
Here is my route:
{
path: '/p/:idormpn/:slug',
name: 'Productpage',
component: Productpage
}
Here is an example of what is happening:
https://www.e-checkout.gr/p/63529/dermatina-anatomika-pedila-mavro
Thanks!

Vue app automatically changing position of # within URL when clicking on a button link

I have a question with vue router. I'm working on a custom library of vue that most of the code has already been created before me. My question has to do with trying to get a page to use a back button on the page that works on a registration process flow (it works on all other pages, besides this one particular page).
I'm not quite sure whats exactly going on with why it's acting a certain way with the '#', which I assume is going to be the problem:
The flow of the app should go like this:
http://localhost:8080/#/events, hitting the 'Continue' button goes to the next page (product page)
http://localhost:8080/#/product, hitting the 'Continue' button goes to the next page (upgrade page)
http://localhost:8080/upgrade#/, hitting the 'Continue' button goes to the next page (payment page)
Each page has a back button on the page to go back to the previous one. Going from '/upgrade#/' and hitting the back button, the app gives me a link to a blank page with this URL:
http://localhost:8080/upgrade#/product
I'm not sure from when we are on the page of:
http://localhost:8080/#/product
and hit the Continue button, why the '#' moves to after '/upgrade'.
This is our method we are using to change pages:
changeRoute() {
this.$router.push("/product");
}
What you want is to put vue-router mode to history.
const router = new VueRouter({
mode: 'history',
routes: [...]
})
The default mode for vue-router is hash mode, which is why you are seeing this behaviour.

How to push a history entry and change address bar url without navigating to that url?

I try to mimic how Pinterest (and a lot of other popular sites) achieved this.
When a user clicks a modal, the modal pops up, the address bar url changes, a new history entry is added, but the page doesn't reload. And when the user closes the modal, everything reverts to previous state with a new history is also added.
Vue-Router offers router.push, router.replace and router.go, but they don't achieve what I want. router.push would navigate to that url, but I am opening a modal which has an URL associated with it, not going to that url. router.replace replaces the current history entry, not adding new one.
What is the standard way to do this?
Use Vue Router (google it) for this. Just keep in mind that stuff like this generally means a lot of work for very little gain.
With Vue Router you can set your app up so that page.com/book/Test directs you to page.com/book with the variable Test set.
I recommend setting your site up as a single file project using Webpack and the Vue plugin, and using that eith Vue router. Then you can get the functionality you want.

How can I have 'guardRoute' run before 'mapUnknownRoutes' in Durandaljs Router?

I'm using the Router in Durandaljs to control routing in a SPA. In my application all routes are created dynamically. Therefore I use only mapUnknownRoutes for mapping all routes. The problem I have is that if the user navigates between different hashes in the same page, clicking on 'back' leads to unloading the current page - something I wish to prevent. I thought of doing this by using guardRoute and there return 'false' when navigation remains in the same page, but 'guardRoute' runs only after 'mapUnknownRoutes' hence does not prevent the unloading of the current page.
Any suggestions?
Thanks !
Elior