Skip to content

Allowing options.locals to be specified to pass in parameters for vie…#13

Open
engineersamuel wants to merge 2 commits intomartyjs:masterfrom
engineersamuel:master
Open

Allowing options.locals to be specified to pass in parameters for vie…#13
engineersamuel wants to merge 2 commits intomartyjs:masterfrom
engineersamuel:master

Conversation

@engineersamuel
Copy link
Copy Markdown

I was attempting to integrate react-starter (webpack) + marty isomorphism and needed to pass various parameters to the view engine (ejs) however the current build hardcoded the starting locals as {}. This will allow passing options like so (based on the martyjs ex):

    app.use(require('marty-express')({
        routes: require('../app/routes'),
        application: require('../app/application'),
        locals: {
            SCRIPT_URL: scriptUrl
        },
        rendered: function (result) {
            console.log('Rendered ' + result.req.url);

            var table = new Table({
                colWidths: [30, 30, 30, 30, 40],
                head: ['Store Id', 'Fetch Id', 'Status', 'Time', 'Result']
            });

            _.each(result.diagnostics, function (diagnostic) {
                table.push([
                    diagnostic.storeId,
                    diagnostic.fetchId,
                    diagnostic.status,
                    diagnostic.time,
                    JSON.stringify(diagnostic.result || diagnostic.error || {}, null, 2)
                ]);
            });

            console.log(table.toString());
        }
    }));

Where the index.ejs contains:

    <script type="text/javascript" src="<%- SCRIPT_URL %>"></script>

In this instance the SCRIPT_URL is generated by webpack hot dev mode:

    var scriptUrl = publicPath + [].concat(stats.assetsByChunkName.main)[0];

Also adding an ability to pass a blacklist of regular expressions for things such as API. It is very basic, feel free to improve upon it, but just an array over /something/.test(req.url)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant