Skip to content

Commit

Permalink
Added baseTag and options.
Browse files Browse the repository at this point in the history
Added option 'deliverStatics' that defaults to true.
When disabled no 'express.static' instance will get started, static files must be provided by the user then.
Added option 'baseTag' defaults to true in 'blocks.middleware'.

Adds a base tag to the head e.g.:
when the current url is 'http://localhost:8000/web/home' and the middleware is mounted on '/web'
the option will add '<base href="http://localhost:8000/web/"></base>' to the head.
  • Loading branch information
Joscha Rohmann committed Dec 9, 2015
1 parent 2af9161 commit 54b6ecc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/node/Middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ define([
Middleware.Defaults = {
static: 'app',
blocksPath: 'node_modues/blocks/blocks.js',
cache: true
cache: true,
baseTag: false
};

Middleware.prototype = {
Expand Down
50 changes: 32 additions & 18 deletions src/node/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,43 @@ define([
return new Server(options);
};

blocks.static = function (options) {

};

blocks.middleware = function (options) {
var express = require('express');
var path = require('path');
// the real middleware
var middleware;
// array of middlewares to return
var middlewares = [];
var express = require('express');
var path = require('path');
// the real middleware
var middleware;
// array of middlewares to return
var middlewares = [];

options = blocks.extend({}, Middleware.Defaults, options);
middleware = new Middleware(options);
var defaults = {
// Should the files in static get delivered
deliverStatics: true,
baseTag: true
};

// express.static is required as the frontend app needs it's files
middlewares.push(express.static(path.resolve(options.static), {
index: false
}));
if (blocks.isString(options)) {
options = {
static: options
};
}

middlewares.push(blocks.bind(middleware.tryServePage, middleware));
options = blocks.extend({}, Middleware.Defaults, defaults, options);
middleware = new Middleware(options);

// returning array of express middlewares that express will call in that order
return middlewares;
};
if (options.deliverStatics) {
// express.static is required as the frontend app needs it's files
middlewares.push(express.static(path.resolve(options.static), {
index: false
}));
}

blocks.static = function (options) {

middlewares.push(blocks.bind(middleware.tryServePage, middleware));

// returning array of express middlewares that express will call in that order
return middlewares;
};
});
8 changes: 8 additions & 0 deletions src/node/overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ define([
server.await(function () {
if (head) {
head.children().splice(0, 0, getServerDataScript());
if (server.options.baseTag) {
head.children().splice(0, 0, getBaseTag());
}
}
server.rendered = root.renderChildren();
});
Expand All @@ -71,6 +74,11 @@ define([
return result;
}

function getBaseTag() {
var baseUrl = window.location.protocol + '//' + window.location.host + window.__baseUrl__ + '/';
return VirtualElement('base').attr('href', baseUrl);
}

function getServerDataScript() {
return VirtualElement('script').html('window.__blocksServerData__ = ' + JSON.stringify(server.data)).render();
}
Expand Down

0 comments on commit 54b6ecc

Please sign in to comment.