New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

next-remote-watch

Package Overview
Dependencies
Maintainers
4
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

next-remote-watch

decorated local server for next.js that enables reloads from data changes

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8.7K
increased by4.13%
Maintainers
4
Weekly downloads
 
Created
Source

Next Remote Watch

This script replaces next dev with a version that adds a couple extra features:

  • The ability to watch a folder that is not within your next.js project for file changes and reload when they are detected.
  • It exposes a special route, /__next_reload, which will trigger a reload when you hit it with any request. You can also POST data to this route which will be displayed in the application logs in the terminal.

The best way to implement is is to use it as an alias for whatever npm script you used to run next dev before. For example, you might run it like this in your package.json:

// ...
"scripts": {
-  "start": "next dev"
+  "start": "next-remote-watch"
}

Important Warnings and Caveats

This package utilizes undocumented APIs from next.js that are not subject to semantic versioning. This means that any version bump to next.js, major, minor, or patch, could cause it to break without warning. If you decide to adopt this package, you should lock your next.js version to patch, and be careful when upgrading.

It's also important to note that this package can only be used to replace the development server and should under no circumstances be used in production. There is no reason that its functionality would be necessary in production anyway 😀

Why Would I Want This?

This script is very useful when you are loading data in getStaticProps or getStaticPaths, and you would like for your application to reload when that data changes.

For example, if you are pulling markdown content from a different repository, you can use this script to watch that repository for changes, then when you change your nextjs files, or any of the content files in the other repository, you get an automatic page reload.

As another example, if you are pulling content from a database of any sort, you could set up a script that triggers a reload when there are changes to your data, this way you can change your database and see an automatic page reload. You could also log any information about data changes to the terminal so it's clear what changed or why the reload was triggered.

It's a small, simple utility but offers a really nice developer experience when working with a nextjs app where the content is split out from the presentation, but you could have people working on either one -- with this script the app produces the same great local dev experience with hot reloads whether the code or the data has changed.

Configuration

If you'd like to pass in a filepath to be watched, it can be passed directly to the command as such:

// ...
"scripts": {
  "start": "next-remote-watch ../some-other-folder/foo"
}

You can use globs, or pass in multiple, space-separated paths to be watched as well.

Optional Flags

OptionShorthandExampleDescriptionDefault
--root-r--root ./srcPath to Nextjs projectprocess.cwd()
--event-e--event addSpecific event to listen for (add, addDir, change, unlink, unlinkDirchange
--script-s--script ./scripts/sync.jsNode script to be called on event. The file should export a function that accepts two arguments (path, event).
--command-c--command 'node ./scripts/sync.js {event} {path}'Command to be called on event. You can use {event} and {path} in the command, which will be replaced with the respective values.
--polling-p-pWhether to use usePolling option in chokidar, typically necessary to successfully watch files over a network; for details on performance impact and use cases see chokidar documentationfalse

The Magic Reload URL

If you want changes to be triggered by something other than filesystem events, just send any request to the __next_reload route and it will hot reload. If you POST json data to the route in the following format, it will reflect some information into the app logs that display in the terminal.

{
  message: "Here's a test message!",
  color: "red / green / yellow / blue / magenta / cyan / gray / black / white" // optional
}

...that's it!

FAQs

Package last updated on 20 Sep 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc