From 7d8011ed63d587b87a7c182748914fe146590093 Mon Sep 17 00:00:00 2001 From: SatowTakeshi Date: Sun, 18 Apr 2021 16:13:00 +0900 Subject: Allow rendering static files to disk and dynamic to memory in server mode Updates #9625 --- commands/commandeer.go | 14 +++++++++++++- commands/hugo.go | 3 +++ commands/server.go | 23 ++++++++++++++--------- commands/static_syncer.go | 9 ++++++++- 4 files changed, 38 insertions(+), 11 deletions(-) (limited to 'commands') diff --git a/commands/commandeer.go b/commands/commandeer.go index bf42501e0..07181f244 100644 --- a/commands/commandeer.go +++ b/commands/commandeer.go @@ -92,6 +92,7 @@ type commandeer struct { languagesConfigured bool languages langs.Languages doLiveReload bool + renderStaticToDisk bool fastRenderMode bool showErrorInBrowser bool wasError bool @@ -368,8 +369,9 @@ func (c *commandeer) loadConfig() error { } createMemFs := config.GetBool("renderToMemory") + c.renderStaticToDisk = config.GetBool("renderStaticToDisk") - if createMemFs { + if createMemFs && !c.renderStaticToDisk { // Rendering to memoryFS, publish to Root regardless of publishDir. config.Set("publishDir", "/") } @@ -380,6 +382,14 @@ func (c *commandeer) loadConfig() error { if c.destinationFs != nil { // Need to reuse the destination on server rebuilds. fs.Destination = c.destinationFs + } else if createMemFs && c.renderStaticToDisk { + // Writes the dynamic output on memory, + // while serve others directly from publishDir + publishDir := config.GetString("publishDir") + writableFs := afero.NewBasePathFs(afero.NewMemMapFs(), publishDir) + publicFs := afero.NewOsFs() + fs.Destination = afero.NewCopyOnWriteFs(afero.NewReadOnlyFs(publicFs), writableFs) + fs.DestinationStatic = publicFs } else if createMemFs { // Hugo writes the output to memory instead of the disk. fs.Destination = new(afero.MemMapFs) @@ -397,11 +407,13 @@ func (c *commandeer) loadConfig() error { changeDetector.PrepareNew() fs.Destination = hugofs.NewHashingFs(fs.Destination, changeDetector) + fs.DestinationStatic = hugofs.NewHashingFs(fs.DestinationStatic, changeDetector) c.changeDetector = changeDetector } if c.Cfg.GetBool("logPathWarnings") { fs.Destination = hugofs.NewCreateCountingFs(fs.Destination) + fs.DestinationStatic = hugofs.NewCreateCountingFs(fs.DestinationStatic) } // To debug hard-to-find path issues. diff --git a/commands/hugo.go b/commands/hugo.go index 8c5294f00..21140fa43 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -652,6 +652,9 @@ func (c *commandeer) copyStaticTo(sourceFs *filesystems.SourceFilesystem) (uint6 syncer.ChmodFilter = chmodFilter syncer.SrcFs = fs syncer.DestFs = c.Fs.Destination + if c.renderStaticToDisk { + syncer.DestFs = c.Fs.DestinationStatic + } // Now that we are using a unionFs for the static directories // We can effectively clean the publishDir on initial sync syncer.Delete = c.Cfg.GetBool("cleanDestinationDir") diff --git a/commands/server.go b/commands/server.go index 7d9462b36..3f5290816 100644 --- a/commands/server.go +++ b/commands/server.go @@ -48,15 +48,16 @@ type serverCmd struct { // Can be used to stop the server. Useful in tests stop <-chan bool - disableLiveReload bool - navigateToChanged bool - renderToDisk bool - serverAppend bool - serverInterface string - serverPort int - liveReloadPort int - serverWatch bool - noHTTPCache bool + disableLiveReload bool + navigateToChanged bool + renderToDisk bool + renderStaticToDisk bool + serverAppend bool + serverInterface string + serverPort int + liveReloadPort int + serverWatch bool + noHTTPCache bool disableFastRender bool disableBrowserError bool @@ -101,6 +102,7 @@ of a second, you will be able to save and see your changes nearly instantly.`, cc.cmd.Flags().BoolVar(&cc.renderToDisk, "renderToDisk", false, "render to Destination path (default is render to memory & serve from there)") cc.cmd.Flags().BoolVar(&cc.disableFastRender, "disableFastRender", false, "enables full re-renders on changes") cc.cmd.Flags().BoolVar(&cc.disableBrowserError, "disableBrowserError", false, "do not show build errors in the browser") + cc.cmd.Flags().BoolVar(&cc.renderStaticToDisk, "renderStaticToDisk", false, "render static files to disk but dynamic files render to memory.") cc.cmd.Flags().String("memstats", "", "log memory usage to this file") cc.cmd.Flags().String("meminterval", "100ms", "interval to poll memory usage (requires --memstats), valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\".") @@ -141,6 +143,7 @@ func (sc *serverCmd) server(cmd *cobra.Command, args []string) error { cfgInit := func(c *commandeer) error { c.Set("renderToMemory", !sc.renderToDisk) + c.Set("renderStaticToDisk", sc.renderStaticToDisk) if cmd.Flags().Changed("navigateToChanged") { c.Set("navigateToChanged", sc.navigateToChanged) } @@ -332,6 +335,8 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, string, string, erro if i == 0 { if f.s.renderToDisk { jww.FEEDBACK.Println("Serving pages from " + absPublishDir) + } else if f.s.renderStaticToDisk { + jww.FEEDBACK.Println("Serving pages from memory and static files from " + absPublishDir) } else { jww.FEEDBACK.Println("Serving pages from memory") } diff --git a/commands/static_syncer.go b/commands/static_syncer.go index 5569d4de6..2eb2b6662 100644 --- a/commands/static_syncer.go +++ b/commands/static_syncer.go @@ -56,6 +56,9 @@ func (s *staticSyncer) syncsStaticEvents(staticEvents []fsnotify.Event) error { syncer.ChmodFilter = chmodFilter syncer.SrcFs = sourceFs.Fs syncer.DestFs = c.Fs.Destination + if c.renderStaticToDisk { + syncer.DestFs = c.Fs.DestinationStatic + } // prevent spamming the log on changes logger := helpers.NewDistinctErrorLogger() @@ -101,7 +104,11 @@ func (s *staticSyncer) syncsStaticEvents(staticEvents []fsnotify.Event) error { toRemove := filepath.Join(publishDir, relPath) logger.Println("File no longer exists in static dir, removing", toRemove) - _ = c.Fs.Destination.RemoveAll(toRemove) + if c.renderStaticToDisk { + _ = c.Fs.DestinationStatic.RemoveAll(toRemove) + } else { + _ = c.Fs.Destination.RemoveAll(toRemove) + } } else if err == nil { // If file still exists, sync it logger.Println("Syncing", relPath, "to", publishDir) -- cgit v1.2.3