Code Monkey home page Code Monkey logo

marko-hot-reload's Introduction

Marko Sample - Hot Reloading

Marko provides a hot-reloading feature that allows templates to automatically be reloaded without a restart. Your app must explicitly turn on this feature as described below and you would typically only enable hot-reloading in development (not production).

Get started

git clone [email protected]:marko-js-samples/marko-hot-reload.git
cd marko-hot-reload
npm install
node server

Enabling Hot Reloading

There are two steps to allowing Marko templates to be hot reloaded on the server:

Step 1: Enable hot reloading:

require('marko/hot-reload').enable();

Step 2: Tell the Marko runtime when a template is modified:

Marko does not come with a mechanism for watching Marko template files for changes. Your app must include the code for watching a change to a file and then pass along the full template path to Marko so that the modified template can be hot reloaded. For example, using the builtin fs file watching service:

require('fs').watch(templatesDir, function (event, filename) {
    if (/\.marko$/.test(filename)) {
        // Resolve the filename to a full template path:
        var templatePath = path.join(templatesDir, filename);

        console.log('Marko template modified: ', templatePath);

        // Pass along the *full* template path to marko
        require('marko/hot-reload').handleFileModified(templatePath);
    }
});

The chokidar module provides more robust file watching functionality that works across most OSs.

Using browser-refresh

You can also use the browser-refresh module to watch for changes to Marko template files. You just need to add the following line of code into the main script for your app:

require('marko/browser-refresh').enable();

Then you would use browser-refresh to launch your Node.js app. For example:

npm install browser-refresh --global
browser-refresh server.js

In addition, you'll need to include the <browser-refresh> tag in your main page template as shown below:

<!doctype html>
<html>
    <head>
        ...
    </head>
    <body>
        ...

        <browser-refresh enabled="true" />
    </body>
</html>

In addition, you will need to let the browser-refresh process launcher know when your server is ready so that it can trigger a refresh of all web pages at the correct time. This can be done using code similar to the following:

app.listen(port, function() {
    console.log('Listening on port %d', port);

    if (process.send) {
        process.send('online');
    }
});

For more details, please see the docs for the browser-refresh module.

marko-hot-reload's People

Contributors

patrick-steele-idem avatar

Watchers

James Cloos avatar Jasbeer Singh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.