diff --git a/README.md b/README.md index 4b2b786..dd32978 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ A javascript library providing server defined loading of assets for a single pag * server.timeout = The timeout to download the server resources. (default: 4096) * server.flag = The window flag which is set when the server.url is know. (default: 'FFServerUrl') * server.header.request = An key value object with the request headers. (default: {'X-FFSpaLoader': 'sync'}) + * server.header.response = An key value object with the required response headers. (default: {}) * question.transport = The transport to prefix the server.url with. (default: 'http://') * question.title = The question ui title. (default: 'Server') * question.submit = The start button text. (default: 'Start') @@ -198,7 +199,6 @@ A javascript library providing server defined loading of assets for a single pag ## Todo * test in production - * Server header check support * Add instance websql options so it can also be used in application code. * Split assets per type so do js first then boot then css + cssData. * Add more tests @@ -215,10 +215,11 @@ Add unit tests for any new or changed functionality. Lint and test your code. ## Release History ### 0.2.0 -* Dropped error.style and question.style for css file. +* Moved error.style and question.style to css file. * Change dist with extra css/js folder. * Added loader progress bar. * refined css so question/loader/error are equal. +* Added response header check support. ### 0.1.1 * Moved websql delete timeout to cleanServerlUrl for faster boot. diff --git a/es5-ff-spa-loader.js b/es5-ff-spa-loader.js index 468978d..eb74cd1 100644 --- a/es5-ff-spa-loader.js +++ b/es5-ff-spa-loader.js @@ -74,7 +74,7 @@ request: { 'X-FFSpaLoader': 'sync' }, - response: { // TODO: add header check support + response: { } } }, @@ -336,6 +336,23 @@ httpRequest.onreadystatechange = function() { if (httpRequest.readyState === 4 && httpRequest.status === 200) { utilDebug('utilHttpFetch url \"'+url+'\" done in '+(new Date().getTime()-startTime)+' ms.'); + + var headerResponseKeys = Object.keys(options.server.header.response); + for (var headerResponseKeyIdx in headerResponseKeys) { + var headerResponseKey = headerResponseKeys[headerResponseKeyIdx]; + var headerResponseValue = options.server.header.response[headerResponseKey]; + var value = httpRequest.getResponseHeader(headerResponseKey); + if (value === null) { + return cb('Header missing: '+headerResponseKey); + } + if (headerResponseValue === null) { + continue; + } + if (headerResponseValue !== value) { + return cb('Header mismatch: '+headerResponseKey); + } + } + cb(null, httpRequest); } else if (httpRequest.readyState === 4) { cb('Wrong status '+httpRequest.status); @@ -602,7 +619,7 @@ var stepProgres10 = 0; var createUITimeout = setTimeout( function () { - utilDebug('createLoaderBar'); + utilDebug('createLoaderBar after timeout: '+options.loader.await); rootTag = document.createElement('div'); rootTag.setAttribute('class','ffWrapper'); @@ -746,10 +763,8 @@ var progressBar = createLoaderBar(resources); var loadResourceStep = function (resource, cb) { - //setTimeout(function() { loadResource(resource,cb); progressBar.nextResource(resource); - //}, 1000); } if (cacheHasService('meta')) { @@ -819,10 +834,9 @@ var resourcesUrl = options.server.url + options.server.assets; utilDebug('askUrlStart check assets '+resourcesUrl); - // TODO: check headers utilHttpFetch(resourcesUrl,function(err, httpRequest) { if (err !== null) { - inputErrorTag.appendChild(document.createTextNode('Error could not get data.')); + inputErrorTag.appendChild(document.createTextNode('Error '+err)); return; } if (httpRequest.responseText.length === 0) { diff --git a/example/app_mobile/www/index.html b/example/app_mobile/www/index.html index c232cbe..7057e77 100644 --- a/example/app_mobile/www/index.html +++ b/example/app_mobile/www/index.html @@ -15,6 +15,7 @@ FFSpaLoader.options.debug.enable = true; FFSpaLoader.options.boot.angular.modules.push('exampleUI'); FFSpaLoader.options.server.assets = '/static/spa-client-resources'; + FFSpaLoader.options.server.header.response['X-My-Api'] = 'noknok'; FFSpaLoader.start(); diff --git a/example/app_server/example.js b/example/app_server/example.js index b211e63..7cfe5bf 100644 --- a/example/app_server/example.js +++ b/example/app_server/example.js @@ -104,6 +104,7 @@ server.use(morgan('dev')); server.use(cors({credentials: true, origin: '*'})); server.set('view engine', 'ejs'); server.set('views', path.join(__dirname,'www_views')); +server.use(function(req, res, next) { res.header('X-My-Api', 'noknok');next(); }); server.use('/static', express.static(path.join(__dirname,'www_static'))); server.use('/static/module/bootstrap', express.static(path.join(__dirname,'../node_modules/bootstrap/dist'))); server.use('/static/module/jquery', express.static(path.join(__dirname,'../node_modules/jquery/dist'))); diff --git a/example/app_server/www_views/index.ejs b/example/app_server/www_views/index.ejs index 15bc3bb..d4508a6 100644 --- a/example/app_server/www_views/index.ejs +++ b/example/app_server/www_views/index.ejs @@ -11,6 +11,7 @@ FFSpaLoader.options.debug.enable = true; FFSpaLoader.options.boot.angular.modules.push('exampleUI'); FFSpaLoader.options.server.assets = '/static/spa-client-resources'; + FFSpaLoader.options.server.header.response['X-My-Api'] = 'noknok'; FFSpaLoader.start();