The website that I'm editing right now, every link at switching pages has hash symbol. And the page doesn't refresh everytime I switch and sticks on the last position of the site.
Where can I find the code and remove the hash from the url.
Does removing it will make the pages refresh everytime?
Vue is a Single Page Application framework. That means you don't have full page loads, instead page content is swapped out dynamically on the client.
Assuming you're using vue router, you can switch to historyMode which won't use # for urls and instead real urls.
From the docs:
The default mode for vue-router is hash mode - it uses the URL hash to simulate a full URL so that the page won't be reloaded when the URL changes.
To get rid of the hash, we can use the router's history mode, which leverages the history.pushState API to achieve URL navigation without a page reload:
const router = new VueRouter({
mode: 'history',
routes: [...]
})
This will get you "normal" urls as seen anywhere else on the web.
Related
I was in the process of creating a PWA using NUXT. I'm using Veutify as the design component.
My requirement is:
I need to open a external URL, inside the app itself (not in a new browser page/tab)
Secondly, I need to detect closing or url change on the page loaded, and trigger a router push automatically
Any help would be appreciated. Thanks.
For your first question, if you want to access an external url in Vuetify, use the 'href' attribute instead of the 'to' attribute. e.g
<v-btn
large
nuxt
depressed
rounded
href="https://www.google.com/"
>Access Google</v-btn
>
And for your 2nd question, you can use a watcher e.g
watch:{
$route (to, from){
//Perform action here
}
}
I want to use an anchor tag in my react spa (using react-router). But if I give it, for example, an href value of google.com, it will just extend my spa route, to say myreactspa.com/sampleRoute/google.com. Is there any way to make my anchor tag directly link to google.com? Or must I append, for example, https:// to my href value? Thanks!
Use Route component rendering null and change the window's location.
<Route path='/externalresource' component={() => {
window.location.href = 'http://google.com'
return null
}}/>
You should append https:// to your URL and set the target attribute of the a tag to _blank. It is not a problem of React Router to handle external links.
So something like that:
Link
There is no need to try use React Router to handle external links
react-router (and similar) is specially designed for SPA. When you are trying to navigate to another website (external link), the router is not supposed to handle that. If you are using Link, then whatever path you pass, react-router assumes that to be a route of your SPA (this is expected behaviour, NOT a bug).
If you want to navigate to another website from your web-app, use plain anchor tag like this
Click here
If you want to open the page in a new tab use
Click here
As titled,
I've a webpage which has multiple pages on it. The page was originally a single page app created using jQuery before I moved some of the contents into separate files, and load it dynamically using a jQuery templating plugin. The page starts at default pathname ('/') and whenever the content change, the pathname on the URL will change too.
function goTo(page){
// Changing URL without reloading the page (to allow changing the URL
//state without changing the page)
if (page !== 'main'){
console.log("Yes it is not main!");
window.history.pushState("", "", "/"+page);
} else {
console.log("Yes it is main!");
window.history.pushState("", "", "/");
}
// Set page template
$('#main').loadTemplate('pages/'+page+'.html');
}
The above is the method i used to change the URL and content whenever user clicks on the link.
The problem that I encounter is that, whenever I refresh the page when there is a pathname on the URL, the page will return 404 error (not found) and it will return an unknown blank html page that is totally empty (no css and script loaded).
For example, the main page URL is http://localhost:8000, and if I clicked to page A, the URL becomes http://localhost:8000/pageA. However in pageA, when I refresh the page, it will return blank. Only if I reset the URL to http://localhost:8000 then the page will refresh normally.
Is there a way to fix this blank page and pathname problem? Like changed the pathname before the DOM loads?
Thanks
Using window.history.pushState you will replace the browser location, and by hit page refresh you are sending request to the server, and you are asking to serve the page which name is in the location bar. However the page does not exist on the server so the server answers with Not found and that's correct behavior.
You have two options:
Implement the page also on the server side
Use client routing using hash (there are a lot of libraries for that, for example http://projects.jga.me/routie/)
I have an AngularJS application that is injected into 3rd party sites. It injects dynamic content into a div on the 3rd party page. Google is successfully indexing this dynamic content but does not appear to be crawling links within the dynamic content. The links would look something like this in the dynamic content:
Link Here
I'm using query parameters for the links rather than an actual url structure like:
http://www.example.com/support/title/Example Title/titleId/12345
I have to use the query parameters as I don't want the 3rd party site to have to change their web server configuration to redirect unfound URLs.
When the link is clicked I use the $locationService to update the url in the browser and then my angular application responds accordingly. Mainly it shows just the relevant content based on the query params, sets the page title and meta description.
Many of the articles I have read use the route provider in angularJS and templates but I'm not sure why this would make a difference to the crawler?
I have read that google should view urls with query parameters as separate pages so I don't believe that should be the issue:
https://webmasters.googleblog.com/2008/09/dynamic-urls-vs-static-urls.html
The only things I have not tried are 1. providing a sitemap with the urls that have the query parameters and 2. adding static links from other pages to the dynamic links to help google discover those pages.
Any help, ideas or insights would be greatly appreciated.
This happens because google crawlers are not able to get the static html from your url since your pages are dynamically rendered with Javascript, you can achieve what you want using the following :
Since #! is deprecated, You can tell google that your pages are rendered with javascript by using the following tag in your header
<meta name="fragment" content="!">
On finding the above tag google bots will request your urls with the _escaped_fragment_ query parameter from your server like
http://www.example.com/?_escaped_fragment_=/support?title=Example Title&titleId=12345
Then you need to rebuild your original url from the _escaped_fragment_ on your server and it will look like this again
http://www.example.com/support?title=Example Title&titleId=12345
Then you will need to serve the static HTML to the crawler for that url.
You can do that using a headless browser to access the url. Phantom.js is a good option to render your page using the javascript and then give the contents into a file to create a HTML snapshot of your page. You can save the snapshot as well on your server for further crawling, so when google bots visit can you can directly serve the snapshot instead of re-rendering the page again.
The web crawler might be running at a higher priority than the AngularJS interpretation of your dynamic links as the web crawler loads the page. Using ng-href makes the dynamic link interpretation happen at a higher priority. Hope it works!
If you use urls with #
Nothing after the hash in the url gets sent to your server. Since Javascript frameworks originally used the hash as a routing mechanism, that's a main reason why Google created this protocol.
Change your urls to #! instead of just using #.
angular.module('myApp').config([
'$locationProvider',
function($locationProvider) {
$locationProvider.hashPrefix('!');
}
]);
This is how Google and bing handle the ajax calls.
The documentation is mentioned here.
The overview as mentioned in the docs is as follows
The crawler finds a pretty AJAX URL (that is, a URL containing a #! hash fragment). It then requests the content for this URL from your server in a slightly modified form. Your web server returns the content in the form of an HTML snapshot, which is then processed by the crawler. The search results will show the original URL.
Step by Step guide is shown in the docs.
Since the Angular JS is designed for the Client Side so you will need to configure your Web server to summon a headless html browser to access your web page and deliver a hashbang url which will be given to the special google URL.
If you use hashbang URL then you would need to instruct the angular application to use them instead of regular hash values
App.config(['$routeProvider', '$locationProvider', function($routes, $location) {
$location.hashPrefix('!');
$routes.when('/home',{
controller : 'IndexCtrl',
templateUrl : './pages/index.html'
});
as mentioned in the code example here
However if you do not wish to use hashtag url but still inform the google of the html content but still want to inform the google then you can use this meta tag as this
<meta name="fragment" content="!" />
and then configure the angular to use the htmlURL's
angular.module('HTML5ModeURLs', []).config(['$routeProvider', function($route) {
$route.html5Mode(true);
}]);
and then whichever method to be installed via module
var App = angular.module('App', ['HashBangURLs']);
//or
var App = angular.module('App', ['HTML5ModeURLs']);
Now you will need a headless browser to access the url
You can use phantom.js to download the contents of the page ,run the javascript and then give the contents into a temporary file.
Phantomrunner.js which takes any url as input,downloads and parses the html into DOM and then checks the data status.
Test each page by using the function defined here
SiteMap can also be made as well as shown in this example
The best feature is you can use search console of verify your site url using
Google search console
Full attribution goes to the website and the author mentioned in this site
.
UPDATE 1
Your crawler needs the pages as -
- com/
- com/category/
- com/category/page/
By default, however, Angular sets your pages up as such:
- com
- com/#/category
- com/#/page
Approach 1
Hash bang allows Angular to know which HTML elements to inject with JS which can be done as mentioned before but since it has been depericated hence the another solution would be the following
Configure the $locationProvider and set up the base for relative links
You can use the $locationProvider as mentioned in these docs and set the html5mode to true
$locationProvider.html5Mode(true);
This lets Angular change the routing and URLs of our pages without refreshing the page
Set the base and head of your document as <base href="/">
The $location service will automatically fallback to the hashbang method for browsers that do not support the HTML5 History API.
Full attribution goes to the page and the author
Also to mention there are also some other measures and tests that you can take care of as mentioned in this document
I had made a SPA in angularjs, when I run it on browser it showed me '#' in the address bar. So I enabled html5Mode. This successfully removed '#' from the url but when I try to reload the page, it gives 404: page not found error. I tried writing in .htaccess from this url:
htaccess redirect for Angular routes
Nothing change.
I also followed grunt answers but that was also not helpful.
Did you correctly enable html5 mode?
Take a look at the answer to a question regarding html5 mode/hashbang mode.
Did you add <base href="/"> to the head of your html?
You need to setup your server routes to return index file for all angular urls.
Usually you register your api at some special endpoint (as mysite.com/api/v1) and if url is not matching this pattern return index.html file with angular bootstrap logic.
This happens because without html5 mode all requests are sent to root server url (mysite.com/ even if link is mysite.com/#!/profile) but when you enable html5 mode, requests are sent as declared in angular so if you have '/' route with href to profile (/profile) and reload page from there (mysite.com/profile), request will be sent to mysite.com/profile and it will respond with 404 as it is setuped to return index file only for '/' url