Skip to content

Commit

Permalink
nextjs: fix handler overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Mar 22, 2024
1 parent d5feae3 commit 7c3ab59
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions internal/nextjs/nextjs.go
Expand Up @@ -144,20 +144,21 @@ func traverse(fs fs.ReadDirFS, fp string, segments []string, parent *node) error
name = "[]"
}

var handler http.Handler
if !child.IsDir() {
handler = httpFileHandler(fs, childPath)
}

// files may share the same name as a directory, so we need to check if the node already exists
childNode, ok := parent.sub[name]
if !ok {
childNode = node{
h: handler,
sub: make(map[string]node),
}
}

if !child.IsDir() {
if childNode.h != nil {
return fmt.Errorf("failed to add route %q: route already exists", childPath)
}
childNode.h = httpFileHandler(fs, childPath)
}

if child.IsDir() {
if err := traverse(fs, childPath, append(segments, name), &childNode); err != nil {
return err
Expand Down

0 comments on commit 7c3ab59

Please sign in to comment.