Search code examples
githubserver-side

How do I store server-side data on github?


I am using Github pages to build a website, but I can't figure out how to store data on the server-side. I can use localStorage and sessionStorage, but those are client-side. Does anyone have a way to use javascript or another programming language that works on Github to store data? Or can static websites not store data?

I have already googled "server-side Github" and "javascript server-side" and researched other programming languages like PHP(doesn't work on Github)


Solution

  • Static websites would need to communicate with a server through an API to store data. You could run that server yourself or use a third-party service to do it. If you're just starting out and prototyping a new app, it makes sense to leverage existing services as much as possible to validate your product.

    If you've been using localStorage on the client-side and want a similar API where the data is stored on a server, I recommend looking into a remote key-value store like KVdb.io (disclaimer: I built it, but it's free to use!).

    For example:

    <script src="https://unpkg.com/kvdb.io@v1.0"></script>
    <script>
    const kvdbStorage = KVdb.bucket('MY_BUCKET_ID').localStorage()
    
    kvdbStorage.setItem('my-key', 'my-value')
      .then(() => console.log('key saved')
      .then(() => kvdbStorage.getItem('my-key'))
      .then(value => console.log('get value', value))
      .catch(err => console.error(err)
    </script>
    

    If your data can be modeled as key-value pairs, this approach can get you pretty far without having to manage a database yourself.