Search code examples
node.jsgitconventionsstatic-files

Is there a standard way to serve latest version of static files from a git repository with Node.js?


Is there any standard/conventional way (package/library, pattern, etc.) to serve the latest version of some static files from a given git repository in Node.js?

My idea so far is to clone the repository at npm start and take the files that I need from there, and to provide a webhook to be called when the repository receives a commit, to then pull the changes so that the files get updated even when the app is running.

This seems like it could work, but I do not want to reinvent the wheel, if it already exists. If there is a method that is already relatively well known, a conventional pattern or a package that already does this, it would be wise to use it and not bother with implementing the details, fixing security vulnerabilities and keeping it up to date.

Also, while we are on it, are there reasons why I should not do this in the first place?

Edit:
I should probably give an explanation on why I would want to do this.
I basically have a web app (nothing too complex, a few routes here and there), let's call it example.com, and a few "self contained sites" (i.e. collections of static files that for all intents and purposes could live independently of the main website and each other).

I could put them each on their sub-domain, but that would probably mean using more app instances and time for just serving some static files, so I want to serve these "sub-websites" on their own paths on that app, like example.com/sub-site1, example.com/sub-site2, etc., from the same app instance as the main example.com, as to not incur higher hosting cost.

I could also modify those static websites to wrap them in Node.js packages and install the packages in the main app, but I want to keep them clean, simple, static files, agnostic of the platform they are being served from.

That leaves me with the option of moving a bunch of files to the main app, but I do not want to manually restart the app each time any one of those sites gets updated, or even worse, manually update the repository of the main app with the new versions of the static files.


Solution

  • Is there any standard/conventional way (package/library, pattern, etc.) to serve the latest version of some static files from a given git repository in Node.js?

    There is no standard for this kind of task. The best way is to use the official Github SDK since there is a getContent method for this purpose.

    Also, while we are on it, are there reasons why I should not do this in the first place?

    A Github repository can be deleted, but an npm package cannot (there is a unpublish policy for this reason). I don't know your use cases, but those files are critical for your application? If so, it is risky to depends like this on an ephemeral repository: you could just fork and keep the fork up-to-date with a Github action