From 91947b9ffaba0ff1b56c2bf8f769369782235edd Mon Sep 17 00:00:00 2001 From: David Stotijn Date: Sun, 27 Sep 2020 11:33:10 +0200 Subject: [PATCH] Enforce trailing slashes on API paths This ensures that the Next.js app works consistently both when running in dev mode as well as when it has been exported to static HTML/JS. --- admin/next.config.js | 4 ++-- admin/src/lib/graphql.ts | 2 +- cmd/hetty/main.go | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/admin/next.config.js b/admin/next.config.js index 8a029e9..0898b94 100644 --- a/admin/next.config.js +++ b/admin/next.config.js @@ -6,8 +6,8 @@ module.exports = withCSS({ async rewrites() { return [ { - source: "/api/:path", - destination: "http://localhost:8080/api/:path", // Matched parameters can be used in the destination + source: "/api/:path/", + destination: "http://localhost:8080/api/:path/", }, ]; }, diff --git a/admin/src/lib/graphql.ts b/admin/src/lib/graphql.ts index ada1469..3863d71 100644 --- a/admin/src/lib/graphql.ts +++ b/admin/src/lib/graphql.ts @@ -8,7 +8,7 @@ function createApolloClient() { return new ApolloClient({ ssrMode: typeof window === "undefined", link: new HttpLink({ - uri: "/api/graphql", + uri: "/api/graphql/", }), cache: new InMemoryCache({ typePolicies: { diff --git a/cmd/hetty/main.go b/cmd/hetty/main.go index e329d76..7224cd8 100644 --- a/cmd/hetty/main.go +++ b/cmd/hetty/main.go @@ -81,11 +81,11 @@ func main() { hostname, _ := os.Hostname() host, _, _ := net.SplitHostPort(req.Host) return strings.EqualFold(host, hostname) || (req.Host == "hetty.proxy" || req.Host == "localhost:8080") - }).Subrouter() + }).Subrouter().StrictSlash(true) // GraphQL server. - adminRouter.Path("/api/playground").Handler(playground.Handler("GraphQL Playground", "/api/graphql")) - adminRouter.Path("/api/graphql").Handler(handler.NewDefaultServer(api.NewExecutableSchema(api.Config{Resolvers: &api.Resolver{ + adminRouter.Path("/api/playground/").Handler(playground.Handler("GraphQL Playground", "/api/graphql/")) + adminRouter.Path("/api/graphql/").Handler(handler.NewDefaultServer(api.NewExecutableSchema(api.Config{Resolvers: &api.Resolver{ RequestLogService: reqLogService, }})))