Quick Netlify Dev Tip for Complex Static Sites

Quick Netlify Dev Tip for Complex Static Sites

Hey everyone, this tip will probably have a very limited audience, but it was a huge deal for me so I wanted to share it with others in case they run into the same issue. In case you don't know it, Netlify Dev is a way to run the Netlify Platform locally. Which means features like redirects, functions, and more will work locally.

As a practical example, I use the redirects feature quite a bit as my site has gone through different engines and domains over the years. Being able to quickly test that support locally is awesome.

However, I ran into an interesting issue with my site. This blog has over six thousand entries. A complete build takes a bit over ten minutes. So when I work locally, I use a different config file that modifies the exclude parameter:

exclude: [_posts/2003,_posts/2004,_posts/2005,_posts/2006,_posts/2007,_posts/2008,_posts/2009,_posts/2010,_posts/2011,_posts/2012,_posts/2013,_posts/2014,_posts/2015,_posts/2016,_posts/2017,_posts/2018/01,_posts/2018/02,_posts/2018/03,_posts/2018/04,node_modules]

I also wrote a quick shell script called start.sh to make using this config easier:

bundle exec jekyll serve --config _config.dev.yml 

As a quick aside, the jekyll CLI does support a "only render last N posts" option, but I discovered that after I had used the exclude feature. Like with most things there's more than one way to solve the problem.

This different configuration takes my typical build time down to about five seconds which is more than quick enough.


Smiling Cat

Unfortunately, when I started using Netlify Dev, I noticed immediately that my builds were taking the usual, very long, time. I was ok with it a bit as it let me do testing of my redirects but it was definitely less than ideal.

So of course I went over to the forum and posted a question about this.

And since I posted a question, I, of course, discovered how to do it about a minute later.

Turns out the CLI supports a way to bypass the normal startup command that Dev uses. It's as simple as passing -c and the command you need. This is what I use for my blog:

netlify dev -c "bundle exec jekyll serve --config _config.dev.yml"

I modified my start.sh to use that and I'm good to go. Running both Jekyll locally and Netlify Dev.

Console output showing the netlify dev version running

Header photo by Sam Truong Dan on Unsplash

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by remotesynth posted on 7/15/2019 at 6:12 PM

Good tip. I discovered this as well because my Certified Fresh Events side needs to publish future posts, which requires the --buildFuture flag when running. However, you mention modifying start.sh. Not to be too ignorant, but can you explain? Will Netlify Dev use that to override the usual command?

Comment 2 (In reply to #1) by Raymond Camden posted on 7/15/2019 at 6:19 PM

My understanding is that netlify dev "sniffs" your environment to figure out what you use. In my case, Jekyll, which has a standard way of starting. start.sh is not a Jekyll or Netllify thing, it's just a script I wrote for my own purposes. So what my tip does is to tell Netlify Dev to *not* to try to start my server but to run my specific command instead.

Comment 3 (In reply to #2) by Raymond Camden posted on 7/15/2019 at 6:19 PM

And to add more context, I updated my start.sh to run netllify dev with the -c and the right values.

Comment 4 (In reply to #3) by remotesynth posted on 7/15/2019 at 6:28 PM

Ah, yes, I see that now in the screenshot. You are not running netlify dev directly but just start.sh.

Comment 5 (In reply to #4) by Raymond Camden posted on 7/15/2019 at 6:30 PM

Yeah sorry if not clear. Normal routine was to run start.sh which just ran jekyll and said, "use this config instead". That's it. Then I tried netlify dev, which worked, but was slow. Then I found the CLI argument, and it worked great, so I modified start.sh to use netlify dev w/ the 'better' config.