Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

engineersamuel
Copy link

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