shithub: hugo

Download patch

ref: df824fa31b4a760bc43d2a2556e5cf134884d1a3
parent: e1618e9d2f609ca6983aa2d3907bd4b1bcef5da4
author: spf13 <steve.francia@gmail.com>
date: Mon Nov 16 16:55:18 EST 2015

Hugo server renders and serves from memory by default (30%+ improvement)

--- a/commands/server.go
+++ b/commands/server.go
@@ -37,17 +37,22 @@
 var serverWatch bool
 var serverAppend bool
 var disableLiveReload bool
+var renderToDisk bool
 
 //var serverCmdV *cobra.Command
 
 var serverCmd = &cobra.Command{
-	Use:   "server",
-	Short: "A high performance webserver",
-	Long: `Hugo provides its own webserver which builds and serves the site.
+	Use:     "server",
+	Aliases: []string{"serve"},
+	Short:   "A high performance webserver",
+	Long: `Hugo provides it's own webserver which builds and serves the site.
 While hugo server is high performance, it is a webserver with limited options.
 Many run it in production, but the standard behavior is for people to use it in development
 and use a more full featured server such as Nginx or Caddy.
 
+'hugo server' will avoid writing the rendered and served content to disk,
+preferring to store it in memory.
+
 Often server is paired with '--watch' which Hugo will look for changes to the source and
 continously rebuild and serve the website.`,
 	//Run: server,
@@ -79,6 +84,7 @@
 	serverCmd.Flags().BoolVarP(&serverWatch, "watch", "w", false, "watch filesystem for changes and recreate as needed")
 	serverCmd.Flags().BoolVarP(&serverAppend, "appendPort", "", true, "append port to baseurl")
 	serverCmd.Flags().BoolVar(&disableLiveReload, "disableLiveReload", false, "watch without enabling live browser reload on rebuild")
+	serverCmd.Flags().BoolVar(&renderToDisk, "renderToDisk", false, "render to Destination path (default is render to memory & serve from there)")
 	serverCmd.Flags().BoolVarP(&NoTimes, "noTimes", "", false, "Don't sync modification time of files")
 	serverCmd.Flags().String("memstats", "", "log memory usage to this file")
 	serverCmd.Flags().Int("meminterval", 100, "interval to poll memory usage (requires --memstats)")
@@ -126,6 +132,18 @@
 		jww.ERROR.Println("memstats error:", err)
 	}
 
+	// If a Destination is provided via flag write to disk
+	if Destination != "" {
+		renderToDisk = true
+	}
+
+	// Hugo writes the output to memory instead of the disk
+	if !renderToDisk {
+		hugofs.DestinationFS = new(afero.MemMapFs)
+		// Rendering to memoryFS, publish to Root regardless of publishDir.
+		viper.Set("PublishDir", "/")
+	}
+
 	build(serverWatch)
 
 	// Watch runs its own server as part of the routine
@@ -148,7 +166,11 @@
 }
 
 func serve(port int) {
-	jww.FEEDBACK.Println("Serving pages from " + helpers.AbsPathify(viper.GetString("PublishDir")))
+	if renderToDisk {
+		jww.FEEDBACK.Println("Serving pages from " + helpers.AbsPathify(viper.GetString("PublishDir")))
+	} else {
+		jww.FEEDBACK.Println("Serving pages from memory")
+	}
 
 	httpFs := &afero.HttpFs{SourceFs: hugofs.DestinationFS}
 	fs := filesOnlyFs{httpFs.Dir(helpers.AbsPathify(viper.GetString("PublishDir")))}
--- a/docs/content/meta/release-notes.md
+++ b/docs/content/meta/release-notes.md
@@ -11,6 +11,7 @@
 ---
 ## **0.15.0** ???
 
+* `hugo server` now builds ~30%+ faster by rendering to memory instead of disk
 * Have Jekyll site, but dreaming of porting it to Hugo? This release introduces a new `hugo import jekyll`command that makes this easier than ever. [1469](https://github.com/spf13/hugo/pull/1469)
 * We now use a custom-built `LazyFileReader` for reading file contents, which means we don't read media files in `/content` into memory anymore -- and file reading is now performed in parallel on multicore PCs. [1181](https://github.com/spf13/hugo/issues/1181)
 * Hugo is now built with `Go 1.5` which, among many other improvements, have fixed the last known data race in Hugo. [917] (https://github.com/spf13/hugo/issues/917)
--- a/hugofs/fs.go
+++ b/hugofs/fs.go
@@ -18,5 +18,3 @@
 var SourceFs afero.Fs = new(afero.OsFs)
 var DestinationFS afero.Fs = new(afero.OsFs)
 var OsFs afero.Fs = new(afero.OsFs)
-
-//var DestinationFS afero.Fs = new(afero.MemMapFs)
--