node-ajax-seo
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -8,2 +8,31 @@ | ||
siteConfig.debug = siteConfig.debug || false; | ||
var defaultSeparator = "[---]"; | ||
var defaultBasePath = {}; | ||
var defaultPath = path.join(__dirname, '..', '..', '..', 'assets', 'dist', 'static'); | ||
defaultBasePath.url = '/'; | ||
defaultBasePath.file = 'index.html'; | ||
siteConfig.staticPages = siteConfig.staticPages || {}; | ||
// ajaxPath is compulsory | ||
if(siteConfig.staticPages === undefined){ | ||
// full equip | ||
siteConfig.staticPages = {}; | ||
siteConfig.staticPages.basePath = defaultBasePath; | ||
siteConfig.staticPages.path = defaultPath; | ||
}else{ | ||
siteConfig.staticPages.path = siteConfig.staticPages.path || defaultPath; | ||
siteConfig.staticPages.separator = siteConfig.staticPages.separator || defaultSeparator; | ||
if(siteConfig.staticPages.basePath === undefined){ | ||
siteConfig.staticPages.basePath = defaultBasePath; | ||
}else{ | ||
if(siteConfig.staticPages.basePath.url === undefined || siteConfig.staticPages.basePath.file === undefined ){ | ||
siteConfig.staticPages.basePath = defaultBasePath; | ||
} | ||
} | ||
} | ||
//console.log("node-ajax-seo", siteConfig); | ||
var fragment = req.query._escaped_fragment_; | ||
@@ -27,5 +56,5 @@ var crawlers = ['FacebookExternalHit', 'Twitterbot', 'Pinterest', 'Baiduspider', 'LinkedInBot']; | ||
if (!isCrawler) { | ||
if(siteConfig.debug) console.log(siteConfig.appPrefix+"sending to ajaxpath: ", siteConfig.indexPath); | ||
if(siteConfig.debug) console.log(siteConfig.appPrefix+"sending to ajaxpath: ", siteConfig.ajaxPath); | ||
res.setHeader('Content-Type', 'text/html'); | ||
res.sendfile(siteConfig.indexPath); | ||
res.sendfile(siteConfig.ajaxPath); | ||
}else{ | ||
@@ -43,14 +72,14 @@ fragment = req._parsedUrl.pathname; | ||
var ajaxCondition = false; | ||
if(siteConfig.ajaxCondition.pattern){ | ||
var urlRgx = new RegExp(siteConfig.ajaxCondition.pattern); | ||
if(siteConfig.nonAjaxCondition instanceof RegExp){ | ||
//if(siteConfig.ajaxCondition.pattern){ | ||
// /((^\/admin)|(\.)|(^\/$))/gi | ||
// var urlTest = req.url.match(urlRegex); | ||
// console.log("URLTEST -[[",urlTest,"]] test: ",(urlTest == null || urlTest.length == 0)); | ||
ajaxCondition = !urlRgx.test(req.url); | ||
ajaxCondition = !siteConfig.nonAjaxCondition.test(req.url); | ||
if(siteConfig.debug) console.log(siteConfig.appPrefix+"using REGEX condition",req.url); | ||
if(siteConfig.debug) console.log(siteConfig.appPrefix+"using REGEX condition",req.url,ajaxCondition); | ||
}else if(siteConfig.ajaxCondition.toEval !== ""){ | ||
}else if(siteConfig.nonAjaxCondition !== ""){ | ||
// using EVAL condition | ||
//ajaxCondition = (req.url.indexOf('.') == -1 && req.url != '/' && req.url.indexOf('/admin') == -1); | ||
ajaxCondition = eval(siteConfig.ajaxCondition.toEval); | ||
ajaxCondition = eval(siteConfig.nonAjaxCondition); | ||
} | ||
@@ -57,0 +86,0 @@ |
{ | ||
"name": "node-ajax-seo", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "It deals with the most popular crawlers, redirecting them to static directory and serving fresh pages to human users.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
node-ajax-seo | ||
============= | ||
Simple node plugin that deals with the most popular crawlers, redirecting them to static directory and serving fresh pages to human users. **It doesn't generate your snapshots.** | ||
Simple node plugin that deals with the most popular crawlers (Google, Facebook, Twitter, Baidu, LinkedIn), redirecting them to static snapshots while serves fresh pages to human users. **It doesn't generate your snapshots, only routes.** For that sort of things we have [some other nice modules](https://www.github.com/ericzon/node-ajax-printer) ;-) | ||
[npmjs page](https://www.npmjs.com/package/node-ajax-seo) | ||
## Installation | ||
@@ -12,3 +14,49 @@ | ||
Options: | ||
-nonAjaxCondition (regex|string) regex condition (better choice this) or typical "if condition" as string to delimite non-ajax pages. | ||
For example: | ||
/((^\/admin)|(^\/api)|(\.)|(^\/$))/ | ||
Same result as: | ||
"(req.url.indexOf('/admin') == -1 && req.url.indexOf('/api') == -1 && req.url.indexOf('.') == -1 && req.url != '/')" | ||
-ajaxPath (string) path to your main SPA .html by default. | ||
-staticPages.path (string) path to your static files. | ||
-staticPages.separator (string) in your static snapshots, filenames contain some token replacing "/" path ("[---]" by default). | ||
-staticPages.basePath.url (string) basepath is an special case: "when path is X, serve file Y" ("/" by default). | ||
-staticPages.basePath.file (string) ("index.html" by default). | ||
-debug (boolean) Enables debug messages (false by default). | ||
## Examples | ||
```javascript | ||
var ajaxSeo = require("node-ajax-seo"); | ||
``` | ||
Minimal config: | ||
```javascript | ||
var siteConfig = { | ||
nonAjaxCondition: /((^\/admin)|(^\/api)|(\.)|(^\/$))/, | ||
ajaxPath: path.join(__dirname, 'assets', 'index.html'), | ||
staticPages: { | ||
path: path.join(__dirname, 'assets', 'dist', 'static'), | ||
} | ||
}; | ||
ajaxSeo.dealWithAjax(siteConfig, req, res, next, function cbk(err) { | ||
if (err) { | ||
console.log(err); | ||
} | ||
else { | ||
console.log(siteConfig.appPrefix+'Sent:', path.join(filePath,fragment)); | ||
} | ||
}); | ||
``` | ||
Normal config: | ||
```javascript | ||
@@ -18,4 +66,4 @@ app.get("/*", function(req, res,next) { | ||
/** | ||
* It's necessary to define a pattern that matches with not ajax requests: | ||
* In this case all the paths are ajax except: | ||
* It's necessary to define a pattern that matches with non ajax requests: | ||
* In this example all the paths are ajax except: | ||
* | ||
@@ -28,16 +76,13 @@ * - /admin and /api paths. | ||
var siteConfig = { | ||
ajaxCondition:{ | ||
pattern: /((^\/admin)|(^\/api)|(\.)|(^\/$))/ // you can specify the condition using regex or typical if condition | ||
//toEval: "(req.url.indexOf('.') == -1 && req.url != '/' && req.url.indexOf('/admin') == -1)" | ||
}, | ||
indexPath: path.join(__dirname, 'assets', 'index.html'), // your main angular .html by default | ||
nonAjaxCondition: /((^\/admin)|(^\/api)|(\.)|(^\/$))/, | ||
ajaxPath: path.join(__dirname, 'assets', 'index.html'), | ||
staticPages: { | ||
path: path.join(__dirname, 'assets', 'dist', 'static'), // path to your static files | ||
separator: "[---]", // in your static files, the filenames contain some token replacing "/" path. | ||
path: path.join(__dirname, 'assets', 'dist', 'static'), | ||
separator: "[---]", | ||
basePath: { | ||
url: "/", | ||
file: "home.html" // the url basepath is an special case | ||
file: "home.html" | ||
} | ||
}, | ||
debug: false // false by default | ||
debug: false | ||
}; | ||
@@ -60,3 +105,4 @@ | ||
}); | ||
``` | ||
## Tests | ||
@@ -81,3 +127,3 @@ | ||
* connect with static page generator (WIP) | ||
* Connect with [node-ajax-printer](https://www.github.com/ericzon/node-ajax-printer) (WIP). | ||
10383
108
125