Most Ajax pages are not SEO friendly, so the natural question is how to implement this in ZK. The question is also applies if your ZK application works using a Bookmark mechanism. You can implement SEO in an easy manner following these two steps.

Here is the whole ZK SEO-Workflow

ZK SEO-Workflow

We create a SEODesktopInit to receive the bookmark changed event which is fired by customized Javascript we provided in the zk.xml. You can store the bookmark mapping information to where you want (like a Database), in this case, we store them in a static HashMap object.
We implement the SEORenderer interface to render the related links for the SEO spider. Once the spider visits the link, the SEOComposer will handle the request and post a bookmark change event. On bookmark charge the relevant page will be created. This page is exactly the same as visiting from a bookmark url (i.e. zksandbox/userguide/#f1)

Steps

1. Copy the following Javascript code into your zk.xml.
For example,


	ajax
	
		(function () {
		var _zAuBookmark = {};
		var _domain = zk.ajaxURI(null, {desktop:zk.Desktop._dt}) + '/' || '';
		function getURI() {
			var nm = location.href,
				start = nm.indexOf(_domain),
				end = nm.indexOf('#');
			nm = decodeURIComponent(nm.substring(start + _domain.length, end >= 0 ? end : nm.length));
			return nm || "";
		}
		zk.override(zAu.cmd0, _zAuBookmark, {
			bookmark: function(bk, replace) {
				var uri = getURI();
				zAu.send(new zk.Event(zk.Desktop._dt, "onBookmarkSEO", {nm: bk, uri: uri}));
				_zAuBookmark.bookmark(bk, replace);
			}
		});
		})();
		
	]]>

2. Specify the following setting in your zk.xml.
For example,


        For bookmark SEO
        SEODesktopInit


    SEOComposer


    org.zkoss.zk.ui.sys.SEORenderer.class
    SEORenderer

Note: the preference of “org.zkoss.zk.ui.sys.SEORenderer.class” is applicable to ZK 5.0.6 version.

Here are the implementations:

1.SEODesktopInit.java

public class SEODesktopInit implements org.zkoss.zk.ui.util.DesktopInit {
	public static Map SEOMap = new HashMap();
	public void init(final Desktop desktop,  Object request) throws Exception {
		desktop.addListener(new AuService() {

			public boolean service(AuRequest request, boolean everError) {
				final String cmd = request.getCommand();
				if (cmd.equals("onBookmarkSEO")) {
					String name = (String) request.getData().get("nm");
					String uri = (String) request.getData().get("uri");
					SEOMap.put(name, uri);
					return true;
				} else return false;
			}
		});
	}
}

2.SEORenderer.java

public class SEORenderer implements org.zkoss.zk.ui.sys.SEORenderer {
	public void render(Page page, Writer out) throws IOException {
		for (Iterator it = SEODesktopInit.SEOMap.entrySet().iterator(); it.hasNext();) {
			Map.Entry me = (Map.Entry)it.next();
			String url = Executions.encodeURL("/" + me.getValue());
			if (url.indexOf("?bk=") >= 0) {
				int start = url.indexOf("?bk=");
				int end = url.indexOf("&", start + 4);
				url = url.substring(0, start) + "?bk=" + me.getKey() + url.substring(end + 1, url.length()); 
			} else if (url.indexOf("&bk=") >= 0) {
				int start = url.indexOf("&bk=");
				int end = url.indexOf("&", start + 4);
				url = url.substring(0, start) + "&bk=" + me.getKey() + url.substring(end + 1, url.length());
			} else {
				if (url.indexOf("?") >=0) {
					url += "&bk=" + me.getKey();
				} else {
					url += "?bk=" + me.getKey();
				}
			}
			out.write(""+me.getKey()+"");
		}		 
	}
}

3.SEOComposer.java

public class SEOComposer implements Composer {
	public void doAfterCompose(Component comp) throws Exception {
		// only handle once.
		Object seo = comp.getDesktop().getAttribute("SEO");
		if (seo == null) {
			String name = Executions.getCurrent().getParameter("bk");
			if (!Strings.isEmpty(name)) {
				Events.postEvent(new BookmarkEvent("onBookmarkChange", name));
				comp.getDesktop().setAttribute("SEO", Boolean.TRUE);
			}
			
		}
	}
}

Download

You can download the example here.

Question: How to integrate the new Ajax bookmark crawling method with Google

The question is how do we integrate the mechanism (#!) with Google’s described methodology from Google’s the tutorial . The answer is to modify the SEOComposer.java to recognize the request parameter that Google passed and post a onBookmarkChange event for ZK.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

One Response to “Another approach to SEO in ZK Applications”

  1. rene says:

    Hello,

    I am writing from Cusco Peru, I have my website http://www.porcelltoursperu.com and I would like to make the seo,but here the people who work in this they say it is a little difficult to make the positioning of my website because they don´t know how it was made, etc, they only work with html format, so please tell me if you can make the positioning, if you do that, i will be very pleased to work with you.or please give me some details to put my website in internet to sell my tours.

    Thanks for your prompt reply.

    Regards.

    Rene Porcel

Leave a Reply