keuhda
Last Updated: February 25, 2016
·
5.365K
· bripkens

Simple Grunt Task to reload Chrome on OS X

Automatically reload your browser on every save with this custom reload task and the watch task.

1) Add the new task

grunt.registerTask("reload", "reload Chrome on OS X",
    function() {
  require("child_process").exec("osascript " +
      "-e 'tell application \"Google Chrome\" " +
        "to tell the active tab of its first window' " +
      "-e 'reload' " +
      "-e 'end tell'");
});

2) Configure your watch Task

// ...
watch: {
  files: ["src/**/*"],
  tasks: "less concat reload"
}
// ...

3) Use it

grunt watch

8 Responses
Add your response

2934
over 1 year ago ·
2936

@eskimoblood Right, there are a few plug-ins which do it in a similar manner. Both solve this problem differently:

grunt-macreload changes the windows focus to the browsers, refreshes it and then refocuses your editor. I do not want my Editor to lose focus so instead I am using a Chrome specific refresh command.

grunt-reload uses WebSockets and some code injection. This might be OK for you, but I want to see my page and only its resources during development just like an end-users would see it.

over 1 year ago ·
2937

Ah, thanks for the clarification. Is your solution necessarily bound to chrome or would it work with other browser too?

over 1 year ago ·
2939

@eskimoblood Never tried it with other browsers, but I would assume that the command is Chrome specific. Let me know when you try it.

over 1 year ago ·
4402

Thanks ben! This is much simpler and cleaner than installing one of the other grunt reload plugins. Just a note, there's a missing "+" on the 6th line of the first code block

over 1 year ago ·
4408

@smartalec43 Thank you, I fixed it!

over 1 year ago ·
8070

Cool

over 1 year ago ·
9054

@eskimoblood Its just AppleScript. Can easily be made to work with different browsers.. For Safari:

tell application "Safari" to tell the current tab of its first window
do JavaScript "window.location.reload()"
end tell

over 1 year ago ·