diff options
Diffstat (limited to 'commands')
-rw-r--r-- | commands/commandeer.go | 5 | ||||
-rw-r--r-- | commands/server.go | 30 |
2 files changed, 33 insertions, 2 deletions
diff --git a/commands/commandeer.go b/commands/commandeer.go index 69418b299..52a47484f 100644 --- a/commands/commandeer.go +++ b/commands/commandeer.go @@ -346,7 +346,10 @@ func (c *commandeer) loadConfig(mustHaveConfigFile, running bool) error { cfg.Logger = logger c.logger = logger - c.serverConfig = hconfig.DecodeServer(cfg.Cfg) + c.serverConfig, err = hconfig.DecodeServer(cfg.Cfg) + if err != nil { + return err + } createMemFs := config.GetBool("renderToMemory") diff --git a/commands/server.go b/commands/server.go index f8370107f..ee3d25a41 100644 --- a/commands/server.go +++ b/commands/server.go @@ -292,6 +292,18 @@ type fileServer struct { s *serverCmd } +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 + +} + func (f *fileServer) createEndpoint(i int) (*http.ServeMux, string, string, error) { baseURL := f.baseURLs[i] root := f.roots[i] @@ -356,10 +368,25 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, string, string, erro w.Header().Set("Pragma", "no-cache") } - for _, header := range f.c.serverConfig.Match(r.RequestURI) { + for _, header := range f.c.serverConfig.MatchHeaders(r.RequestURI) { w.Header().Set(header.Key, header.Value) } + if redirect := f.c.serverConfig.MatchRedirect(r.RequestURI); !redirect.IsZero() { + // This matches Netlify's behaviour and is needed for SPA behaviour. + // See https://docs.netlify.com/routing/redirects/rewrites-proxies/ + if redirect.Status == 200 { + if r2 := f.rewriteRequest(r, strings.TrimPrefix(redirect.To, u.Path)); r2 != nil { + r = r2 + } + } else { + w.Header().Set("Content-Type", "") + http.Redirect(w, r, redirect.To, redirect.Status) + return + } + + } + if f.c.fastRenderMode && f.c.buildErr == nil { p := strings.TrimSuffix(r.RequestURI, "?"+r.URL.RawQuery) @@ -379,6 +406,7 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, string, string, erro } } + h.ServeHTTP(w, r) }) } |