2019-01-02 06:33:26 -05:00
// Copyright 2019 The Hugo Authors. All rights reserved.
2013-09-29 02:09:03 -04:00
//
2015-11-23 22:16:36 -05:00
// Licensed under the Apache License, Version 2.0 (the "License");
2013-09-29 02:09:03 -04:00
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2015-11-23 22:16:36 -05:00
// http://www.apache.org/licenses/LICENSE-2.0
2013-09-29 02:09:03 -04:00
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package commands
import (
2018-10-03 08:58:09 -04:00
"bytes"
2022-03-14 11:34:23 -04:00
"context"
2013-09-29 02:09:03 -04:00
"fmt"
2019-12-10 13:56:44 -05:00
"io"
2014-05-15 15:07:46 -04:00
"net"
2013-09-29 02:09:03 -04:00
"net/http"
2014-08-22 07:59:59 -04:00
"net/url"
2014-01-26 04:48:00 -05:00
"os"
2018-01-14 14:58:52 -05:00
"os/signal"
2022-03-21 04:35:15 -04:00
"path"
2017-11-02 03:25:20 -04:00
"path/filepath"
2018-10-03 08:58:09 -04:00
"regexp"
2014-09-22 09:45:05 -04:00
"runtime"
2013-09-29 02:09:03 -04:00
"strconv"
2013-11-22 00:28:05 -05:00
"strings"
2018-03-18 06:07:24 -04:00
"sync"
2018-01-14 14:58:52 -05:00
"syscall"
2014-09-22 09:45:05 -04:00
"time"
2014-03-31 13:23:34 -04:00
2022-04-26 13:57:04 -04:00
"github.com/gohugoio/hugo/common/htime"
2021-06-18 04:27:27 -04:00
"github.com/gohugoio/hugo/common/paths"
2022-05-16 03:22:51 -04:00
"github.com/gohugoio/hugo/hugolib"
"github.com/gohugoio/hugo/tpl"
2022-03-14 11:34:23 -04:00
"golang.org/x/sync/errgroup"
2021-06-18 04:27:27 -04:00
2017-11-12 04:03:56 -05:00
"github.com/gohugoio/hugo/livereload"
2017-06-13 12:42:45 -04:00
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/helpers"
2017-06-13 13:07:35 -04:00
"github.com/spf13/afero"
"github.com/spf13/cobra"
2014-03-31 13:23:34 -04:00
jww "github.com/spf13/jwalterweatherman"
2013-09-29 02:09:03 -04:00
)
2018-04-09 16:28:03 -04:00
type serverCmd struct {
2018-04-11 03:38:58 -04:00
// Can be used to stop the server. Useful in tests
2022-03-18 03:54:44 -04:00
stop chan bool
2018-04-11 03:38:58 -04:00
2022-03-14 11:02:04 -04:00
disableLiveReload bool
navigateToChanged bool
renderToDisk bool
renderStaticToDisk bool
serverAppend bool
serverInterface string
serverPort int
liveReloadPort int
serverWatch bool
noHTTPCache bool
Only re-render the view(s) you're working on
Hugo already, in its server mode, support partial rebuilds. To put it simply: If you change `about.md`, only that content page is read and processed, then Hugo does some processing (taxonomies etc.) and the full site is rendered.
This commit covers the rendering part: We now only re-render the pages you work on, i.e. the last n pages you watched in the browser (which obviously also includes the page in the example above).
To be more specific: When you are running the hugo server in watch (aka. livereload) mode, and change a template or a content file, then we do a partial re-rendering of the following:
* The current content page (if it is a content change)
* The home page
* Up to the last 10 pages you visited on the site.
This should in most cases be enough, but if you navigate to something completely different, you may see stale content. Doing an edit will then refresh that page.
Note that this feature is enabled by default. To turn it off, run `hugo server --disableFastRender`.
Fixes #3962
See #1643
2017-10-14 07:40:43 -04:00
2018-10-03 08:58:09 -04:00
disableFastRender bool
disableBrowserError bool
2013-09-29 02:09:03 -04:00
2018-04-11 02:39:39 -04:00
* baseBuilderCmd
2018-04-09 16:28:03 -04:00
}
2018-04-13 02:42:29 -04:00
func ( b * commandsBuilder ) newServerCmd ( ) * serverCmd {
return b . newServerCmdSignaled ( nil )
2018-04-11 03:38:58 -04:00
}
2022-03-18 03:54:44 -04:00
func ( b * commandsBuilder ) newServerCmdSignaled ( stop chan bool ) * serverCmd {
2018-04-12 03:31:53 -04:00
cc := & serverCmd { stop : stop }
2018-04-09 16:28:03 -04:00
2018-04-13 02:42:29 -04:00
cc . baseBuilderCmd = b . newBuilderCmd ( & cobra . Command {
2018-04-09 16:28:03 -04:00
Use : "server" ,
Aliases : [ ] string { "serve" } ,
Short : "A high performance webserver" ,
Long : ` Hugo provides its own webserver which builds and serves the site .
2015-11-12 10:23:41 -05:00
While hugo server is high performance , it is a webserver with limited options .
2015-11-21 08:31:10 -05:00
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 .
2015-11-12 10:23:41 -05:00
2015-11-16 21:55:18 -05:00
' hugo server ' will avoid writing the rendered and served content to disk ,
preferring to store it in memory .
2015-11-20 10:13:03 -05:00
By default hugo will also watch your files for any changes you make and
automatically rebuild the site . It will then live reload any open browser pages
2015-11-21 08:31:10 -05:00
and push the latest content to them . As most Hugo sites are built in a fraction
of a second , you will be able to save and see your changes nearly instantly . ` ,
2022-03-18 03:54:44 -04:00
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
err := cc . server ( cmd , args )
if err != nil && cc . stop != nil {
cc . stop <- true
}
return err
} ,
2018-04-10 03:19:26 -04:00
} )
2018-04-09 16:28:03 -04:00
2018-04-10 13:16:09 -04:00
cc . cmd . Flags ( ) . IntVarP ( & cc . serverPort , "port" , "p" , 1313 , "port on which the server will listen" )
cc . cmd . Flags ( ) . IntVar ( & cc . liveReloadPort , "liveReloadPort" , - 1 , "port for live reloading (i.e. 443 in HTTPS proxy situations)" )
cc . cmd . Flags ( ) . StringVarP ( & cc . serverInterface , "bind" , "" , "127.0.0.1" , "interface to which the server will bind" )
cc . cmd . Flags ( ) . BoolVarP ( & cc . serverWatch , "watch" , "w" , true , "watch filesystem for changes and recreate as needed" )
cc . cmd . Flags ( ) . BoolVar ( & cc . noHTTPCache , "noHTTPCache" , false , "prevent HTTP caching" )
cc . cmd . Flags ( ) . BoolVarP ( & cc . serverAppend , "appendPort" , "" , true , "append port to baseURL" )
cc . cmd . Flags ( ) . BoolVar ( & cc . disableLiveReload , "disableLiveReload" , false , "watch without enabling live browser reload on rebuild" )
cc . cmd . Flags ( ) . BoolVar ( & cc . navigateToChanged , "navigateToChanged" , false , "navigate to changed content file on live browser reload" )
2022-03-14 11:02:06 -04:00
cc . cmd . Flags ( ) . BoolVar ( & cc . renderToDisk , "renderToDisk" , false , "serve all files from disk (default is from memory)" )
cc . cmd . Flags ( ) . BoolVar ( & cc . renderStaticToDisk , "renderStaticToDisk" , false , "serve static files from disk and dynamic files from memory" )
2018-04-10 13:16:09 -04:00
cc . cmd . Flags ( ) . BoolVar ( & cc . disableFastRender , "disableFastRender" , false , "enables full re-renders on changes" )
2018-10-03 08:58:09 -04:00
cc . cmd . Flags ( ) . BoolVar ( & cc . disableBrowserError , "disableBrowserError" , false , "do not show build errors in the browser" )
2018-04-10 13:16:09 -04:00
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\"." )
2018-04-09 16:28:03 -04:00
return cc
2014-05-16 17:49:27 -04:00
}
2015-10-23 12:21:37 -04:00
type filesOnlyFs struct {
fs http . FileSystem
}
type noDirFile struct {
http . File
}
func ( fs filesOnlyFs ) Open ( name string ) ( http . File , error ) {
f , err := fs . fs . Open ( name )
if err != nil {
return nil , err
}
return noDirFile { f } , nil
}
func ( f noDirFile ) Readdir ( count int ) ( [ ] os . FileInfo , error ) {
return nil , nil
}
2018-09-06 11:53:18 -04:00
func ( sc * serverCmd ) server ( cmd * cobra . Command , args [ ] string ) error {
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
// If a Destination is provided via flag write to disk
2018-04-09 11:20:18 -04:00
destination , _ := cmd . Flags ( ) . GetString ( "destination" )
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
if destination != "" {
2018-09-06 11:53:18 -04:00
sc . renderToDisk = true
2017-06-26 15:34:16 -04:00
}
2018-03-18 06:07:24 -04:00
var serverCfgInit sync . Once
2022-03-14 11:34:23 -04:00
cfgInit := func ( c * commandeer ) ( rerr error ) {
2022-03-21 04:35:15 -04:00
c . Set ( "renderToMemory" , ! ( sc . renderToDisk || sc . renderStaticToDisk ) )
2022-03-14 11:02:04 -04:00
c . Set ( "renderStaticToDisk" , sc . renderStaticToDisk )
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
if cmd . Flags ( ) . Changed ( "navigateToChanged" ) {
2018-09-06 11:53:18 -04:00
c . Set ( "navigateToChanged" , sc . navigateToChanged )
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
}
if cmd . Flags ( ) . Changed ( "disableLiveReload" ) {
2018-09-06 11:53:18 -04:00
c . Set ( "disableLiveReload" , sc . disableLiveReload )
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
}
if cmd . Flags ( ) . Changed ( "disableFastRender" ) {
2018-09-06 11:53:18 -04:00
c . Set ( "disableFastRender" , sc . disableFastRender )
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
}
2018-10-03 08:58:09 -04:00
if cmd . Flags ( ) . Changed ( "disableBrowserError" ) {
c . Set ( "disableBrowserError" , sc . disableBrowserError )
}
2018-09-06 11:53:18 -04:00
if sc . serverWatch {
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
c . Set ( "watch" , true )
}
Only re-render the view(s) you're working on
Hugo already, in its server mode, support partial rebuilds. To put it simply: If you change `about.md`, only that content page is read and processed, then Hugo does some processing (taxonomies etc.) and the full site is rendered.
This commit covers the rendering part: We now only re-render the pages you work on, i.e. the last n pages you watched in the browser (which obviously also includes the page in the example above).
To be more specific: When you are running the hugo server in watch (aka. livereload) mode, and change a template or a content file, then we do a partial re-rendering of the following:
* The current content page (if it is a content change)
* The home page
* Up to the last 10 pages you visited on the site.
This should in most cases be enough, but if you navigate to something completely different, you may see stale content. Doing an edit will then refresh that page.
Note that this feature is enabled by default. To turn it off, run `hugo server --disableFastRender`.
Fixes #3962
See #1643
2017-10-14 07:40:43 -04:00
2022-05-18 03:47:55 -04:00
// TODO(bep) see issue 9901
// cfgInit is called twice, before and after the languages have been initialized.
// The servers (below) can not be initialized before we
// know if we're configured in a multihost setup.
if len ( c . languages ) == 0 {
return nil
}
2018-03-18 06:07:24 -04:00
// We can only do this once.
serverCfgInit . Do ( func ( ) {
2022-03-18 03:54:44 -04:00
c . serverPorts = make ( [ ] serverPortListener , 1 )
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
2018-03-18 06:07:24 -04:00
if c . languages . IsMultihost ( ) {
2018-09-06 11:53:18 -04:00
if ! sc . serverAppend {
2022-03-14 11:34:23 -04:00
rerr = newSystemError ( "--appendPort=false not supported when in multihost mode" )
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
}
2022-03-18 03:54:44 -04:00
c . serverPorts = make ( [ ] serverPortListener , len ( c . languages ) )
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
}
2017-11-02 03:25:20 -04:00
2018-09-06 11:53:18 -04:00
currentServerPort := sc . serverPort
2018-03-18 06:07:24 -04:00
2022-03-18 03:54:44 -04:00
for i := 0 ; i < len ( c . serverPorts ) ; i ++ {
2018-09-06 11:53:18 -04:00
l , err := net . Listen ( "tcp" , net . JoinHostPort ( sc . serverInterface , strconv . Itoa ( currentServerPort ) ) )
2018-03-18 06:07:24 -04:00
if err == nil {
2022-03-18 03:54:44 -04:00
c . serverPorts [ i ] = serverPortListener { ln : l , p : currentServerPort }
2018-03-18 06:07:24 -04:00
} else {
2018-09-06 11:53:18 -04:00
if i == 0 && sc . cmd . Flags ( ) . Changed ( "port" ) {
2018-03-18 06:07:24 -04:00
// port set explicitly by user -- he/she probably meant it!
2022-03-14 11:34:23 -04:00
rerr = newSystemErrorF ( "Server startup failed: %s" , err )
return
2018-03-18 06:07:24 -04:00
}
2020-10-21 05:17:48 -04:00
c . logger . Println ( "port" , sc . serverPort , "already in use, attempting to use an available port" )
2022-03-18 03:54:44 -04:00
l , sp , err := helpers . TCPListen ( )
2018-03-18 06:07:24 -04:00
if err != nil {
2022-03-14 11:34:23 -04:00
rerr = newSystemError ( "Unable to find alternative port to use:" , err )
return
2018-03-18 06:07:24 -04:00
}
2022-03-18 03:54:44 -04:00
c . serverPorts [ i ] = serverPortListener { ln : l , p : sp . Port }
2018-03-18 06:07:24 -04:00
}
2022-03-18 03:54:44 -04:00
currentServerPort = c . serverPorts [ i ] . p + 1
2018-03-18 06:07:24 -04:00
}
} )
2017-11-02 03:25:20 -04:00
2022-03-14 11:34:23 -04:00
if rerr != nil {
return
}
2018-09-06 11:53:18 -04:00
c . Set ( "port" , sc . serverPort )
if sc . liveReloadPort != - 1 {
c . Set ( "liveReloadPort" , sc . liveReloadPort )
2017-11-02 03:25:20 -04:00
} else {
2022-03-18 03:54:44 -04:00
c . Set ( "liveReloadPort" , c . serverPorts [ 0 ] . p )
2014-05-15 15:07:46 -04:00
}
2017-11-02 03:25:20 -04:00
2018-01-25 11:03:29 -05:00
isMultiHost := c . languages . IsMultihost ( )
for i , language := range c . languages {
var serverPort int
if isMultiHost {
2022-03-18 03:54:44 -04:00
serverPort = c . serverPorts [ i ] . p
2018-01-25 11:03:29 -05:00
} else {
2022-03-18 03:54:44 -04:00
serverPort = c . serverPorts [ 0 ] . p
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
}
2018-01-25 11:03:29 -05:00
2018-09-06 11:53:18 -04:00
baseURL , err := sc . fixURL ( language , sc . baseURL , serverPort )
2017-11-02 03:25:20 -04:00
if err != nil {
2018-03-18 06:07:24 -04:00
return nil
2017-11-02 03:25:20 -04:00
}
2018-01-27 04:58:30 -05:00
if isMultiHost {
language . Set ( "baseURL" , baseURL )
}
2018-01-25 11:03:29 -05:00
if i == 0 {
c . Set ( "baseURL" , baseURL )
}
2017-11-02 03:25:20 -04:00
}
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
2022-03-14 11:34:23 -04:00
return
2014-01-29 17:50:31 -05:00
}
2013-11-22 00:28:05 -05:00
2014-09-22 09:45:05 -04:00
if err := memStats ( ) ; err != nil {
2018-10-03 08:58:09 -04:00
jww . WARN . Println ( "memstats error:" , err )
2014-09-22 09:45:05 -04:00
}
2021-07-05 04:38:54 -04:00
// silence errors in cobra so we can handle them here
cmd . SilenceErrors = true
2021-08-31 11:02:51 -04:00
c , err := initializeConfig ( true , true , true , & sc . hugoBuilderCommon , sc , cfgInit )
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
if err != nil {
2021-07-05 04:38:54 -04:00
cmd . PrintErrln ( "Error:" , err . Error ( ) )
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
return err
2015-11-16 21:55:18 -05:00
}
2021-07-05 04:38:54 -04:00
err = func ( ) error {
2022-05-09 04:05:19 -04:00
defer c . timeTrack ( time . Now ( ) , "Built" )
2021-07-05 04:38:54 -04:00
err := c . serverBuild ( )
if err != nil {
cmd . PrintErrln ( "Error:" , err . Error ( ) )
}
return err
} ( )
if err != nil {
2015-12-02 05:42:53 -05:00
return err
}
2013-10-09 18:24:40 -04:00
2013-09-29 02:09:03 -04:00
// Watch runs its own server as part of the routine
2018-09-06 11:53:18 -04:00
if sc . serverWatch {
2017-11-12 04:03:56 -05:00
watchDirs , err := c . getDirList ( )
if err != nil {
return err
}
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
watchGroups := helpers . ExtractAndGroupRootPaths ( watchDirs )
2015-11-23 10:32:06 -05:00
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
for _ , group := range watchGroups {
jww . FEEDBACK . Printf ( "Watching for changes in %s\n" , group )
}
2021-07-02 03:54:03 -04:00
watcher , err := c . newWatcher ( sc . poll , watchDirs ... )
2013-09-29 02:09:03 -04:00
if err != nil {
2015-12-02 05:42:53 -05:00
return err
2013-09-29 02:09:03 -04:00
}
2018-01-14 14:58:52 -05:00
2018-01-15 04:02:17 -05:00
defer watcher . Close ( )
2013-09-29 02:09:03 -04:00
}
2021-08-21 10:32:20 -04:00
return c . serve ( sc )
2013-09-29 02:09:03 -04:00
}
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
func getRootWatchDirsStr ( baseDir string , watchDirs [ ] string ) string {
relWatchDirs := make ( [ ] string , len ( watchDirs ) )
for i , dir := range watchDirs {
2021-06-18 04:27:27 -04:00
relWatchDirs [ i ] , _ = paths . GetRelativePath ( dir , baseDir )
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
}
return strings . Join ( helpers . UniqueStringsSorted ( helpers . ExtractRootPaths ( relWatchDirs ) ) , "," )
}
2017-11-02 03:25:20 -04:00
type fileServer struct {
2018-10-03 08:58:09 -04:00
baseURLs [ ] string
roots [ ] string
2022-03-17 17:03:27 -04:00
errorTemplate func ( err any ) ( io . Reader , error )
2018-10-03 08:58:09 -04:00
c * commandeer
s * serverCmd
2017-11-02 03:25:20 -04:00
}
2020-05-27 07:50:13 -04:00
func ( f * fileServer ) rewriteRequest ( r * http . Request , toPath string ) * http . Request {
r2 := new ( http . Request )
* r2 = * r
r2 . URL = new ( url . URL )
* r2 . URL = * r . URL
r2 . URL . Path = toPath
r2 . Header . Set ( "X-Rewrite-Original-URI" , r . URL . RequestURI ( ) )
return r2
}
2022-03-18 03:54:44 -04:00
func ( f * fileServer ) createEndpoint ( i int ) ( * http . ServeMux , net . Listener , string , string , error ) {
2017-11-02 03:25:20 -04:00
baseURL := f . baseURLs [ i ]
root := f . roots [ i ]
2022-03-18 03:54:44 -04:00
port := f . c . serverPorts [ i ] . p
listener := f . c . serverPorts [ i ] . ln
2017-11-02 03:25:20 -04:00
2022-03-21 04:35:15 -04:00
// For logging only.
// TODO(bep) consolidate.
2017-11-02 03:25:20 -04:00
publishDir := f . c . Cfg . GetString ( "publishDir" )
2022-03-21 04:35:15 -04:00
publishDirStatic := f . c . Cfg . GetString ( "publishDirStatic" )
workingDir := f . c . Cfg . GetString ( "workingDir" )
2017-11-02 03:25:20 -04:00
if root != "" {
publishDir = filepath . Join ( publishDir , root )
2022-03-21 04:35:15 -04:00
publishDirStatic = filepath . Join ( publishDirStatic , root )
2017-11-02 03:25:20 -04:00
}
2022-03-21 04:35:15 -04:00
absPublishDir := paths . AbsPathify ( workingDir , publishDir )
absPublishDirStatic := paths . AbsPathify ( workingDir , publishDirStatic )
2017-11-02 03:25:20 -04:00
2019-08-15 03:33:47 -04:00
jww . FEEDBACK . Printf ( "Environment: %q" , f . c . hugo ( ) . Deps . Site . Hugo ( ) . Environment )
2018-11-15 03:28:02 -05:00
2017-11-12 04:03:56 -05:00
if i == 0 {
2018-04-09 16:28:03 -04:00
if f . s . renderToDisk {
2017-11-12 04:03:56 -05:00
jww . FEEDBACK . Println ( "Serving pages from " + absPublishDir )
2022-03-14 11:02:04 -04:00
} else if f . s . renderStaticToDisk {
2022-03-21 04:35:15 -04:00
jww . FEEDBACK . Println ( "Serving pages from memory and static files from " + absPublishDirStatic )
2017-11-12 04:03:56 -05:00
} else {
jww . FEEDBACK . Println ( "Serving pages from memory" )
}
2015-11-16 21:55:18 -05:00
}
2014-01-26 04:48:00 -05:00
2022-03-21 04:35:15 -04:00
httpFs := afero . NewHttpFs ( f . c . publishDirServerFs )
fs := filesOnlyFs { httpFs . Dir ( path . Join ( "/" , root ) ) }
Only re-render the view(s) you're working on
Hugo already, in its server mode, support partial rebuilds. To put it simply: If you change `about.md`, only that content page is read and processed, then Hugo does some processing (taxonomies etc.) and the full site is rendered.
This commit covers the rendering part: We now only re-render the pages you work on, i.e. the last n pages you watched in the browser (which obviously also includes the page in the example above).
To be more specific: When you are running the hugo server in watch (aka. livereload) mode, and change a template or a content file, then we do a partial re-rendering of the following:
* The current content page (if it is a content change)
* The home page
* Up to the last 10 pages you visited on the site.
This should in most cases be enough, but if you navigate to something completely different, you may see stale content. Doing an edit will then refresh that page.
Note that this feature is enabled by default. To turn it off, run `hugo server --disableFastRender`.
Fixes #3962
See #1643
2017-10-14 07:40:43 -04:00
2018-10-03 08:58:09 -04:00
if i == 0 && f . c . fastRenderMode {
Only re-render the view(s) you're working on
Hugo already, in its server mode, support partial rebuilds. To put it simply: If you change `about.md`, only that content page is read and processed, then Hugo does some processing (taxonomies etc.) and the full site is rendered.
This commit covers the rendering part: We now only re-render the pages you work on, i.e. the last n pages you watched in the browser (which obviously also includes the page in the example above).
To be more specific: When you are running the hugo server in watch (aka. livereload) mode, and change a template or a content file, then we do a partial re-rendering of the following:
* The current content page (if it is a content change)
* The home page
* Up to the last 10 pages you visited on the site.
This should in most cases be enough, but if you navigate to something completely different, you may see stale content. Doing an edit will then refresh that page.
Note that this feature is enabled by default. To turn it off, run `hugo server --disableFastRender`.
Fixes #3962
See #1643
2017-10-14 07:40:43 -04:00
jww . FEEDBACK . Println ( "Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender" )
}
2017-11-02 03:25:20 -04:00
// We're only interested in the path
u , err := url . Parse ( baseURL )
if err != nil {
2022-05-02 10:07:52 -04:00
return nil , nil , "" , "" , fmt . Errorf ( "Invalid baseURL: %w" , err )
2017-11-02 03:25:20 -04:00
}
2017-09-22 11:13:21 -04:00
decorate := func ( h http . Handler ) http . Handler {
return http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) {
2018-10-03 08:58:09 -04:00
if f . c . showErrorInBrowser {
// First check the error state
err := f . c . getErrorWithContext ( )
if err != nil {
2019-12-20 02:11:36 -05:00
f . c . wasError = true
2018-10-03 08:58:09 -04:00
w . WriteHeader ( 500 )
2019-12-10 13:56:44 -05:00
r , err := f . errorTemplate ( err )
2018-10-03 08:58:09 -04:00
if err != nil {
2020-10-21 05:17:48 -04:00
f . c . logger . Errorln ( err )
2018-10-03 08:58:09 -04:00
}
2020-03-10 13:12:11 -04:00
2018-10-22 13:50:27 -04:00
port = 1313
if ! f . c . paused {
port = f . c . Cfg . GetInt ( "liveReloadPort" )
}
2020-12-02 06:52:26 -05:00
lr := * u
lr . Host = fmt . Sprintf ( "%s:%d" , lr . Hostname ( ) , port )
fmt . Fprint ( w , injectLiveReloadScript ( r , lr ) )
2018-10-03 08:58:09 -04:00
return
}
}
2018-04-09 16:28:03 -04:00
if f . s . noHTTPCache {
2017-09-22 14:05:19 -04:00
w . Header ( ) . Set ( "Cache-Control" , "no-store, no-cache, must-revalidate, max-age=0" )
2017-09-22 11:13:21 -04:00
w . Header ( ) . Set ( "Pragma" , "no-cache" )
}
Only re-render the view(s) you're working on
Hugo already, in its server mode, support partial rebuilds. To put it simply: If you change `about.md`, only that content page is read and processed, then Hugo does some processing (taxonomies etc.) and the full site is rendered.
This commit covers the rendering part: We now only re-render the pages you work on, i.e. the last n pages you watched in the browser (which obviously also includes the page in the example above).
To be more specific: When you are running the hugo server in watch (aka. livereload) mode, and change a template or a content file, then we do a partial re-rendering of the following:
* The current content page (if it is a content change)
* The home page
* Up to the last 10 pages you visited on the site.
This should in most cases be enough, but if you navigate to something completely different, you may see stale content. Doing an edit will then refresh that page.
Note that this feature is enabled by default. To turn it off, run `hugo server --disableFastRender`.
Fixes #3962
See #1643
2017-10-14 07:40:43 -04:00
2020-06-05 06:13:26 -04:00
// Ignore any query params for the operations below.
2022-09-17 05:25:37 -04:00
requestURI , _ := url . PathUnescape ( strings . TrimSuffix ( r . RequestURI , "?" + r . URL . RawQuery ) )
2020-06-05 06:13:26 -04:00
for _ , header := range f . c . serverConfig . MatchHeaders ( requestURI ) {
2020-03-08 11:33:15 -04:00
w . Header ( ) . Set ( header . Key , header . Value )
}
2020-06-05 06:13:26 -04:00
if redirect := f . c . serverConfig . MatchRedirect ( requestURI ) ; ! redirect . IsZero ( ) {
2022-09-17 05:25:37 -04:00
// fullName := filepath.Join(dir, filepath.FromSlash(path.Clean("/"+name)))
2020-10-05 11:56:28 -04:00
doRedirect := true
2020-05-27 07:50:13 -04:00
// This matches Netlify's behaviour and is needed for SPA behaviour.
// See https://docs.netlify.com/routing/redirects/rewrites-proxies/
2020-10-05 11:56:28 -04:00
if ! redirect . Force {
path := filepath . Clean ( strings . TrimPrefix ( requestURI , u . Path ) )
2022-09-13 05:33:42 -04:00
if root != "" {
path = filepath . Join ( root , path )
}
fs := f . c . publishDirServerFs
fi , err := fs . Stat ( path )
2020-10-05 11:56:28 -04:00
if err == nil {
if fi . IsDir ( ) {
// There will be overlapping directories, so we
// need to check for a file.
2022-09-13 05:33:42 -04:00
_ , err = fs . Stat ( filepath . Join ( path , "index.html" ) )
2020-10-05 11:56:28 -04:00
doRedirect = err != nil
} else {
doRedirect = false
}
}
}
if doRedirect {
2022-09-13 05:33:42 -04:00
switch redirect . Status {
case 404 :
w . WriteHeader ( 404 )
2022-09-23 03:16:59 -04:00
file , err := fs . Open ( strings . TrimPrefix ( redirect . To , u . Path ) )
2022-09-13 05:33:42 -04:00
if err == nil {
defer file . Close ( )
io . Copy ( w , file )
} else {
fmt . Fprintln ( w , "<h1>Page Not Found</h1>" )
}
return
case 200 :
2020-10-05 11:56:28 -04:00
if r2 := f . rewriteRequest ( r , strings . TrimPrefix ( redirect . To , u . Path ) ) ; r2 != nil {
requestURI = redirect . To
r = r2
}
2022-09-13 05:33:42 -04:00
fallthrough
default :
2020-10-05 11:56:28 -04:00
w . Header ( ) . Set ( "Content-Type" , "" )
http . Redirect ( w , r , redirect . To , redirect . Status )
return
2022-09-13 05:33:42 -04:00
2020-05-27 07:50:13 -04:00
}
}
}
2018-10-17 03:28:04 -04:00
if f . c . fastRenderMode && f . c . buildErr == nil {
2020-06-05 06:13:26 -04:00
if strings . HasSuffix ( requestURI , "/" ) || strings . HasSuffix ( requestURI , "html" ) || strings . HasSuffix ( requestURI , "htm" ) {
if ! f . c . visitedURLs . Contains ( requestURI ) {
2018-10-17 03:28:04 -04:00
// If not already on stack, re-render that single page.
2020-06-05 06:13:26 -04:00
if err := f . c . partialReRender ( requestURI ) ; err != nil {
f . c . handleBuildErr ( err , fmt . Sprintf ( "Failed to render %q" , requestURI ) )
2018-10-17 03:28:04 -04:00
if f . c . showErrorInBrowser {
2020-06-05 06:13:26 -04:00
http . Redirect ( w , r , requestURI , http . StatusMovedPermanently )
2018-10-17 03:28:04 -04:00
return
}
}
}
2020-06-05 06:13:26 -04:00
f . c . visitedURLs . Add ( requestURI )
2018-10-17 03:28:04 -04:00
Only re-render the view(s) you're working on
Hugo already, in its server mode, support partial rebuilds. To put it simply: If you change `about.md`, only that content page is read and processed, then Hugo does some processing (taxonomies etc.) and the full site is rendered.
This commit covers the rendering part: We now only re-render the pages you work on, i.e. the last n pages you watched in the browser (which obviously also includes the page in the example above).
To be more specific: When you are running the hugo server in watch (aka. livereload) mode, and change a template or a content file, then we do a partial re-rendering of the following:
* The current content page (if it is a content change)
* The home page
* Up to the last 10 pages you visited on the site.
This should in most cases be enough, but if you navigate to something completely different, you may see stale content. Doing an edit will then refresh that page.
Note that this feature is enabled by default. To turn it off, run `hugo server --disableFastRender`.
Fixes #3962
See #1643
2017-10-14 07:40:43 -04:00
}
}
2020-05-27 07:50:13 -04:00
2017-09-22 11:13:21 -04:00
h . ServeHTTP ( w , r )
} )
}
fileserver := decorate ( http . FileServer ( fs ) )
2017-11-02 03:25:20 -04:00
mu := http . NewServeMux ( )
2014-08-22 07:59:59 -04:00
if u . Path == "" || u . Path == "/" {
2017-11-02 03:25:20 -04:00
mu . Handle ( "/" , fileserver )
2014-08-22 07:59:59 -04:00
} else {
2017-11-02 03:25:20 -04:00
mu . Handle ( u . Path , http . StripPrefix ( u . Path , fileserver ) )
2014-08-22 07:59:59 -04:00
}
2018-04-09 16:28:03 -04:00
endpoint := net . JoinHostPort ( f . s . serverInterface , strconv . Itoa ( port ) )
2017-11-02 03:25:20 -04:00
2022-03-18 03:54:44 -04:00
return mu , listener , u . String ( ) , endpoint , nil
2017-11-02 03:25:20 -04:00
}
2022-05-12 05:43:20 -04:00
var (
logErrorRe = regexp . MustCompile ( ` (?s)ERROR \d { 4}/\d { 2}/\d { 2} \d { 2}:\d { 2}:\d { 2} ` )
logDuplicateTemplateExecuteRe = regexp . MustCompile ( ` : template: .*?:\d+:\d+: executing ".*?" ` )
logDuplicateTemplateParseRe = regexp . MustCompile ( ` : template: .*?:\d+:\d* ` )
)
2018-10-03 08:58:09 -04:00
func removeErrorPrefixFromLog ( content string ) string {
return logErrorRe . ReplaceAllLiteralString ( content , "" )
}
2022-05-12 05:43:20 -04:00
var logReplacer = strings . NewReplacer (
"can't" , "can’ t" , // Chroma lexer does'nt do well with "can't"
"*hugolib.pageState" , "page.Page" , // Page is the public interface.
2022-05-15 05:40:34 -04:00
"Rebuild failed:" , "" ,
2022-05-12 05:43:20 -04:00
)
2022-05-02 10:07:52 -04:00
func cleanErrorLog ( content string ) string {
2022-05-12 05:43:20 -04:00
content = strings . ReplaceAll ( content , "\n" , " " )
content = logReplacer . Replace ( content )
content = logDuplicateTemplateExecuteRe . ReplaceAllString ( content , "" )
content = logDuplicateTemplateParseRe . ReplaceAllString ( content , "" )
2022-05-02 10:07:52 -04:00
seen := make ( map [ string ] bool )
parts := strings . Split ( content , ": " )
keep := make ( [ ] string , 0 , len ( parts ) )
for _ , part := range parts {
if seen [ part ] {
continue
}
seen [ part ] = true
keep = append ( keep , part )
}
return strings . Join ( keep , ": " )
}
2017-11-02 03:25:20 -04:00
2020-12-02 07:23:25 -05:00
func ( c * commandeer ) serve ( s * serverCmd ) error {
2019-08-15 03:33:47 -04:00
isMultiHost := c . hugo ( ) . IsMultihost ( )
2017-11-02 03:25:20 -04:00
var (
baseURLs [ ] string
roots [ ] string
)
if isMultiHost {
2019-08-15 03:33:47 -04:00
for _ , s := range c . hugo ( ) . Sites {
2017-11-02 03:25:20 -04:00
baseURLs = append ( baseURLs , s . BaseURL . String ( ) )
2019-01-02 06:33:26 -05:00
roots = append ( roots , s . Language ( ) . Lang )
2017-11-02 03:25:20 -04:00
}
} else {
2019-08-15 03:33:47 -04:00
s := c . hugo ( ) . Sites [ 0 ]
2017-11-12 04:03:56 -05:00
baseURLs = [ ] string { s . BaseURL . String ( ) }
2017-11-02 03:25:20 -04:00
roots = [ ] string { "" }
2014-01-26 04:48:00 -05:00
}
2017-11-02 03:25:20 -04:00
2022-05-16 03:22:51 -04:00
// Cache it here. The HugoSites object may be unavaialble later on due to intermitent configuration errors.
// To allow the en user to change the error template while the server is running, we use
// the freshest template we can provide.
var (
errTempl tpl . Template
templHandler tpl . TemplateHandler
)
getErrorTemplateAndHandler := func ( h * hugolib . HugoSites ) ( tpl . Template , tpl . TemplateHandler ) {
if h == nil {
return errTempl , templHandler
}
templHandler := h . Tmpl ( )
errTempl , found := templHandler . Lookup ( "_server/error.html" )
if ! found {
panic ( "template server/error.html not found" )
}
return errTempl , templHandler
2022-05-15 15:01:36 -04:00
}
2022-05-16 03:22:51 -04:00
errTempl , templHandler = getErrorTemplateAndHandler ( c . hugo ( ) )
2022-05-15 15:01:36 -04:00
2017-11-02 03:25:20 -04:00
srv := & fileServer {
2019-12-10 13:56:44 -05:00
baseURLs : baseURLs ,
roots : roots ,
c : c ,
s : s ,
2022-03-17 17:03:27 -04:00
errorTemplate : func ( ctx any ) ( io . Reader , error ) {
2022-05-16 03:22:51 -04:00
// hugoTry does not block, getErrorTemplateAndHandler will fall back
// to cached values if nil.
templ , handler := getErrorTemplateAndHandler ( c . hugoTry ( ) )
2019-12-10 13:56:44 -05:00
b := & bytes . Buffer { }
2022-05-16 03:22:51 -04:00
err := handler . Execute ( templ , b , ctx )
2019-12-10 13:56:44 -05:00
return b , err
} ,
2017-11-02 03:25:20 -04:00
}
2017-11-12 04:03:56 -05:00
doLiveReload := ! c . Cfg . GetBool ( "disableLiveReload" )
if doLiveReload {
livereload . Initialize ( )
}
2020-12-02 07:23:25 -05:00
sigs := make ( chan os . Signal , 1 )
2018-01-14 14:58:52 -05:00
signal . Notify ( sigs , syscall . SIGINT , syscall . SIGTERM )
2022-03-14 11:34:23 -04:00
var servers [ ] * http . Server
2018-01-14 14:58:52 -05:00
2022-03-18 03:54:44 -04:00
wg1 , ctx := errgroup . WithContext ( context . Background ( ) )
2018-02-21 03:23:43 -05:00
for i := range baseURLs {
2022-03-18 03:54:44 -04:00
mu , listener , serverURL , endpoint , err := srv . createEndpoint ( i )
2022-03-14 11:34:23 -04:00
srv := & http . Server {
Addr : endpoint ,
Handler : mu ,
}
servers = append ( servers , srv )
2017-11-02 03:25:20 -04:00
2017-11-12 04:03:56 -05:00
if doLiveReload {
2020-12-02 06:52:26 -05:00
u , err := url . Parse ( helpers . SanitizeURL ( baseURLs [ i ] ) )
if err != nil {
return err
}
mu . HandleFunc ( u . Path + "/livereload.js" , livereload . ServeJS )
mu . HandleFunc ( u . Path + "/livereload" , livereload . Handler )
2017-11-12 04:03:56 -05:00
}
2018-04-09 16:28:03 -04:00
jww . FEEDBACK . Printf ( "Web Server is available at %s (bind address %s)\n" , serverURL , s . serverInterface )
2022-03-18 03:54:44 -04:00
wg1 . Go ( func ( ) error {
err = srv . Serve ( listener )
2022-03-14 11:34:23 -04:00
if err != nil && err != http . ErrServerClosed {
2022-03-18 03:54:44 -04:00
return err
2017-11-02 03:25:20 -04:00
}
2022-03-18 03:54:44 -04:00
return nil
} )
2017-11-02 03:25:20 -04:00
}
jww . FEEDBACK . Println ( "Press Ctrl+C to stop" )
2018-01-14 14:58:52 -05:00
2022-05-15 15:01:36 -04:00
err := func ( ) error {
if s . stop != nil {
for {
select {
case <- sigs :
return nil
case <- s . stop :
return nil
case <- ctx . Done ( ) :
return ctx . Err ( )
}
}
} else {
for {
select {
case <- sigs :
return nil
case <- ctx . Done ( ) :
return ctx . Err ( )
}
}
2018-04-11 03:38:58 -04:00
}
2022-05-15 15:01:36 -04:00
} ( )
if err != nil {
jww . ERROR . Println ( "Error:" , err )
2018-04-11 03:38:58 -04:00
}
2018-01-14 14:58:52 -05:00
2022-05-15 15:01:36 -04:00
if h := c . hugoTry ( ) ; h != nil {
h . Close ( )
}
2020-12-23 03:26:23 -05:00
2022-03-14 11:34:23 -04:00
ctx , cancel := context . WithTimeout ( context . Background ( ) , 5 * time . Second )
defer cancel ( )
2022-03-18 03:54:44 -04:00
wg2 , ctx := errgroup . WithContext ( ctx )
2022-03-14 11:34:23 -04:00
for _ , srv := range servers {
srv := srv
2022-03-18 03:54:44 -04:00
wg2 . Go ( func ( ) error {
2022-03-14 11:34:23 -04:00
return srv . Shutdown ( ctx )
} )
}
2022-03-18 03:54:44 -04:00
err1 , err2 := wg1 . Wait ( ) , wg2 . Wait ( )
if err1 != nil {
return err1
}
return err2
2013-09-29 02:09:03 -04:00
}
2014-08-22 07:59:59 -04:00
2016-10-24 14:56:00 -04:00
// fixURL massages the baseURL into a form needed for serving
2015-01-15 20:02:19 -05:00
// all pages correctly.
2018-04-09 16:28:03 -04:00
func ( sc * serverCmd ) fixURL ( cfg config . Provider , s string , port int ) ( string , error ) {
2014-08-22 07:59:59 -04:00
useLocalhost := false
if s == "" {
2017-02-04 22:20:06 -05:00
s = cfg . GetString ( "baseURL" )
2014-08-22 07:59:59 -04:00
useLocalhost = true
}
2016-05-12 19:06:56 -04:00
2015-01-22 19:46:29 -05:00
if ! strings . HasSuffix ( s , "/" ) {
2015-01-15 20:02:19 -05:00
s = s + "/"
}
2016-05-12 19:06:56 -04:00
// do an initial parse of the input string
2014-08-22 07:59:59 -04:00
u , err := url . Parse ( s )
if err != nil {
return "" , err
}
2016-05-12 19:06:56 -04:00
// if no Host is defined, then assume that no schema or double-slash were
// present in the url. Add a double-slash and make a best effort attempt.
if u . Host == "" && s != "/" {
s = "//" + s
u , err = url . Parse ( s )
if err != nil {
return "" , err
}
}
if useLocalhost {
if u . Scheme == "https" {
2015-01-01 10:32:56 -05:00
u . Scheme = "http"
2014-08-22 07:59:59 -04:00
}
2016-05-12 19:06:56 -04:00
u . Host = "localhost"
}
2018-04-09 16:28:03 -04:00
if sc . serverAppend {
2016-05-12 19:06:56 -04:00
if strings . Contains ( u . Host , ":" ) {
u . Host , _ , err = net . SplitHostPort ( u . Host )
2014-08-22 07:59:59 -04:00
if err != nil {
2022-05-02 10:07:52 -04:00
return "" , fmt . Errorf ( "Failed to split baseURL hostpost: %w" , err )
2014-08-22 07:59:59 -04:00
}
}
2017-11-02 03:25:20 -04:00
u . Host += fmt . Sprintf ( ":%d" , port )
2014-08-22 07:59:59 -04:00
}
return u . String ( ) , nil
}
2014-09-22 09:45:05 -04:00
func memStats ( ) error {
2018-04-13 02:42:29 -04:00
b := newCommandsBuilder ( )
sc := b . newServerCmd ( ) . getCommand ( )
2018-04-09 16:28:03 -04:00
memstats := sc . Flags ( ) . Lookup ( "memstats" ) . Value . String ( )
2014-09-22 09:45:05 -04:00
if memstats != "" {
2018-04-09 16:28:03 -04:00
interval , err := time . ParseDuration ( sc . Flags ( ) . Lookup ( "meminterval" ) . Value . String ( ) )
2014-09-22 09:45:05 -04:00
if err != nil {
interval , _ = time . ParseDuration ( "100ms" )
}
fileMemStats , err := os . Create ( memstats )
if err != nil {
return err
}
fileMemStats . WriteString ( "# Time\tHeapSys\tHeapAlloc\tHeapIdle\tHeapReleased\n" )
go func ( ) {
var stats runtime . MemStats
2022-04-26 13:57:04 -04:00
start := htime . Now ( ) . UnixNano ( )
2014-09-22 09:45:05 -04:00
for {
runtime . ReadMemStats ( & stats )
if fileMemStats != nil {
fileMemStats . WriteString ( fmt . Sprintf ( "%d\t%d\t%d\t%d\t%d\n" ,
2022-04-26 13:57:04 -04:00
( htime . Now ( ) . UnixNano ( ) - start ) / 1000000 , stats . HeapSys , stats . HeapAlloc , stats . HeapIdle , stats . HeapReleased ) )
2014-09-22 09:45:05 -04:00
time . Sleep ( interval )
} else {
break
}
}
} ( )
}
return nil
}