기본 콘텐츠로 건너뛰기

라벨이 ajax인 게시물 표시

node.js에서 angular.js 같은 동적 웹페이지를 사용할 때 crawling 문제

이전글 ( http://spectrumdig.blogspot.kr/2013/10/spiderable-package.html )에서 meteor가 어떻게 봇들에게 렌더링된 이후의 html을 전달하는지 보았다. 근데 만일 node.js에서 같은 구현을 해야한다면? 역시 request를 까발려서 _escaped_fragment_ 가 있는지 user-agent가 어떤 것인지 분별하면 된다. 방법이야 여러가지가 있을 텐데 preprocessor를 사용하여 가로채서 조건에 맞으면 처리하고 아닐 경우 next()로 포워딩하는 방법이 있고 ( https://github.com/acidsound/pushpot/blob/master/app.js#L30 ) Express/Connect Middle-ware를 사용하여 app.use(function(req, res, next){   if (req.query._escaped_fragment_ ||     [/^facebookexternalhit/i, /^linkedinbot/i, /^twitterbot/i].some(function(v) {       return v.test(req.headers['user-agent']);     })) {       // render by phantomJS       res.send(getHTMLfromPhantomJS(req.url));   } else {     next();   } 이런 식으로 처리해주면 되겠다.발로 짜서 실제 작동 여부는 모르겠다. getHTMLfromPhantomJS 는 물론 직접 구현하시라 :p https://github.com/meteor/meteor/blob/devel/packages/spiderable/spiderable.js 이런식으로 구현하면 됨.

spiderable package를 살펴보았다.

동적페이지를 만들면 웹크롤러들이 찌르러 왔다가 빈손으로 돌아가곤 한다. 이래서야 안될일. 발번역부터 시작해보자. // list of bot user agents that we want to serve statically, but do // not obey the _escaped_fragment_ protocol. The page is served // statically to any client whos user agent matches any of these // regexps. Users may modify this array. // // An original goal with the spiderable package was to avoid doing // user-agent based tests. But the reality is not enough bots support // the _escaped_fragment_ protocol, so we need to hardcode a list // here. I shed a silent tear. 우리가 정적으로 제공하고 싶지만 _escaped_fragment_ 프로토콜을 따르지 않는 봇 사용자 에이전트의 목록입니다. 페이지는 사용자 에이전트가 이러한 정규식 중 하나와 일치하는 모든 클라이언트에 정적으로 제공합니다. 사용자는 이 배열을 변경할 수 있습니다. spiderable 패키지와 원래 목표는 사용자 에이전트 기반의 테스트를 실시하는 것을 피하기 위해서였다. 하지만 현실은 봇들은 _escaped_fragment_ 프로토콜을 지원하지 않기 때문에, 여기에서는 목록을 하드 코딩해야합니다. 나는 조용히 눈물을 흘렸다. Spiderable.userAgentRegExps = [     /^facebookexternalhit/i, /^linkedinbot/i, /^twitterbot/i]; 요놈들! 말을 듣지 않는 나쁜 아이들이 니 놈들이렸다! 이 오라질 것들...