sapper icon indicating copy to clipboard operation
sapper copied to clipboard

Watch custom directories

Open dkzlv opened this issue 5 years ago • 14 comments

Is your feature request related to a problem? Please describe. If you have a content-driven blog, that uses Markdown processor, you want rollup/webpack to watch certain directories, where you hold your content files. Otherwise all the content is pre-processed during the build phase and you need to restart the sapper dev command to get fresh content.

Describe the solution you'd like Would be awesome, if sapper dev command would respect the watch-attributes of rollup config.

Describe alternatives you've considered Write a wrapper script, that would restart sapper dev every time something changes in the directory, but the page won't refresh and it is just kind of messy.

How important is this feature to you? Somehow quite important. I've already written the said script 😅

Additional context I use sapper to power my blog. I have a src/posts/ folder, where I keep all the posts in markdown-format. And I have src/routes/[slug].json.js file, where I read files in this folder, convert them to HTML and serve.

dkzlv avatar Jul 30 '19 17:07 dkzlv

I think this relates very closely to the idea behind https://github.com/sveltejs/sapper/pull/853

antony avatar Aug 27 '19 18:08 antony

I'm keen on this feature too. I also have a set of markdown posts that I'd like Sapper to watch and the browser to reload while editing.

ben-rogerson avatar Jan 22 '20 06:01 ben-rogerson

I'm keen on this feature too. I also have a set of markdown posts that I'd like Sapper to watch and the browser to reload while editing.

I'm having the same issue, currently I'm editing my markdown files, and I didn't find a way to update those automatically, is there any workaround?

mromanuk avatar Feb 04 '20 14:02 mromanuk

I'm keen on this feature too. I also have a set of markdown posts that I'd like Sapper to watch and the browser to reload while editing.

I'm having the same issue, currently I'm editing my markdown files, and I didn't find a way to update those automatically, is there any workaround?

Im trying to do the same with this.addWatchFile and seems to work on individual files, but not on folders:

export default {
    client: {
		input: config.client.input(),
		output: config.client.output(),
		plugins: [{
        	buildStart() {
				this.addWatchFile('static/posts/post1.md') // works
				this.addWatchFile('static/posts') // Does not work
				this.addWatchFile('static/**/*') // Does not work
			}
		},
...
}

EDIT: quick dirty fix (wont work on new files until restarted though)

buildStart() {
    const files = fs.readdirSync('./static/posts/');
	files.forEach(file => {
		this.addWatchFile('static/posts/' + file)
	});
}

ajboni avatar Feb 08 '20 01:02 ajboni

EDIT: quick dirty fix (wont work on new files until restarted though)

buildStart() {
    const files = fs.readdirSync('./static/posts/');
	files.forEach(file => {
		this.addWatchFile('static/posts/' + file)
	});
}

Thank you @ajboni !

Your code should go in server, right? For my own case, I've added globbing because my posts are inside a directory:


import glob from 'glob'

server: {
...
plugins: [
       //...,
      {
        buildStart() {
          var self = this
          const postsDir = './src/_articles'
          glob(postsDir + '/**/*.md', null, function(er, files) {
            files.forEach(file => {
              self.addWatchFile(file)
            })
          })
        }
      }
     //...
     ]

mromanuk avatar Feb 08 '20 14:02 mromanuk

Thank you @ajboni !

Your code should go in server, right? For my own case, I've added globbing because my posts are inside a directory:

I've put it on client, but now I'm having doubts on where it belongs... It's working anyway heh

ajboni avatar Feb 20 '20 03:02 ajboni

For my use case, I wasn't able to get it working using the suggestions above.

I generate a server from markdown files which are fetched using node-fetch and window.fetch. The server automatically updates so that when the page manually reloads the updated content appears but I can't get Sapper to hot-reload when the file changes.

I've confirmed using the suggestions above that it's watching the files and they are successfully recognised when they're updated. But the client and server aren't reloading and therefore fetching the new content.

update: I was able to get it working. It appeared there was something causing my client bundle to error. I had to place it in the server config, I'm not sure why that as opposed to the client, but it seems to work. Thanks!

gavinmcfarland avatar Jun 19 '20 11:06 gavinmcfarland

For me, the snippets from @ajboni and @mromanuk are throwing

TypeError: this.addWatchFile is not a function
  server: {
    // ...
    plugins: [
      {
        buildStart() {
          glob('content/**/*', null, function (err, files) {
            files.forEach((file) => {
              this.addWatchFile(file)
            })
          })
        },
      },
    ],
    // ...
  },

janosh avatar Oct 10 '20 11:10 janosh

For me, the snippets from @ajboni and @mromanuk are throwing

TypeError: this.addWatchFile is not a function
  server: {
    // ...
    plugins: [
      {
        buildStart() {
          glob('content/**/*', null, function (err, files) {
            files.forEach((file) => {
              this.addWatchFile(file)
            })
          })
        },
      },
    ],
    // ...
  },

This was working for me back then: https://www.github.com/ajboni/svelte-sapper-static-blog/tree/master/rollup.config.js

ajboni avatar Oct 10 '20 14:10 ajboni

Oops, my mistake. I put that code snippet in server rather than client. Thanks!

janosh avatar Oct 11 '20 09:10 janosh

Follow up question: rollup is now detecting changes and rebuilding but the changes don't appear on the page. I assume that's because I'm watching Markdown and Yaml files and the file that parses these content files isn't being rerun. Would I need something like rollup.watch to rerun the parser.js when a START event fires?

janosh avatar Oct 11 '20 10:10 janosh

If I put that buildStart snippet into the client it builds but doesn't refresh. If I put it into the server it builds and refreshes but doesn't update the page content. If I put it in both server and client it builds and refreshes and sometimes updates the content but sometimes doesn't.

Is there any way to get this working properly? I don't like having to stop and start the dev server manually whenever I need to change something in the static files.

JamesCoyle avatar Oct 23 '20 14:10 JamesCoyle

@JamesCoyle

If I put that buildStart snippet into the client it builds but doesn't refresh. If I put it into the server it builds and refreshes but doesn't update the page content. If I put it in both server and client it builds and refreshes and sometimes updates the content but sometimes doesn't.

yeah, i imagine when you (properly) have both the server reload content and client reload page it depends on which gets reloaded first to determine if you're going to see the new content when your page reloads.

i'd also like a supported solution for this!

zhammer avatar Dec 23 '20 13:12 zhammer

is there any solutions now?

Zeng1998 avatar May 19 '21 07:05 Zeng1998