0

I'm redesigning an outdated site from Asp.Net to Gatsby. The site has a PDF and Word Document library. The files are stored on the actual website directory, for SEO purposes according to the client.

The client wants the ability to dynamically upload PDFs or Word Documents as their library grows.

I can do this no problem with a CMS or DAM, but the URL will no longer point to the clients site, but rather will point to the CMS or DAM.

Is it possible to upload files directly to a gatsby site (specifically the static folder) and trigger a rebuild?

silencedogood
  • 3,209
  • 1
  • 11
  • 36

1 Answers1

2

Is it possible to upload files directly to a gatsby site (specifically the static folder) and trigger a rebuild?

Not by default. All the workarounds need to be attached to some watcher in the server since Gatsby doesn't provide this feature by default.

I think you can achieve what you are trying by using chokidar (to watch folder changes) + chokidar-cli (to run bash commands):

const chokidar = require('chokidar');

var watcher = chokidar.watch('your folder path', {ignored: /^\./, persistent: true});

watcher
  .on('add', function(path) {console.log('File', path, 'has been added');})
  .on('change', function(path) {console.log('File', path, 'has been changed');})
  .on('unlink', function(path) {console.log('File', path, 'has been removed');})
  .on('error', function(error) {console.error('Error happened', error);})

From Watch a folder for changes using node.js, and print file paths when they are changed

Regarding the chokidar-cli, the -c (or --command) flag should do the trick:

Options: -c, --command

Command to run after each change.

Needs to be surrounded with quotes when command contains spaces. Instances of {path} or {event} within the command will be replaced by the corresponding values from the chokidar event.

Ferran Buireu
  • 28,630
  • 6
  • 39
  • 67
  • Okay that's helpful. And what would actually trigger the rebuild? I could imagine calling a netlify build hook in the watcher callback. Is this what you're suggesting? Also, what about the actual file upload itself? Would I just build a standard node api in the gatsby-node.js file to call via and ajax call or something? – silencedogood Jul 23 '21 at 14:08
  • It's the first time you mention Netlify but of course, the idea is to call a webhook in order to trigger the deployment. – Ferran Buireu Jul 23 '21 at 14:48
  • Okay. And what about the proper method for the actual file upload itself? Typically I'd just create an api and call it to upload the file. Can this be done in gatsby-node.js? – silencedogood Jul 23 '21 at 14:55